All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.streamone.shiro.cache.RedissonShiroCacheManager Maven / Gradle / Ivy

package com.github.streamone.shiro.cache;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.io.ResourceUtils;
import org.apache.shiro.util.Initializable;
import org.redisson.api.RMap;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.spring.cache.CacheConfig;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * 

A {@link org.apache.shiro.cache.CacheManager} implementation * backed by Redisson instance.

* * @author streamone */ public class RedissonShiroCacheManager implements CacheManager, Initializable { private boolean allowNullValues = true; private Codec codec = new JsonJacksonCodec(); private RedissonClient redisson; private String configLocation; private Map configMap = new ConcurrentHashMap<>(); private ConcurrentMap instanceMap = new ConcurrentHashMap<>(); public RedissonShiroCacheManager(){} public RedissonShiroCacheManager(RedissonClient redisson){ this(redisson, (String)null, null); } public RedissonShiroCacheManager(RedissonClient redisson, Map config) { this(redisson, config, null); } public RedissonShiroCacheManager(RedissonClient redisson, Map config, Codec codec) { this.redisson = redisson; this.configMap = (Map) config; if (codec != null) { this.codec = codec; } } public RedissonShiroCacheManager(RedissonClient redisson, String configLocation) { this(redisson, configLocation, null); } public RedissonShiroCacheManager(RedissonClient redisson, String configLocation, Codec codec) { this.redisson = redisson; this.configLocation = configLocation; if (codec != null) { this.codec = codec; } } protected CacheConfig createDefaultConfig() { return new CacheConfig(); } @Override public Cache getCache(String name) throws CacheException { Cache cache = this.instanceMap.get(name); if (cache != null) { return cache; } CacheConfig config = this.configMap.get(name); if (config == null) { config = createDefaultConfig(); configMap.put(name, config); } if (config.getMaxIdleTime() == 0 && config.getTTL() == 0 && config.getMaxSize() == 0) { return createMap(name, config); } return createMapCache(name, config); } private Cache createMap(String name, CacheConfig config) { RMap map = getMap(name, config); Cache cache = new RedissonShiroCache<>(map, this.allowNullValues); Cache oldCache = this.instanceMap.putIfAbsent(name, cache); if (oldCache != null) { cache = oldCache; } return cache; } protected RMap getMap(String name, CacheConfig config) { if (this.codec != null) { return this.redisson.getMap(name, this.codec); } return this.redisson.getMap(name); } private Cache createMapCache(String name, CacheConfig config) { RMapCache map = getMapCache(name, config); Cache cache = new RedissonShiroCache<>(map, config, this.allowNullValues); Cache oldCache = this.instanceMap.putIfAbsent(name, cache); if (oldCache != null) { cache = oldCache; } else { map.setMaxSize(config.getMaxSize()); } return cache; } protected RMapCache getMapCache(String name, CacheConfig config) { if (this.codec != null) { return this.redisson.getMapCache(name, this.codec); } return redisson.getMapCache(name); } @Override public void init() { if (this.configLocation == null) { return; } try { this.configMap = (Map) CacheConfig.fromJSON(ResourceUtils.getInputStreamForPath(this.configLocation)); } catch (IOException e) { // try to read yaml try { this.configMap = (Map) CacheConfig.fromYAML(ResourceUtils.getInputStreamForPath(this.configLocation)); } catch (IOException e1) { throw new IllegalArgumentException( "Could not parse cache configuration at [" + configLocation + "]", e1); } } } public void setConfig(Map config) { this.configMap = (Map) config; } public RedissonClient getRedisson() { return redisson; } public void setRedisson(RedissonClient redisson) { this.redisson = redisson; } public Codec getCodec() { return codec; } public void setCodec(Codec codec) { this.codec = codec; } public String getConfigLocation() { return configLocation; } public void setConfigLocation(String configLocation) { this.configLocation = configLocation; } public boolean isAllowNullValues() { return allowNullValues; } public void setAllowNullValues(boolean allowNullValues) { this.allowNullValues = allowNullValues; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy