shiro中CacheManager相关的类结构介绍,提供redis Cache实现
cacheManager主要用于对shiro中的session、realm中的认证信息、授权信息进行缓存。
1.类结构
2.接口及类介绍
- CacheManager
提供根据名字获取cache的作用。
-
AbstractCacheManager
本地提供并发map做缓存。提供抽象类给子类继承,子类只需要创建cache即可。
-
MemoryConstrainedCacheManager
实现上面的抽象类。创建一个map作为缓存。
3.Cache相关介绍
- Cache接口
主要提供缓存相关的增删改查方法。
- MapCache
用map做缓存。通过构造器注入。
下面也提供我自己的redisCache实现。key是String类型的。需要自己提供spring redistemplate。
import lombok.Getter; import lombok.Setter; import org.apache.commons.collections.CollectionUtils; import org.apache.shiro.cache.Cache; import org.apache.shiro.cache.CacheException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import java.util.*; import java.util.concurrent.TimeUnit; /** * desc: * * @author: * creat_date: 2018/3/22 0022 * creat_time: 9:53 **/ @Getter @Setter public class ShiroRedisCache<V> implements Cache<String, V> { private Logger log = LoggerFactory.getLogger(getClass()); private RedisTemplate<String, V> redisTemplate; /** * 缓存的全局前缀 */ private String globalPrefix = "shiro_cache:"; /** * 真正的缓存前缀 = 全局前缀 + 缓存名 */ private String prefix; /** * 过期时间 */ private int expireTime; public ShiroRedisCache(RedisTemplate<String, V> redisTemplate, String prefix, int expireTime) { this.redisTemplate = redisTemplate; this.prefix = prefix; this.expireTime = expireTime; } @Override public V get(String key) throws CacheException { if (log.isDebugEnabled()) { log.debug("Key: {}", key); } if (key == null) { return null; } return redisTemplate.opsForValue().get(key); } @Override public V put(String key, V value) throws CacheException { if (log.isDebugEnabled()) { log.debug("Key: {}, value: {}", key, value); } if (key == null || value == null) { return null; } redisTemplate.opsForValue().set(key, value); redisTemplate.expire(key, expireTime, TimeUnit.MINUTES); return value; } @Override public V remove(String key) throws CacheException { if (log.isDebugEnabled()) { log.debug("Key: {}", key); } if (key == null) { return null; } ValueOperations<String, V> vo = redisTemplate.opsForValue(); V value = vo.get(key); redisTemplate.delete(key); return value; } @Override public void clear() throws CacheException { redisTemplate.delete(keys()); } @Override public int size() { int len = keys().size(); return len; } @SuppressWarnings("unchecked") @Override public Set<String> keys() { String key = prefix + "*"; Set<String> set = redisTemplate.keys(key); if (CollectionUtils.isEmpty(set)) { return Collections.emptySet(); } return set; } @Override public Collection<V> values() { Set<String> keys = keys(); List<V> values = new ArrayList<>(keys.size()); for (String key : keys) { values.add(redisTemplate.opsForValue().get(key)); } return values; } }
原文地址:https://www.cnblogs.com/grey-wolf/p/8630676.html
相关推荐
-
使用面向对象抽取领域名词 Java基础
2019-5-16
-
数组基础 Java基础
2019-10-3
-
深度解析Java中的5个“黑魔法” Java基础
2020-6-13
-
Java 8 的时间日期 API Java基础
2019-9-15
-
Arthas的高级操作 – Ognl表达式 Java基础
2019-10-3
-
微服务Spring Cloud Eureka 客户端-基本配置(eureka.client.xxx) Java基础
2020-5-30
-
一入前端深似海,从此红尘是路人系列第八弹之浅析Vue组件开发 Java基础
2020-5-29
-
【知识积累】JavaMail实现发邮件功能 Java基础
2019-3-19
-
【大学到研究生自学Java的学习路线】这是一份最适合普通大众、非科班的路线,帮你快速找到一份满意的工作 Java基础
2020-6-13
-
SpringMVC+Mybatis 如何配置多个数据源并切换? Java基础
2020-6-14