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;
} }
shiro中CacheManager相关的类结构介绍,提供redis Cache实现的更多相关文章
- hadoop2 YARN/Mv2中 ApplicationMaster相关问题及介绍
ApplicationMaster是什么? ApplicationMaster是一个框架特殊的库,对于Map-Reduce计算模型而言有它自己的ApplicationMaster实现,对于其他的想要运 ...
- Java中定时器相关实现的介绍与对比之:Timer和TimerTask
Timer和TimerTask JDK自带,具体的定时任务由TimerTask指定,定时任务的执行调度由Timer设定.Timer和TimerTask均在包java.util里实现. 本文基于java ...
- Shiro中的授权问题(二)
上篇博客(Shiro中的授权问题 )我们介绍了Shiro中最最基本的授权问题,以及常见的权限字符的匹配问题.但是这里边还有许多细节需要我们继续介绍,本节我们就来看看Shiro中授权的一些细节问题. 验 ...
- Azure Redis Cache作为ASP.NET 缓存输出提供程序
前一篇文章<Azure Redis Cache作为ASP.NET Session状态提供程序 >我们已经知道如何将ASP.NET应用程序Session存储在Redis Cache中,这里我 ...
- Azure Redis Cache作为ASP.NET Session状态提供程序
从上一篇博客<使用Azure Redis Cache>我们已经可以创建并使用Redis Cache为我们服务了. 作为Web开发者,我们都知道Session状态默认是保存在内存中的,它的优 ...
- 从零到实现Shiro中Authorization和Authentication的缓存
本文大纲 一.简介 二.缓存的概念 三.自定义实现缓存机制 四.什么是Ehcache 五.Ehcache怎么用 六.Spring对缓存的支持 七.Spring+Ehcache实现 八.Spring+S ...
- (转)shiro权限框架详解03-shiro介绍
http://blog.csdn.net/facekbook/article/details/54893740 shiro介绍 本文正式进入主题.本文将介绍如下内容: 什么是shiro 为什么需要学习 ...
- Shiro中Realm
6.1 Realm [2.5 Realm]及[3.5 Authorizer]部分都已经详细介绍过Realm了,接下来再来看一下一般真实环境下的Realm如何实现. 1.定义实体及关系 即用户-角色 ...
- shiro中INI配置
4.1 根对象SecurityManager 从之前的Shiro架构图可以看出,Shiro是从根对象SecurityManager进行身份验证和授权的:也就是所有操作都是自它开始的,这个对象是线程安全 ...
随机推荐
- 简单的nodejs 文件系统(fs)读写例子。
在nodejs中,可以通过fs(file system)模块进行文件的I/O操作. API链接地址: http://nodeapi.ucdok.com/#/api/fs.html 下面进行fs文件系统 ...
- RxJS速成 (上)
What is RxJS? RxJS是ReactiveX编程理念的JavaScript版本.ReactiveX是一种针对异步数据流的编程.简单来说,它将一切数据,包括HTTP请求,DOM事件或者普通数 ...
- 不可思议的纯CSS导航栏下划线跟随效果
先上张图,如何使用纯 CSS 制作如下效果? 在继续阅读下文之前,你可以先缓一缓.尝试思考一下上面的效果或者动手尝试一下,不借助 JS ,能否巧妙的实现上述效果. OK,继续.这个效果是我在业务开发的 ...
- jquery easyui+spring mnv 样式引不进,需要加载静态资源
前台用的EasyUI,页面中引入了easyUI的js与css(引入路径正确),但是无论如何都显示不出来, 如下图: EasyUI的样式没有,看控制台:警告,找不到 21:26:25,643 WARN ...
- [luogu2831][noip d2t3]愤怒的小鸟_状压dp
愤怒的小鸟 noip-d2t3 luogu-2831 题目大意:给你n个点,问最少需要多少条经过原点的抛物线将其覆盖. 注释:1<=点数<=18,1<=数据组数<=30.且规定 ...
- 替换Java字符串中的“& lt;”为“<”
发布webservice时 Java中的String类型会将 “<” 自动转换为 “<”,在建String转换为XML时就会出错,具体做法是: String strXml = “< ...
- 多线程 Synchronized关键字和Lock
Synchronized 分为实例锁和全局锁. 实例锁为 synchronized(this) 和 非static synchronized方法. 也加对象锁. 只要一个线程访问这类的一个syn ...
- Java注解(1)-注解基础
注解(Annotation)是在JAVA5中开始引入的,它为在代码中添加信息提供了一种新的方式.注解在一定程度上把元数据与源代码文件结合在一起,正如许多成熟的框架(Spring)所做的那样.那么,注解 ...
- phpcms v9 搜索结果列表页时间显示1970问题解决方案
对于喜欢用phpcms v9 的小伙伴来说,在调用时间时,总会出现时间1970这样的问题,对于这个问题,网上的说法很多,内容页时间显示通常不会问题,搜索结果页就不行了,通过总结,发现使用{format ...
- [COGS 2583]南极科考旅行
2583. 南极科考旅行 ★★ 输入文件:BitonicTour.in 输出文件:BitonicTour.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 小美要 ...