<bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO"> <property name="cacheManager" ref="shiroCacheManager" /> </bean>
<bean id="shiroCacheManager" class="net.cache.redis.RedisCacheManager" />
想跟換Shiro中緩存系統,試了很多方法,一直報錯 org.apache.shiro.session.UnknownSessionException: There is no session with
要實現自己的Redis緩存,還是使用自帶的EnterpriseCacheSessionDAO,只要把它需要的 cacheManager 換成自己的redis cache 實現就可以了。測試啟動后沒有再出現 no session 問題了
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import javax.annotation.Resource; import org.apache.shiro.cache.Cache; import org.apache.shiro.cache.CacheException; import org.apache.shiro.cache.CacheManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class RedisCacheManager implements CacheManager{ private static final Logger logger = LoggerFactory.getLogger(RedisCacheManager.class); // fast lookup by name map private final ConcurrentMap<String, Cache> caches = new ConcurrentHashMap<String, Cache>(); @Resource private RedisDao redisDao; public <K, V> Cache<K, V> getCache(String name) throws CacheException { logger.debug("獲取名稱為: " + name + " 的RedisCache實例"); Cache<K, V> c = caches.get(name); if (c == null) { c = new RedisCache<K, V>(redisDao); caches.put(name, c); } return c; } }
