org.crazycake.shiro.RedisCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shiro-redis Show documentation
Show all versions of shiro-redis Show documentation
shiro only provide the support of ehcache and concurrentHashMap. Here is an implement of redis cache can be used by shiro. Hope it will help you!
package org.crazycake.shiro;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
public class RedisCache implements Cache {
private static Logger logger = LoggerFactory.getLogger(RedisCache.class);
private static final String DEFAULT_CACHE_KEY_PREFIX = "shiro:cache:";
private RedisSerializer keySerializer = new StringSerializer();
private RedisSerializer valueSerializer = new ObjectSerializer();
private IRedisManager redisManager;
private String keyPrefix = DEFAULT_CACHE_KEY_PREFIX;
/**
* Construction
* @param redisManager
*/
public RedisCache(IRedisManager redisManager, RedisSerializer keySerializer, RedisSerializer valueSerializer){
if (redisManager == null) {
throw new IllegalArgumentException("Cache argument cannot be null.");
}
this.redisManager = redisManager;
this.keySerializer = keySerializer;
this.valueSerializer = valueSerializer;
}
/**
* Constructs a cache instance with the specified
* Redis manager and using a custom key prefix.
* @param cache The cache manager instance
* @param prefix The Redis key prefix
*/
public RedisCache(IRedisManager cache, RedisSerializer keySerializer, RedisSerializer valueSerializer,
String prefix){
this( cache, keySerializer, valueSerializer );
this.keyPrefix = prefix;
}
@Override
public V get(K key) throws CacheException {
logger.debug("get key [" + key + "]");
if (key == null) {
return null;
}
try {
Object redisCacheKey = getRedisCacheKey(key);
byte[] rawValue = redisManager.get(keySerializer.serialize(redisCacheKey));
V value = (V) valueSerializer.deserialize(rawValue);
return value;
} catch (SerializationException e) {
throw new CacheException(e);
}
}
@Override
public V put(K key, V value) throws CacheException {
logger.debug("put key [" + key + "]");
try {
Object redisCacheKey = getRedisCacheKey(key);
redisManager.set(keySerializer.serialize(redisCacheKey), valueSerializer.serialize(value));
return value;
} catch (SerializationException e) {
throw new CacheException(e);
}
}
@Override
public V remove(K key) throws CacheException {
logger.debug("remove key [" + key + "]");
if (key == null) {
return null;
}
try {
Object redisCacheKey = getRedisCacheKey(key);
byte[] rawValue = redisManager.get(keySerializer.serialize(redisCacheKey));
V previous = (V) valueSerializer.deserialize(rawValue);
redisManager.del(keySerializer.serialize(redisCacheKey));
return previous;
} catch (SerializationException e) {
throw new CacheException(e);
}
}
private Object getRedisCacheKey(K key) {
if (key == null) {
return null;
}
Object redisKey = key;
if (keySerializer instanceof StringSerializer) {
redisKey = getStringRedisKey(key);
}
if (redisKey instanceof String) {
return this.keyPrefix + (String)redisKey;
}
return redisKey;
}
private Object getStringRedisKey(K key) {
Object redisKey;
if (key instanceof PrincipalCollection) {
redisKey = getRedisKeyFromPrincipalCollection((PrincipalCollection) key);
} else {
redisKey = key.toString();
}
return redisKey;
}
private Object getRedisKeyFromPrincipalCollection(PrincipalCollection key) {
Object redisKey;
List realmNames = getRealmNames(key);
Collections.sort(realmNames);
redisKey = joinRealmNames(realmNames);
return redisKey;
}
private List getRealmNames(PrincipalCollection key) {
List realmArr = new ArrayList();
Set realmNames = key.getRealmNames();
for (String realmName: realmNames) {
realmArr.add(realmName);
}
return realmArr;
}
private Object joinRealmNames(List realmArr) {
Object redisKey;
StringBuilder redisKeyBuilder = new StringBuilder();
for (int i = 0; i < realmArr.size(); i++) {
String s = realmArr.get(i);
redisKeyBuilder.append(s);
}
redisKey = redisKeyBuilder.toString();
return redisKey;
}
@Override
public void clear() throws CacheException {
logger.debug("clear cache");
Set keys = null;
try {
keys = redisManager.keys(keySerializer.serialize(this.keyPrefix + "*"));
} catch (SerializationException e) {
logger.error("get keys error", e);
}
if (keys == null || keys.size() == 0) {
return;
}
for (byte[] key: keys) {
redisManager.del(key);
}
}
@Override
public int size() {
Long longSize = new Long(redisManager.dbSize());
return longSize.intValue();
}
@SuppressWarnings("unchecked")
@Override
public Set keys() {
Set keys = null;
try {
keys = redisManager.keys(keySerializer.serialize(this.keyPrefix + "*"));
} catch (SerializationException e) {
logger.error("get keys error", e);
return Collections.emptySet();
}
if (CollectionUtils.isEmpty(keys)) {
return Collections.emptySet();
}
Set convertedKeys = new HashSet();
for(byte[] key:keys){
try {
convertedKeys.add((K)keySerializer.deserialize(key));
} catch (SerializationException e) {
logger.error("deserialize keys error", e);
}
}
return convertedKeys;
}
@Override
public Collection values() {
Set keys = null;
try {
keys = redisManager.keys(keySerializer.serialize(this.keyPrefix + "*"));
} catch (SerializationException e) {
logger.error("get values error", e);
return Collections.emptySet();
}
if(CollectionUtils.isEmpty(keys)) {
return Collections.emptySet();
}
List values = new ArrayList(keys.size());
for (byte[] key : keys) {
V value = null;
try {
value = (V)valueSerializer.deserialize(redisManager.get(key));
} catch (SerializationException e) {
logger.error("deserialize values= error", e);
}
if (value != null) {
values.add(value);
}
}
return Collections.unmodifiableList(values);
}
public String getKeyPrefix() {
return keyPrefix;
}
public void setKeyPrefix(String keyPrefix) {
this.keyPrefix = keyPrefix;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy