为了实现session共享 ,写了个ShiroRedisCache替代shiroCacheManager,项目运行变的非常慢,求大神支招
package com.jfinalshop.shiro.cache; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; import org.apache.shiro.cache.Cache; import org.apache.shiro.cache.CacheException; import com.jfinal.plugin.redis.Redis; public class ShiroCache<K, V> implements Cache<K, V>{ private static final String REDIS_SHIRO_CACHE = "weiyou-shiro-cache:"; private String cacheKey; public ShiroCache(String name) { this.cacheKey = REDIS_SHIRO_CACHE + name + ":"; } @Override public V get(K var1) throws CacheException { return Redis.use().get(getCacheKey(var1.toString())); } @Override public V put(K var1, V var2) throws CacheException { V v = Redis.use().get(getCacheKey(var1.toString())); Redis.use().set(getCacheKey(var1.toString()), var2); return v; } @Override public V remove(K var1) throws CacheException { V v = Redis.use().get(getCacheKey(var1.toString())); Redis.use().del(getCacheKey(var1.toString())); return v; } @Override public void clear() throws CacheException { Redis.use().del(keys()); } @Override public int size() { // TODO Auto-generated method stub return keys().size(); } @SuppressWarnings("unchecked") @Override public Set<K> keys() { Set<String> keys = Redis.use().keys(getCacheKey( "*")); Set<K> ks = new HashSet<>(); for (String key:keys) { ks.add((K) key); } return ks; } @Override public Collection<V> values() { Set<K> set = keys(); List<V> list = new ArrayList<>(); for (K s : set) { list.add(get(s)); } return list; } private String getCacheKey(String k) { return cacheKey + k; } }
package com.jfinalshop.shiro.cache; import org.apache.shiro.cache.Cache; import org.apache.shiro.cache.CacheException; import org.apache.shiro.cache.CacheManager; import com.jfinalshop.shiro.cache.ShiroCache; public class RedisCacheManager implements CacheManager{ @Override public <K, V> Cache<K, V> getCache(String var1) throws CacheException { return new ShiroCache<K, V>(var1); } }
shiroCacheManager = com.jfinalshop.shiro.cache.RedisCacheManager
项目:Shiro整合