Shiroのキャッシュをredis管理に渡す


Shiroのキャッシュをredis管理に渡す
ラベル(スペース区切り):Shiro redis
前言:
          redis  

一、Shiroとredisをインポートするjar
    jedis-2.7.3.jar
    shiro-core-1.2.3.jar
    shiro-ehcache-1.2.3.jar
    shiro-spring-1.2.3.jar
    shiro-web-1.2.3.jar
    slf4j-api-1.7.5.jar
    slf4j-log4j12-1.7.5.jar

二、具体的な実現
  • JedisUtilsツールクラス
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    public class JedisUtils {
        private static JedisPool jedisPool;
        static {
            JedisPoolConfig jedisConfig = new JedisPoolConfig();
            jedisConfig.setMaxTotal(100);
            jedisConfig.setMaxIdle(10);
            jedisConfig.setMaxWaitMillis(100);
            //        ,  redis        
            jedisPool = new JedisPool(jedisConfig, "192.168.0.118", 6379);
        }
    
        public static Jedis getJedis() {
            return jedisPool.getResource();
        }
    
        public static void close(Jedis jedis) {
            jedis.close();
        }
    }
  • Shiroのキャッシュをredisに渡すにはShiroのCacheインタフェース
    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    
    import org.apache.commons.lang3.SerializationUtils;
    import org.apache.shiro.cache.Cache;
    import org.apache.shiro.cache.CacheException;
    import redis.clients.jedis.Jedis;
    
    import com.baizhi.util.JedisUtils;
    
    /**
     *    redis cache
     * @author MOTUI
     *
     */
    public class ShiroRedisCache implements Cache{
        // shiro cache  key = value 
        // redis key = value
    
        public Object get(Object key) throws CacheException {
    
            byte[] bs = SerializationUtils.serialize((Serializable)key);
    
            byte[] value = JedisUtils.getJedis().get(bs);
    
            if(value == null){
                return null;
            }
            return SerializationUtils.deserialize(value);
        }
    
        /**
         *  shiro      redis 
         */
        public Object put(Object key, Object value) throws CacheException {
            Jedis jedis = JedisUtils.getJedis();
            //             
            jedis.set(SerializationUtils.serialize((Serializable)key), SerializationUtils.serialize((Serializable)value));
    
            byte[] bs = jedis.get(SerializationUtils.serialize((Serializable)key));
    
            Object object = SerializationUtils.deserialize(bs);
    
            return object;
        }
    
        public Object remove(Object key) throws CacheException {
    
            Jedis jedis = JedisUtils.getJedis();
    
            byte[] bs = jedis.get(SerializationUtils.serialize((Serializable)key));
    
            jedis.del(SerializationUtils.serialize((Serializable)key));
    
            return SerializationUtils.deserialize(bs);
        }
        /**
         *       
         */
        public void clear() throws CacheException {
            JedisUtils.getJedis().flushDB();
        }
    
        /**
         *      
         */
        public int size() {
    
            Long size = JedisUtils.getJedis().dbSize();
    
            return size.intValue();
        }
    
        /**
         *      key
         */
        public Set keys() {
    
            Set<byte[]> keys = JedisUtils.getJedis().keys(new String("*").getBytes());
    
            Set set = new HashSet();
    
            for (byte[] bs : keys) {
                set.add(SerializationUtils.deserialize(bs));
            }
            return set;
        }
    
        /**
         *      value
         */
        public Collection values() {
    
            Set keys = this.keys();
    
            List values = new ArrayList();
    
            for (Object object : keys) {
                byte[] bs = JedisUtils.getJedis().get(SerializationUtils.serialize((Serializable)object));
                values.add(SerializationUtils.deserialize(bs));
            }
            return values;
        }
    }
    を実現する必要がある
  • .
  • カスタムCustomCustomCacheManager実装CacheManager
    import org.apache.shiro.cache.Cache;
    import org.apache.shiro.cache.CacheException;
    import org.apache.shiro.cache.CacheManager;
    
    public class CustomCacheManager implements CacheManager{
    
        public  Cache getCache(String name) throws CacheException {
    
            return new ShiroRedisCache();
        }
    }
  • は、Shiroの構成において配置する、shiro.xml
    
                         
    
    
    
        
        
        
        
        
        
        
        
            
                /** = authc         
            
        
    
    
    
    
        
        
        
        
    
    
    
    
        
        
        
        【           ,    ??】
        
        
        
    
    
    
        
        
    
    
    
    
        
        
         
    
    
    
    【    CustomCacheManager】
    
    
    
  • まとめ
     Shiro     redis        。