zoukankan      html  css  js  c++  java
  • Shiro缓存管理

    Shiro缓存管理
    • CacheManager、Cache
    • Redis实现CacheManager
    spring.xml
    <!-- 创建SecurityManager对象-->
    <bean class="org.apache.shiro.web.mgt.DefaultWebSecurityManager" id="securityManager">
        <property name="realm" ref="realm" />
        <property name="sessionManager" ref="sessionManager" />
        <property name="cacheManager" ref="cacheManager" />
    </bean>
    <bean class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager" id="sessionManager">
        <property name="sessionDAO" ref="redisSessionDao" />
    </bean>
    <bean class="com.imooc.session.RedisSessionDao" id="redisSessionDao" />
    <bean class="com.imooc.cache.RedisCacheManager" id="cacheManager" />
    
    RedisCache
    public class RedisCache<K, V> implements Cache<K, V> {
        @Resource
        private JedisUtil jedisUtil;
    
        private final String CACHE_PREFIX = "imooc-cache";
    
        private byte[] getKey(K k) {
            if (k instanceof String) {
                return (CACHE_PREFIX + k).getBytes();
            }
            return SerializationUtils.serialize(k);
        }
    
        @Override
        public V get(K k) throws CacheException {
            byte[] value= jedisUtil.get(getKey(k));
            if (value != null) {
                return (V) SerializationUtils.deserialize(value);
            }
            return null;
        }
    
        @Override
        public V put(K k, V v) throws CacheException {
            byte[] key = getKey(k);
            byte[] value = SerializationUtils.serialize(v);
            jedisUtil.set(key, value);
            jedisUtil.expire(key, 600);
            return v;
        }
    
        @Override
        public V remove(K k) throws CacheException {
            byte[] key = getKey(k);
            byte[] value = jedisUtil.get(key);
            jedisUtil.del(key);
            if (value != null) {
                return (V) SerializationUtils.deserialize(value);
            }
            return null;
        }
    
        @Override
        public void clear() throws CacheException {
            //
        }
    
        @Override
        public int size() {
            return 0;
        }
    
        @Override
        public Set<K> keys() {
            return null;
        }
    
        @Override
        public Collection<V> values() {
            return null;
        }
    }
    
    
    RedisCacheManager
    public class RedisCacheManager implements CacheManager {
    
        @Resource
        private RedisCache redisCache;
    
        @Override
        public <K, V> Cache<K, V> getCache(String s) throws CacheException {
            return redisCache;
        }
    }
    
  • 相关阅读:
    中值定理
    poj 3984 迷宫问题 bfs
    android 处理网络状态——无网,2g,3g,wifi,ethernet,other
    POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)
    Android硬件加速
    Android的横竖屏切换
    滑雪_poj_1088(记忆化搜索).java
    Rank of Tetris(hdu1811拓扑排序+并查集)
    git在windows下clone、pull或者push内存溢出的解决办法
    数据库中DDL、DML、DCL和TCP概念
  • 原文地址:https://www.cnblogs.com/sanjun/p/10007948.html
Copyright © 2011-2022 走看看