当前位置:网站首页>Shiro之缓存管理
Shiro之缓存管理
2022-04-22 14:06:00 【xiaoweiwei99】
缓存管理
为什么要使用缓存
在没有使用缓存的情况下,每发送一次请求都会调用一次doGetAuthorizationInfo方法来进行用户的授权操作,但是我们知道,一个用户具有的权限一般不会频繁的修改,也就是每次授权的内容都是一样的,所以我们希望在用户登录成功的第一次授权成功后将用户的权限保存在缓存中,下一次请求授权的话就直接从缓存中获取,这样效率会更高一些。
使用EhCache实现缓存
导入依赖
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.5.3</version>
</dependency>
添加EhCache的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<!--diskStore:缓存数据持久化的目录 地址 -->
<diskStore path="C: oolsehcache" />
<!--
eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
maxElementsInMemory:缓存中允许创建的最大对象数
overflowToDisk:内存不足时,是否启用磁盘缓存。
timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前, 两次访问时间的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是 0 就意味着元素可以停顿无穷长的时间。
timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。
diskPersistent:设定在虚拟机重启时是否进行磁盘存储,默认为false
diskExpiryThreadIntervalSeconds: 属性可以设置该线程执行的间隔时间(默认是120秒,不能太小
1 FIFO,先进先出
2 LFU,最少被使用,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。
3 LRU,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
-->
<defaultCache
maxElementsInMemory="1000"
maxElementsOnDisk="10000000"
eternal="false"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
</ehcache>
在shiro的配置文件中配置缓存
<!-- 配置缓存 -->
<bean class="org.apache.shiro.cache.ehcache.EhCacheManager" id="cacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"></property>
</bean>
在自定义的Realm类中添加清除缓存的方法
/**
* 清除缓存
*/
public void clearCache() {
PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
super.clearCache(principals);
}
在Controller中添加清除缓存的业务
@Autowired
private MyRealm myRealm;
@RequestMapping("/clear")
@ResponseBody
public void clearCache() {
myRealm.clearCache();
}
总结
第一次正常请求会授权,之后访问从缓存中获取。当调用清空缓存方法后,再次请求的时候因为缓存已经空了,所以会再次授权,
场景: 权限修改生效后,立即刷新清空缓存,则可以实现用户不退出生效新的权限
版权声明
本文为[xiaoweiwei99]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_46416934/article/details/124343466
边栏推荐
- BCC-stackcount
- Super user of oceanbase - system tenant sys
- Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
- Customization of AoYa Dao community pledge mining DAPP system
- cocoyaxi库 channel实现详解
- 分析电脑使用变慢的七大原因
- Is it effective to control caching through meta information in HTML files? Is it used much at present?
- 如何使用OpenFeign实现对于第三方接口的调用
- 【Zeekr_Tech】ROS/ROS 2介绍
- 2022 examination questions and simulation examination of operation certificate for safety management personnel of hazardous chemical business units
猜你喜欢

idea中pom.xml文件里mysql5.7坐标报红出错

2022化工自动化控制仪表考试练习题模拟考试平台操作
![Genesis creative comics [stable pass]](/img/14/30e0288119d77f37e5d44af857b149.jpg)
Genesis creative comics [stable pass]

Ebpf learning - getting started

2D转换(移动:translate,旋转:rotate,缩放:scale,2D转换综合写法)

Redis connection tool cannot connect to redis in docker

Why should the bank go to the fortress machine? Which one to choose? Are there any cases?

uniapp运行到微信开发者工具中小程序端页面空白的解决办法

2022危险化学品经营单位安全管理人员操作证考试题及模拟考试

Go down and continue to go down. Xiaohui's fund has lost 640000...
随机推荐
What is adapter mode?
【论文笔记】Vision Transformers for Dense Prediction
Redis connection tool cannot connect to redis in docker
Genesis public chain gives Dao new vitality
Redis 批量删除数据(通配符)
苏小红C语言程序设计第四、五章知识总结
idea中pom.xml文件里mysql5.7坐标报红出错
Is it safe to open an account in Zhujiang futures?
Genesis creative comics [stable pass]
华为云媒体査勇:华为云在视频AI转码领域的技术实践
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
那些年我们一起优化的SQL
Imeta: integrating macroomics to re understand life and Environmental Sciences
LeetCode——字符的最短距离
关于局域网特性的三个要素简述
Crater encountered in calling bash script by makefile
LeetCode-819 最常见的单词
日志脱敏是什么意思?为什么要做日志脱敏?
TensorFlow-ValueError: ‘images‘ contains no shape
Add pyuic to pychar's external tool