1. 授权数据缓存优化
每次需要shiro做权限控制, Realm的授权方法就会被调用, 查询数据库重新完成授权!
问题: 性能开销比较大
解决: 对用户授权,只进行一次 查询,查询后,将用户授权信息放入缓存中,以后需要授权时,直接从缓存中获取数据,而无需查询数据表
使用Spring框架 集成 EhCache
步骤一: 在项目中导入 Ehcache的jar 包
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.6</version>
</dependency>
将下发maven/net 复制 repository 下
步骤二: 使用Ehcache 在classpath 下,创建缓存框架配置文件 (ehcache.xml )
将导入ehcache的jar 中 ehcache-failsafe.xml 改名 ehcache.xml 复制 src/main/resources
<diskStore path="java.io.tmpdir"/>
<!-- 默认缓存配置策略 -->
<!-- maxElementsInMemory 内存中允许存放对象最大数量 -->
<!-- eternal 缓存数据是否是永久的 -->
<!-- maxElementsOnDisk 硬盘中缓存最大对象数量 -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
步骤三: 对Spring 进行 Ehcache 整合
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
通过配置注解 使用cache
<cache:annotation-driven cache-manager="ehCacheManager"/>
<!-- 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>
步骤四: 通过Spring 提供缓存注解,对数据进行缓存
@Cacheable和@CacheEvict来对缓存进行操作
@Cacheable:负责将方法的返回值加入到缓存中
@CacheEvict:负责清除缓存
@Cacheable("staff")// 对pageQuery 方法使用缓存
public Object pageQuery(int page, int rows) {
@CacheEvict(value = "staff", allEntries = true)// 清除缓存
public String save(Staff staff) {
通常在查询数据时,进行缓存, 在增加、修改、删除数据时,清除缓存 !
注意: ehcache在初始化时,至少要配置缓存区域, 而且配置区域名称需要和 @Cacheable 注解中指定区域名称一致!!!
<!-- name 是 缓存区域的名称 -->
<cache name="staff"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
></cache>
第五步: 整合shiro 缓存管理器
配置spring-shiro.xml
<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">
<!--设置自定义realm -->
<property name="realm" ref="monitorRealm" />
<!-- 将缓存管理器,交给安全管理器 -->
<property name="cacheManager" ref="cacheManager"></property>
</bean>
<!--自定义Realm 继承自AuthorizingRealm -->
<bean id="monitorRealm" class="cn.itcast.bos.shiro.MonitorRealm">
<!-- 配置授权 信息 缓存区名称 -->
<property name="authorizationCacheName" value="authorizationCacheName"></property>
</bean>
相当于把EhCache缓存管理器,将shiro
(shiro 在执行 doGetAuthorizationInfo 授权时,会将授权返回 SimpleAuthorizationInfo 信息,放入Ehcache缓存 ,缓存区名称 ?? )
配置ehcache.xml
<cache name="authorizationCacheName"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
></cache>
Shiro对于登陆用户只授权一次,使用subject.logout方法, 对授权缓存区evict操作!