shiro默认对ehcache缓存技术的支持,因此不使用redis缓冲技术
---------------------------------------------------------------------------------------
ehcache配置步骤:
1、导包:ehcache的开发包 ehcache-core
spring整合ehcache的包在spring-context-support里面
2、解压ehcache-core包,将里面的 ehcache-failsafe.xml复制并改名为ehcache.xml
自定义缓存区,cache标签后添加name属性
<!-- 自定义缓存 -->
<!-- 复制一份,修改成cache标签并添加name属性 -->
<cache name="bos"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
3、配置spring整合ehcache
ehcacheManager交由spring管理,即创建ehcacheManagerFactoryBean
<!-- ehcache缓存 -->
<bean id="ehCacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"></property>
</bean>
----------------------------------------------------------------------------------------
shiro整合ehcache的配置步骤:
1、配置shiro封装缓存管理器
<!-- 配置shiro整合缓存管理器 -->
<bean id="shiroEhCahceManager"
class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManager" ref="ehCacheManager"></property>
</bean>
2、将shiro缓存管理器注入安全管理器
<!-- 安全管理器 -->
<bean id="securityManager"
class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 自定义的realm -->
<property name="realm" ref="bosRealm"></property>
<property name="cacheManager" ref="shiroEhCahceManager"></property>
</bean>
3、对认证数据、授权数据 自定义的realm进行缓存配置
<!-- 配置bosRealm -->
<bean id="bosRealm" class="cn.itcast.bos.realm.BosRealm">
<!-- 对其进行缓存配置 -->
<!-- bos为缓存cahce.xml中自定义缓存配置的name属性 -->
<property name="authenticationCacheName" value="bos"></property>
</bean>
--------------------------------------------------------------------------------------
注意: 使需要缓存对象,实现 Serializable 接口
java.io.NotSerializableException: cn.itcast.bos.domain.system.User
--------------------------------------------------------------------------------------
ehcache对普通业务进行缓存
1、spring 封装cache缓存管理器
<!-- spring封装cache缓存管理器 -->
<bean id="springCacheManager"
class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehCacheManager"></property>
</bean>
2、在自定义的applicationContext-cache.xml中引入cache名称空间
3、激活spring的缓存注解
<!-- spring激活缓存注解 -->
<cache:annotation-driven cache-manager="springCacheManager"/>
4、可以在自定义缓存对象类的方法上使用缓存注解
@Cacheable 应用缓存区,对方法返回结果进行缓存 ---- 用于查询方法
@CacheEvict 清除缓存区数据 --- 用于 增加、修改、删除 方法
针对数据在不同条件下进行不同缓存,设置@Cacheable 注解 key 属性
---------------------------------------------------------------------------------------
ehcache配置步骤:
1、导包:ehcache的开发包 ehcache-core
spring整合ehcache的包在spring-context-support里面
2、解压ehcache-core包,将里面的 ehcache-failsafe.xml复制并改名为ehcache.xml
自定义缓存区,cache标签后添加name属性
<!-- 自定义缓存 -->
<!-- 复制一份,修改成cache标签并添加name属性 -->
<cache name="bos"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
3、配置spring整合ehcache
ehcacheManager交由spring管理,即创建ehcacheManagerFactoryBean
<!-- ehcache缓存 -->
<bean id="ehCacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"></property>
</bean>
----------------------------------------------------------------------------------------
shiro整合ehcache的配置步骤:
1、配置shiro封装缓存管理器
<!-- 配置shiro整合缓存管理器 -->
<bean id="shiroEhCahceManager"
class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManager" ref="ehCacheManager"></property>
</bean>
2、将shiro缓存管理器注入安全管理器
<!-- 安全管理器 -->
<bean id="securityManager"
class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 自定义的realm -->
<property name="realm" ref="bosRealm"></property>
<property name="cacheManager" ref="shiroEhCahceManager"></property>
</bean>
3、对认证数据、授权数据 自定义的realm进行缓存配置
<!-- 配置bosRealm -->
<bean id="bosRealm" class="cn.itcast.bos.realm.BosRealm">
<!-- 对其进行缓存配置 -->
<!-- bos为缓存cahce.xml中自定义缓存配置的name属性 -->
<property name="authenticationCacheName" value="bos"></property>
</bean>
--------------------------------------------------------------------------------------
注意: 使需要缓存对象,实现 Serializable 接口
java.io.NotSerializableException: cn.itcast.bos.domain.system.User
--------------------------------------------------------------------------------------
ehcache对普通业务进行缓存
1、spring 封装cache缓存管理器
<!-- spring封装cache缓存管理器 -->
<bean id="springCacheManager"
class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehCacheManager"></property>
</bean>
2、在自定义的applicationContext-cache.xml中引入cache名称空间
3、激活spring的缓存注解
<!-- spring激活缓存注解 -->
<cache:annotation-driven cache-manager="springCacheManager"/>
4、可以在自定义缓存对象类的方法上使用缓存注解
@Cacheable 应用缓存区,对方法返回结果进行缓存 ---- 用于查询方法
@CacheEvict 清除缓存区数据 --- 用于 增加、修改、删除 方法
针对数据在不同条件下进行不同缓存,设置@Cacheable 注解 key 属性