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进行身份验证和授权的:也就是所有操作都是自它开始的,这个对象是线程安全 ...
随机推荐
- Ubuntu Mininet环境搭建
我们通过源码方式搭建mininet仿真平台,使用git下载mininet源码 git clone git://github.com/mininet/mininet 下载完成之后,使用下面命令选择安装版 ...
- parted分区和挂载及非交互式操作
author : headsen chen date : 2017-11-17 09:45:36 个人原创,转载请注明作者,出处,否则依法追究法律责任 1,将磁盘上原有的分区删除掉: 进入:#pa ...
- openstack安装系列问题:window7 64位安装的virtualBox 只能选择32位的系统安装不能选择64位的系统安装
个人原创,转载请注明作者,出处,否则依法追究法律责任 2017-10-03-12:22:22 现象:window7 64位安装的virtualBox 只能选择32位的系统安装不能选择64位的系统安装 ...
- 剑指Offer-平衡二叉树
package Tree; /** * 平衡二叉树 * 输入一棵二叉树,判断该二叉树是否是平衡二叉树. * 平衡二叉树(Balanced Binary Tree)又被称为AVL树(有别于AVL算法), ...
- eventProxyAPI(转)
EventProxy 仅仅是一个很轻量的工具,但是能够带来一种事件式编程的思维变化.有几个特点: 利用事件机制解耦复杂业务逻辑 移除被广为诟病的深度callback嵌套问题 将串行等待变成并行等待,提 ...
- vuex2中使用mapMutations/mapActions报错解决方法 BabelLoaderError: SyntaxError: Unexpected token
在尝鲜vuex2时,发现vuex2增加了 mapGetters 和 mapActions 的方法,借助stage2的 Object Rest Operator 特性,可以写出下面代码:methods: ...
- 走进webpack(1)--环境拆分及模块化
初级的文章和demo已经基本完成了,代码也已经上传到了我的github上,如果你对webpack的使用并不是十分了解,那么建议你回头看下走近系列,里面包括了当前项目中使用频繁的插件,loader的讲解 ...
- ELK+filebeat、kafka、zookeeper搭建文档
系统:centos 6.5 JDK:1.8 Elasticsearch-6.0.0Logstash-6.0.0kibana-6.0.0zookeeper-3.5.3kafka_2.12-1.0.0fi ...
- 网络通信 --> CRC校验
CRC校验 一.什么是CRC校验 循环校验码(Jyclic Redundancy Check,简称CRC码): 是数据通信领域中最常用的一种差错校验码,其特征是信息字段和校验字段的长度可以任意 ...
- 解决C盘中的文件不能修改问题
在不能修改的文件右击属性>点击安全>编辑>点击用户>完全控制. 步骤如图: 最后点击确定.