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;
        }
    }
    
  • 相关阅读:
    linux下FFmpeg编译生成ffplay
    linux下ffmpeg安装
    linux之x86裁剪移植---字符界面sdl开发入门
    Linux忘记开机密码怎么办?
    linux命令--ldconfig和ldd用法
    linux命令之 ifconfig
    Linuxshell脚本之if条件判断
    redis之django-redis
    深刻理解Python中的元类(metaclass)
    【Django错误】OSError: raw write() returned invalid length 14 (should have been between 0 and 7)
  • 原文地址:https://www.cnblogs.com/sanjun/p/10007948.html
Copyright © 2011-2022 走看看