shiro ehcache
来源:互联网 发布:协同过滤推荐算法原理 编辑:程序博客网 时间:2024/06/05 20:50
项目部署shiro后发现任何操作都会导致重新查一遍数据库操作,本来以为只有登录才会触发,可是看到每次哪怕点击一个按钮 就查询一连串 权限表和角色表之后 发现必须要去优化了。
开始思路 因为所有操作都要通过 doGetAuthorizationInfo (授权)这个方法,然后把数据库查到的结果放到缓存 用户作为key 权限和角色所谓value。
后来发现有一个更为简便的方法 -> 对持久层做缓存 例如每次都要调用roleService.findAll();这个方法 那么就可以在roleService的实现方法上增加shiro标签@CacheAble("role")里边的role是在eheache配置文件中定义的。这样 就可以不去查数据库直接在缓存充取就行了,至于key和value就不是我们关心的了
好了 整体思路再过一遍
首先在pom文件中增加ehcache
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.6</version>
</dependency>
2 创建配置文件 ehcache.xml
<!-- 一个缓存区域, 用来存放取派员 -->
<!--name 是缓存区域的名称 -->
<cache name="staff"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
></cache>
3 把echache配置到spring 中去
<!-- spring对ehcache的缓存工厂支持 -->
<bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
<property name="shared" value="false"/>
</bean>
<!-- spring对ehcache的缓存管理 -->
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehCacheManagerFactory"></property>
</bean>
<!-- 使用注解配置缓存 -->
<cache:annotation-driven cache-manager="ehCacheManager"/>
4在service的实现类添加注解
@Cacheable("staff")// 对pageQuery 方法使用缓存
public Object pageQuery(int page,int rows) {
@CacheEvict(value = "staff", allEntries =true)// 清除缓存
public String save(Staffstaff) {
5 把ehcache和shiro结合
<!-- 配置shiro需要缓存管理器 -->
<!-- shiro对ehcache的缓存管理直接使用spring的缓存工厂 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManager" ref="ehCacheManagerFactory" />
</bean>
将缓存管理器,交给安全管理器
<!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 缓存管理器 -->
<property name="cacheManager"ref="cacheManager" />
<property name="realm"ref="bosRealm" />
</bean>
在Realm中指定使用的缓存区域的名称
<!-- 自定义Realm -->
<bean id="bosRealm" class="cn.itcast.bos.shiro.BosRealm">
<property name="userService"ref="userService" />
<property name="roleService"ref="roleService" />
<property name="functionService"ref="functionService" />
<!-- 指定使用缓存区域的名称 -->
<property name="authorizationCacheName" value="authorization"/>
</bean>
经过这么多配置 就可以把权限的数据放到缓存中去啦
有时候可能会报ehcache的错误
intellj idea告诉我是cache.xsd文件找不到 这个 从官网上下载一份到本地 在 头部添加
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"
- shiro ehcache
- shiro ehcache
- Apache Shiro 集成-Ehcache
- shiro ehcache配置
- Shiro EHCache缓存配置
- shiro-session-ehcache配置
- Shiro缓存整合EhCache
- shiro整合ehcache
- Shiro与EHCache冲突问题
- shiro.ehcache.xml配置参数
- Shiro + EHCache 缓存的使用
- Spring mvc+shiro+ehcache整合
- SpringBoot集成shiro和ehcache
- ehcache.xml及结合shiro缓存的ehcache的配置文件
- springmvc 整合shiro ehcache 项目源码
- springmvc SSM java redis shiro ehcache 整合
- Shiro初步5_加入ehcache缓存
- 使用shiro+ehcache进行缓存权限数据
- 使用对象池(ObjectPool)方式处理子弹的发射逻辑
- Qt 数据处理 — model
- android listview
- Python-新浪微博爬虫采集数据
- 插入排序伪代码与编程实现
- shiro ehcache
- 操作系统简介
- SDRAM理论篇之基础知识及操作时序讲解
- 盒子模型
- 面向对象——创建对象的方式
- 机器学习---kNN算法
- SQL表连接查询(inner join、full join、left join、right join)
- stock存储模板类指针
- Java难点解析(八)-接口