shiro-redis存取改为hash

This commit is contained in:
zhuangqian 2017-01-07 15:11:38 +08:00
parent a28c7a2ecf
commit 95657aa301
2 changed files with 10 additions and 14 deletions

View File

@ -57,7 +57,7 @@ public class RedisCache<K, V> implements Cache<K, V>, Serializable {
this.name = name; this.name = name;
} }
private final byte[] getKeyToBytes(K key) { private final byte[] toBytes(Object key) {
if (key instanceof byte[]) { if (key instanceof byte[]) {
return (byte[]) key; return (byte[]) key;
} }
@ -67,11 +67,7 @@ public class RedisCache<K, V> implements Cache<K, V>, Serializable {
} }
private final byte[] getPrefixToBytes() { private final byte[] getPrefixToBytes() {
return SafeEncoder.encode(getName().concat("_").concat(getKeyPrefix())); return SafeEncoder.encode(getKeyPrefix());
}
private final byte[] getKeyPattern() {
return SafeEncoder.encode(getName().concat("_").concat(getKeyPrefix()) + "*");
} }
@Override @Override
@ -84,7 +80,7 @@ public class RedisCache<K, V> implements Cache<K, V>, Serializable {
if (key == null) { if (key == null) {
return null; return null;
} else { } else {
V value = jedis.get(getKeyToBytes(key)); V value = jedis.hget(toBytes(getName()), toBytes(key));
if (value == null) { if (value == null) {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("缓存主键: [" + key + "] 对应的值为空"); LOGGER.debug("缓存主键: [" + key + "] 对应的值为空");
@ -110,7 +106,7 @@ public class RedisCache<K, V> implements Cache<K, V>, Serializable {
IJedis jedis = initJedis(); IJedis jedis = initJedis();
try { try {
V previous = get(key); V previous = get(key);
jedis.set(getKeyToBytes(key), value); jedis.hset(toBytes(getName()), toBytes(key), value);
return previous; return previous;
} catch (Throwable t) { } catch (Throwable t) {
throw new CacheException(t); throw new CacheException(t);
@ -125,10 +121,10 @@ public class RedisCache<K, V> implements Cache<K, V>, Serializable {
IJedis jedis = initJedis(); IJedis jedis = initJedis();
try { try {
V previous = get(key); V previous = get(key);
long statusCode = jedis.del(getKeyToBytes(key)); long statusCode = jedis.hdel(toBytes(getName()), toBytes(key));
if (statusCode > 0) { if (statusCode > 0) {
if (LOGGER.isInfoEnabled()) { if (LOGGER.isInfoEnabled()) {
LOGGER.info("缓存主键 [{}] 删除成功", key); LOGGER.info("从缓存名[{}] 缓存主键 [{}] 删除缓存成功", getName(), key);
} }
} }
return previous; return previous;
@ -171,10 +167,10 @@ public class RedisCache<K, V> implements Cache<K, V>, Serializable {
public Set<K> keys() { public Set<K> keys() {
IJedis jedis = initJedis(); IJedis jedis = initJedis();
try { try {
Set<byte[]> keySet = jedis.keys(getKeyPattern()); Set<Object> keySet = jedis.hkeys(toBytes(getName()));
if (!CollectionUtils.isEmpty(keySet)) { if (!CollectionUtils.isEmpty(keySet)) {
Set<K> keys = new LinkedHashSet<K>(); Set<K> keys = new LinkedHashSet<K>();
for (byte[] key : keySet) { for (Object key : keySet) {
keys.add((K) key); keys.add((K) key);
} }
return keys; return keys;
@ -239,5 +235,5 @@ public class RedisCache<K, V> implements Cache<K, V>, Serializable {
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
} }

View File

@ -68,7 +68,7 @@ public class RedisCacheManager implements CacheManager, Initializable, Destroyab
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings({ "unchecked", "rawtypes" })
public <K, V> Cache<K, V> getCache(String name) throws CacheException { public <K, V> Cache<K, V> getCache(String name) throws CacheException {
Cache cache = caches.get(name); Cache cache = caches.get(name);
if (cache == null) { if (null == cache) {
cache = new RedisCache<K, V>(getRedisName(), getKeyPrefix(), name); cache = new RedisCache<K, V>(getRedisName(), getKeyPrefix(), name);
caches.put(name, cache); caches.put(name, cache);
} }