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进行身份验证和授权的:也就是所有操作都是自它开始的,这个对象是线程安全 ...
随机推荐
- 笔记:Jersey REST 传输格式
通常REST接口会以XML或JSON作为主要传输格式,同时 Jersey 也支持其他的数据格式,比如基本类型.文件.流等格式. 基本类型 Java的基本类型又叫原生类型,包括4种整数(byte.sho ...
- 总结的Javascript插件
1.很好用的弹窗 https://limonte.github.io/sweetalert2/ https://github.com/limonte/sweetalert2 import './unt ...
- java排序算法(七):折半插入排序
java排序算法(七):折半插入排序 折半插入排序法又称为二分插入排序法,是直接插入排序法的改良版本,也需要执行i-1趟插入.不同之处在于第i趟插入.先找出第i+1个元素应该插入的位置.假设前i个数据 ...
- [css 揭秘]:CSS揭秘 技巧(一):半透明边框
我的github地址:https://github.com/FannieGirl/ifannie/ 源码都在上面哦 喜欢的给我一个星吧 半透明边框 css 中的半透明颜色,比如用 rgba() 和 h ...
- sqlite语句主页
因为现在android手机用sqlite数据,但是sql语句很多和sqlserver不同..所以还是把官网记下以便开发:http://www.sqlite.org/lang.html
- hihocoder [Offer收割]编程练习赛52 D 部门聚会
看了题目的讨论才会做的 首先一点,算每条边(u, v)对于n*(n+1)/2种[l, r]组合的贡献 正着算不如反着算 哪些[l, r]的组合没有包含这条边(u, v)呢 这个很好算 只需要统计u这半 ...
- 使用 Except 和 Intersect
做了一个如下的小厕所,如果我需要得到返回是 d,f 那我需要用那组语句呢? A: ;WITH CA AS( SELECT * FROM (VALUES('a'),('b'),('c'),('d'))a ...
- C语言程序设计课程总结
第一次教授C语言程序设计课程,相比计算机组成原理.arm体系结构等偏向硬件的课程,C的教学方式要灵活一些.计算机组成原理课程偏向理论,哈尔滨工业大学的计算机组成原理是国家精品课,增加了mooc+spo ...
- 20162318 实验四 Android程序设计
北京电子科技学院(BESTI) 实 验 报 告 课程:程序设计与数据结构 班级:1623班 姓名:张泰毓 指导老师:娄老师.王老师 实验日期:2017年5月26日 实验密级:非密级 实验器材:带Lin ...
- 关于FPGA随笔
verilog与c