首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【jfinal修仙系列】扩展ShiroCacheManager 支持Redis缓存

【jfinal修仙系列】扩展ShiroCacheManager 支持Redis缓存

作者头像
冷冷
发布2018-02-08 12:25:44
发布2018-02-08 12:25:44
9030
举报
文章被收录于专栏:冷冷冷冷

shiro 内置CacheManager

  • MemoryConstrainedCacheManager (适用于单JVM生产环境的实现)
代码语言:javascript
复制
shiroCacheManager = org.apache.shiro.cache.MemoryConstrainedCacheManager
securityManager.cacheManager = $shiroCacheManager
  • EhCacheManager (依赖ehcache)
代码语言:javascript
复制
shiroCacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
shiroCacheManager.cacheManagerConfigFile = classpath:ehcache-shiro.xml
securityManager.cacheManager = $shiroCacheManager

自定义RedisCacheManager 支持Redis缓存

代码语言:javascript
复制
public class RedisCacheManager implements CacheManager {
    private final ConcurrentMap<String, Cache> caches = new ConcurrentHashMap<String, Cache>();
    /*永不过期*/
    private int expire = 0;

    public int getExpire() {
        return expire;
    }

    public void setExpire(int expire) {
        this.expire = expire;
    }

    public <K, V> Cache<K, V> getCache(String name) throws CacheException {
        Cache c = caches.get(name);
        if (c == null) {
            c = new RedisCache<K, V>(expire);
            caches.put(name, c);
        }
        return c;
    }
}

RedisCache实现

代码语言:javascript
复制
public class RedisCache<K, V> implements org.apache.shiro.cache.Cache<K, V> {
    private static String SHIRO_KEY = "shiro";
    private int expire = 0;

    public RedisCache(int expire) {
        this.expire = expire;
    }

    public V get(K key) throws CacheException {
        LogKit.debug("根据key从Redis中获取对象 key [" + key + "]");
        if (key == null) {
            return null;
        } else {
            return Redis.use().hget(SHIRO_KEY, key);
        }
    }

    public V put(K key, V value) throws CacheException {
        LogKit.debug("根据key从存储 key [" + key + "]");
        Redis.use().hset(SHIRO_KEY, key, value);
        Redis.use().expire(SHIRO_KEY, expire);
        return value;
    }

    /**
     * shiro 的默认删除KEY:用户的
     *
     * @throws CacheException CacheException
     */
    public V remove(K key) throws CacheException {
        LogKit.debug("从redis中删除 key [" + key + "]");
        V previous = get(key);
        return previous;
    }

    public void clear() throws CacheException {
        LogKit.debug("从redis中删除所有元素");
        try {
            Redis.use().del(SHIRO_KEY);
        } catch (Throwable t) {
            throw new CacheException(t);
        }
    }

    public int size() {
        Long length = Redis.use().hlen(SHIRO_KEY);
        return length.intValue();
    }

    public Set<K> keys() {
        return (Set<K>) Redis.use().hkeys(SHIRO_KEY);
    }

    public Collection<V> values() {
        return Redis.use().hvals(SHIRO_KEY);
    }
}

使用方法

在ini文件中配置

代码语言:javascript
复制
shiroCacheManager = io.github.wx.common.shiro.cache.RedisCacheManager
shiroCacheManager.expire = 1800
securityManager.cacheManager = $shiroCacheManager
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • shiro 内置CacheManager
  • 自定义RedisCacheManager 支持Redis缓存
  • RedisCache实现
  • 使用方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档