forked from wosyingjun/beauty_ssm_dubbo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7e5b8b
commit 69e948e
Showing
5 changed files
with
182 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
beautyssm_common/src/main/java/com/yingjun/ssm/common/util/cache/RedisClusterCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package com.yingjun.ssm.common.util.cache; | ||
|
||
import com.yingjun.ssm.common.util.ProtoStuffSerializerUtil; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import redis.clients.jedis.Jedis; | ||
import redis.clients.jedis.JedisCluster; | ||
import redis.clients.jedis.JedisPool; | ||
|
||
import java.util.*; | ||
|
||
|
||
/** | ||
* 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 <T> void putCache(String key, T obj) { | ||
final byte[] bkey = key.getBytes(); | ||
final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj); | ||
jedisCluster.set(bkey,bvalue); | ||
} | ||
|
||
public <T> 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 <T> void putListCache(String key, List<T> objList) { | ||
final byte[] bkey = key.getBytes(); | ||
final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList); | ||
jedisCluster.set(bkey,bvalue); | ||
} | ||
|
||
public <T> void putListCacheWithExpireTime(String key, List<T> objList, int expireTime) { | ||
final byte[] bkey = key.getBytes(); | ||
final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList); | ||
jedisCluster.setex(bkey, expireTime, bvalue); | ||
} | ||
|
||
public <T> T getCache(final String key, Class<T> targetClass) { | ||
byte[] result =jedisCluster.get(key.getBytes()); | ||
if (result == null) { | ||
return null; | ||
} | ||
return ProtoStuffSerializerUtil.deserialize(result, targetClass); | ||
} | ||
|
||
public <T> List<T> getListCache(String key, Class<T> 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<String> keys =this.keys(pattern); | ||
for(String key:keys){ | ||
jedisCluster.del(key); | ||
} | ||
} | ||
|
||
/** | ||
* 清空所有缓存 | ||
*/ | ||
public void clearCache() { | ||
deleteCacheWithPattern(RedisClusterCache.CAHCENAME+"|*"); | ||
} | ||
|
||
/** | ||
* 由于JedisCluster没有提供对keys命令的封装,只能自己实现 | ||
* @param pattern | ||
* @return | ||
*/ | ||
public Set<String> keys(String pattern){ | ||
Set<String> keys = new HashSet<>(); | ||
Map<String, JedisPool> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters