cache 缓存 数据结构设计

package com.gochinatv.vrs.framework.dao.cacheDao.base;

import com.gochinatv.vrs.framework.cacheutil.TransforCache;
import com.gochinatv.vrs.framework.util.ReflexUtil;
import net.sf.json.JSONArray;

import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by shhao
 * Date: 14-4-25.
 * Time:下午9:49
 */
public abstract class BaseCacheDao<T> {
    protected Map<String, String> referMap = new HashMap<String, String>();
    protected String objectId = null;
    public TransforCache transforCache = new TransforCache();
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    protected String clazzName = null;

    public void init() {
        if (referMap.size() == 0||!checkParam()) {
            return;
        }

        checkReferenceIndex();
    }

    protected boolean checkParam() {
        if (StringUtils.isEmpty(clazzName) || StringUtils.isEmpty(objectId)) {
            logger.info("clazzName or objectId is null ! them are is required!");
            return false;
        }
        return true;
    }

    //初始化 checkReferenceIndex
    private void checkReferenceIndex() {
        T t = null;
        Map<String, String> notExist = new HashMap<String, String>();
        for (String key : referMap.keySet()) {
            if (transforCache.exists(key + ":*")) {
                continue;
            }
            notExist.put(key, referMap.get(key));
        }
        if (notExist.isEmpty()) return; //不存在 reference_key
        Set<String> set = transforCache.getKeys(clazzName + ":*");
        Map<String, JSONArray> cacheMap = new HashMap<String, JSONArray>();
        //循环所有 cache 保存 video
        for (String value : set) {
            T object = (T) JSONObject.toBean(JSONObject.fromObject(value));
            for (String key : notExist.keySet()) {
                String soreKey = key; //album_video:123   video_list
                if (!StringUtils.isEmpty(notExist.get(key))) {
                    Object proValue = ReflexUtil.invokeMethod(notExist.get(key), t);
                    if (proValue == null) continue;
                    soreKey = key + ":" + proValue;
                }
                JSONArray cacheJson = null;
                if (cacheMap.get(soreKey) == null) {
                    cacheJson = new JSONArray();
                } else {
                    cacheJson = cacheMap.get(soreKey);
                }
                cacheJson.add(ReflexUtil.invokeMethod(objectId, t));
                cacheMap.put(soreKey, cacheJson);
            }
        }

        // 向缓存添加 reference
        for (String key : cacheMap.keySet()) {
            transforCache.setJsonArrayCache(key, cacheMap.get(key), null);
        }
    }

    public JSONObject get(String key) {
        return JSONObject.fromObject(transforCache.getJsonCache(key));
    }

    //
    public void save(T t) {
        if (referMap.size() > 0) {
            addReference(t);
        }
        Object objId = ReflexUtil.invokeMethod(objectId, t);
        if (objId == null) return;
        transforCache.setJsonCache(clazzName + ":" + objId, t, "");
    }


    public void remove(T t) {
        if (referMap.size() > 0) {
            removeReference(t);
        }
        Object objId = ReflexUtil.invokeMethod(objectId, t);
        if (objId == null) return;
        transforCache.removeCache(clazzName + ":" + objId);
    }


    public void update(T t) {
        if (referMap.size() > 0) {
            removeReference(t);
            addReference(t);
        }
        Object objId = ReflexUtil.invokeMethod(objectId, t);
        if (objId == null) return;
        transforCache.setJsonCache(clazzName + ":" + objId, t, "");

    }

    //  add reference
    public void addReference(T t) {
        Object objId = ReflexUtil.invokeMethod(objectId, t);
        for (String key : referMap.keySet()) { //遍历所有reference key
            String soreKey = key; //album_video:123   video_list
            if (!StringUtils.isEmpty(referMap.get(key))) {
                Object proValue = ReflexUtil.invokeMethod(referMap.get(key), t);
                if (proValue == null) continue;
                soreKey = key + ":" + proValue;
            }
            JSONArray jsonArray = transforCache.getJsonArray(soreKey);
            if (jsonArray == null) jsonArray = new JSONArray();
            if (jsonArray.contains(objId)) continue;
            jsonArray.add(objId);//添加
            logger.info("T={} add reference key={} value={}", new Object[]{clazzName, soreKey, objId});
            transforCache.setJsonArrayCache(soreKey, jsonArray, null);
        }
    }

    //  remove  reference
    public void removeReference(T t) {
        Object objId = ReflexUtil.invokeMethod(objectId, t);
        for (String key : referMap.keySet()) { //遍历所有reference key
            String soreKey = key; //album_video:123   video_list
            if (!StringUtils.isEmpty(referMap.get(key))) {
                Object proValue = ReflexUtil.invokeMethod(referMap.get(key), t);
                if (proValue == null) continue;
                soreKey = key + ":" + proValue;
            }
            JSONArray jsonArray = transforCache.getJsonArray(soreKey);
            if (jsonArray == null) continue;
            if (!jsonArray.contains(objId)) continue;
            jsonArray.remove(objId);
            logger.info("T={} add reference key={} value={}", new Object[]{clazzName, soreKey, objId});
            transforCache.setJsonArrayCache(soreKey, jsonArray, null);
        }
    }


}



TransforCache


public class TransforCache {

    static JsonDateValueProcessor jsonDateValueProcessor = new JsonDateValueProcessor();

    @SuppressWarnings("static-access")
    public void setJsonCache(String key, Object obj, String excludeProperties) {
        JedisPoolUtils jedisPoolUtils = new JedisPoolUtils();
        Jedis jedis = null;
        try {
            jedis = jedisPoolUtils.getJedis();
            String value = configReturnString(obj, excludeProperties);
            jedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedisPoolUtils.returnRes(jedis);
        }
    }

