From 3e52c6ba8cf9603809523aa2939395a3fc023666 Mon Sep 17 00:00:00 2001 From: yingjun Date: Sun, 31 Jul 2016 22:42:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0redis=20cluster=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yingjun/ssm/cache/RedisClusterCache.java | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/main/java/com/yingjun/ssm/cache/RedisClusterCache.java diff --git a/src/main/java/com/yingjun/ssm/cache/RedisClusterCache.java b/src/main/java/com/yingjun/ssm/cache/RedisClusterCache.java new file mode 100644 index 0000000..3f2ab71 --- /dev/null +++ b/src/main/java/com/yingjun/ssm/cache/RedisClusterCache.java @@ -0,0 +1,124 @@ +package com.yingjun.ssm.cache; + +import com.yingjun.ssm.util.ProtoStuffSerializerUtil; +import org.springframework.stereotype.Component; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.JedisPool; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + + +/** + * redis缓存 + * + * 采用Jedis Cluster + * + * @author yingjun + * + */ +@Component +public class RedisClusterCache { + + + public final static String CAHCENAME="cache";//缓存名 + public final static int CAHCETIME=60;//默认缓存时间 + + //@Autowired + private JedisCluster jedisCluster; + + + public void putCache(String key, T obj) { + final byte[] bkey = key.getBytes(); + final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj); + jedisCluster.set(bkey,bvalue); + } + + public void putCacheWithExpireTime(String key, T obj, int expireTime) { + final byte[] bkey = key.getBytes(); + final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj); + jedisCluster.setex(bkey, expireTime, bvalue); + } + + public void putListCache(String key, List objList) { + final byte[] bkey = key.getBytes(); + final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList); + jedisCluster.set(bkey,bvalue); + } + + public void putListCacheWithExpireTime(String key, List objList, int expireTime) { + final byte[] bkey = key.getBytes(); + final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList); + jedisCluster.setex(bkey, expireTime, bvalue); + } + + public T getCache(final String key, Class targetClass) { + byte[] result =jedisCluster.get(key.getBytes()); + if (result == null) { + return null; + } + return ProtoStuffSerializerUtil.deserialize(result, targetClass); + } + + public List getListCache(String key, Class targetClass) { + byte[] result =jedisCluster.get(key.getBytes()); + if (result == null) { + return null; + } + return ProtoStuffSerializerUtil.deserializeList(result, targetClass); + } + + /** + * 精确删除key + * + * @param key + */ + public void deleteCache(String key) { + jedisCluster.del(key); + } + + /** + * 模糊删除key + * + * @param pattern + */ + public void deleteCacheWithPattern(String pattern) { + Set keys =this.keys(pattern); + for(String key:keys){ + jedisCluster.del(key); + } + } + + /** + * 清空所有缓存 + */ + public void clearCache() { + deleteCacheWithPattern(RedisClusterCache.CAHCENAME+"|*"); + } + + /** + * 由于JedisCluster没有提供对keys命令的封装,只能自己实现 + * @param pattern + * @return + */ + public Set keys(String pattern){ + Set keys = new HashSet<>(); + Map clusterNodes = jedisCluster.getClusterNodes(); + for(String k : clusterNodes.keySet()){ + JedisPool jp = clusterNodes.get(k); + Jedis connection = jp.getResource(); + try { + keys.addAll(connection.keys(pattern)); + } catch(Exception e){ + e.printStackTrace(); + } finally{ + //用完一定要close这个链接!!! + connection.close(); + } + } + return keys; + } +} -- GitLab