    @SuppressWarnings("static-access")
    public void setJsonArrayCache(String key, JSONArray obj, String excludeProperties) {
        JedisPoolUtils jedisPoolUtils = new JedisPoolUtils();
        Jedis jedis = null;
        try {
            jedis = jedisPoolUtils.getJedis();
            String value = configJsonArray(obj, excludeProperties);
            jedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedisPoolUtils.returnRes(jedis);
        }
    }

    @SuppressWarnings("static-access")
    public void setStringCache(String key, String value) {
        JedisPoolUtils jedisPoolUtils = new JedisPoolUtils();
        Jedis jedis = null;
        try {
            jedis = jedisPoolUtils.getJedis();
            jedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedisPoolUtils.returnRes(jedis);
        }
    }

    @SuppressWarnings("static-access")
    public String getJsonCache(String key) {
        JedisPoolUtils jedisPoolUtils = new JedisPoolUtils();
        Jedis jedis = null;
        try {
            jedis = jedisPoolUtils.getJedis();
            return jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedisPoolUtils.returnRes(jedis);
        }
        return null;
    }

    @SuppressWarnings("static-access")
    public JSONArray getJsonArray(String key) {
        JedisPoolUtils jedisPoolUtils = new JedisPoolUtils();
        Jedis jedis = null;
        try {
            jedis = jedisPoolUtils.getJedis();
            return JSONArray.fromObject(jedis.get(key));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedisPoolUtils.returnRes(jedis);
        }
        return null;
    }

    @SuppressWarnings("static-access")
    public void removeCache(String key) {
        JedisPoolUtils jedisPoolUtils = new JedisPoolUtils();
        Jedis jedis = null;
        try {
            jedis = jedisPoolUtils.getJedis();
            jedis.del(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedisPoolUtils.returnRes(jedis);
        }
    }

    @SuppressWarnings("static-access")
    public String reset() {
        JedisPoolUtils jedisPoolUtils = new JedisPoolUtils();
        Jedis jedis = null;
        try {
            jedis = jedisPoolUtils.getJedis();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedisPoolUtils.returnRes(jedis);
        }
        return jedis.flushDB();
    }

    public String configReturnString(Object obj, String excludeProperties) throws Exception {
        JsonConfig config = new JsonConfig();
        config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
        String[] fields = null;
        if (-1 != excludeProperties.indexOf(",")) {
            fields = excludeProperties.split(",");
        } else {
            fields = new String[]{excludeProperties};
        }
        if (null != fields)
            config.setJsonPropertyFilter(new IgnoreFieldProcessorImpl(fields));
        config.registerJsonValueProcessor(Date.class, jsonDateValueProcessor);
        return JSONObject.fromObject(obj, config).toString();
    }

    public String configJsonArray(Object obj, String excludeProperties) throws Exception {
        JsonConfig config = new JsonConfig();
        config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
        String[] fields = null;
        if (-1 != excludeProperties.indexOf(",")) {
            fields = excludeProperties.split(",");
        } else {
            fields = new String[]{excludeProperties};
        }
        if (null != fields)
            config.setJsonPropertyFilter(new IgnoreFieldProcessorImpl(fields));
        config.registerJsonValueProcessor(Date.class, jsonDateValueProcessor);
        return JSONArray.fromObject(obj, config).toString();
    }

    // key is exists
    public boolean exists(String key) {
        JedisPoolUtils jedisPoolUtils = new JedisPoolUtils();
        Jedis jedis = null;
        boolean exist = false;
        try {
            jedis = jedisPoolUtils.getJedis();
            exist = jedis.exists(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedisPoolUtils.returnRes(jedis);
        }
        return exist;
    }


    //get key contain k ,such as video:*

    public Set<String> getKeys(String key) {
        JedisPoolUtils jedisPoolUtils = new JedisPoolUtils();
        Jedis jedis = null;
        Set<String> set = null;
        try {
            jedis = jedisPoolUtils.getJedis();
            set = jedis.keys(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedisPoolUtils.returnRes(jedis);
        }
        return set;
    }
}



JedisPoolUtils


package com.gochinatv.vrs.framework.cacheutil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.ResourceBundle;

public class JedisPoolUtils {
	
	private static JedisPool pool;

    /**
     * 建立连接池 真实环境,一般把配置参数缺抽取出来。
     * 
     */
    private static void createJedisPool() {
    	
    	Properties pro = new Properties();
        try {
          pro.load(JedisPoolUtils.class.getResourceAsStream("/redis.properties"));
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
        
        ResourceBundle bundle = ResourceBundle.getBundle("redis");   
        if (bundle == null) {   
            throw new IllegalArgumentException(   
                    "[redis.properties] is not found!");   
        } 
    	

        // 建立连接池配置参数
        JedisPoolConfig config = new JedisPoolConfig();

        // 设置最大连接数
        config.setMaxActive(Integer.valueOf(bundle.getString("redis.pool.maxActive")));

        // 设置最大阻塞时间,记住是毫秒数milliseconds
        config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));

        // 设置空间连接
        config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));

        // 创建连接池
        pool = new JedisPool(config, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")));

    }

    /**
     * 在多线程环境同步初始化
     */
    private static synchronized void poolInit() {
        if (pool == null)
            createJedisPool();
    }

    /**
     * 获取一个jedis 对象
     * 
     * @return
     */
    public static Jedis getJedis() {

        if (pool == null)
            poolInit();
        return pool.getResource();
    }

    /**
     * 归还一个连接
     * 
     * @param jedis
     */
    public static void returnRes(Jedis jedis) {
        pool.returnResource(jedis);
    }

}


你可能感兴趣的:(cache 缓存 数据结构设计)