爲何使用Ehcache而不使用redis緩存呢?
1.Shiro默認對ehcache的支持. 2.在後臺管理系統中ehcache使用廣泛.css
1.Spring整合Ehcachehtml
第一步:在 common_parent 導入 ehcache maven 座標, 默認已經導好包:Spring整合ehcache:Spring-context-support包java
<!-- 緩存 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
第二步:使用ehcache,導入ehcache.xml配置文件 : 解壓核心包,將ehcache-failsafe複製更名ehcache.xmlweb
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
<!-- 自定義緩存區-->
<cache name="bos" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
<cache name="standard" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>
第三步:配置Spring整合ehcache:將ehcacheManager交給Spring管理redis
:默認 Ehcache 提供 CacheManager 提供 addCache(),getCache(),加入緩存區或查詢緩存區方法,原理是EhCacheManagerFactoryBean裏構造Map<key,value>.spring
<!-- spring管理ehcache緩存管理器 -->
<bean id="ehcacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>
2 . 配置shiro整合ehcache完成對受權數據緩存 apache
第四步: 配置 shiro 封裝緩存管理器緩存
<!-- shiro整合ehcache 緩存管理器 -->
<bean id="shiroCacheManager"
class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManager" ref="ehcacheManager" />
</bean>
第五步: 將 shiro 的緩存管理器,注入到安全管理器中在app-shiro.xml加入
應用程序代碼 – Subject – SecurityManager – Realm (查詢認證和受權數據)安全
<!-- 安全管理器 -->
<bean id="securityManager"
class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="bosRealm" />
<property name="cacheManager" ref="shiroCacheManager" />
</bean>
第六步: 對認證數據、受權數據 哪些進行緩存 ? 用了緩存,實體類須要序列化app
<!-- 配置Realm -->
<bean id="bosRealm" class="cn.itcast.bos.realm.BosRealm">
<!-- 緩存區的名字 就是 ehcache.xml 自定義 cache的name -->
<property name="authorizationCacheName" value="bos" />
</bean>
3 . Ehcache 對普通業務數據進行緩存
一 .配置 spring 緩存管理器,封裝 ehcache 自帶 CacheManager
<!-- spring緩存管理器,整合ehcache -->
<bean id="springCacheManager"
class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcacheManager" />
</bean>
二. 在 applicationContext-cache.xml 引入 cache 名稱空間
三 .<!-- 激活spring 緩存註解 -->
<cache:annotation-driven cache-manager="springCacheManager"/>
四 在被 spring 管理 bean 對象方法上 使用@Cacheable 、@CacheEvict
@Cacheable 應用緩存區,對方法返回結果進行緩存 ---- 用於查詢方法
@CacheEvict 清除緩存區數據 --- 用於 增長、修改、刪除 方法
<cache name="standard" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
對實體類進行序列化,實現類配置@Cacheable
配置總結以下: 在applicationContext.xml引入applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!-- 掃描 @Server @Controller @Repository -->
<context:component-scan base-package="cn.itcast"/>
<!-- 加載properties文件 -->
<context:property-placeholder location="classpath:config.properties" />
<!-- 引入外部數據文件 -->
<import resource="applicationContext-dataSource.xml"/>
<!-- 引入WebService配置 -->
<import resource="applicationContext-webService.xml"/>
<!-- 引入quartz配置 -->
<import resource="applicationContext-quartz.xml"/>
<!-- 引入 mq配置 -->
<import resource="applicationContext-mq.xml"/>
<!-- 引入 elasticsearch 配置 -->
<import resource="applicationContext-elasticsearch.xml"/>
<!-- 引入 shiro權限控制配置 -->
<import resource="applicationContext-shiro.xml"/>
<!-- 引入cache 配置 -->
<import resource="applicationContext-cache.xml"/>
</beans>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
<!-- 自定義緩存區-->
<cache name="bos" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
<cache name="standard" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/cache
http://www.springframework.org/schema/cache/spring-cache.xsd ">
<!-- 緩存配置 -->
<bean id="ehCacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>
<!-- shiro封裝cacheManager -->
<bean id="shiroCacheManager"
class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManager" ref="ehCacheManager" />
</bean>
<!-- spring 封裝ehcache緩存管理器 -->
<bean id="springCacheManager"
class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehCacheManager" />
</bean>
<!-- 激活spring 緩存註解 -->
<cache:annotation-driven cache-manager="springCacheManager"/>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!-- 配置Shiro核心Filter -->
<bean id="shiroFilter"
class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 安全管理器 -->
<property name="securityManager" ref="securityManager" />
<!-- 未認證,跳轉到哪一個頁面 -->
<property name="loginUrl" value="/login.html" />
<!-- 登陸頁面頁面 -->
<property name="successUrl" value="/index.html" />
<!-- 認證後,沒有權限跳轉頁面 -->
<property name="unauthorizedUrl" value="/unauthorized.html" />
<!-- shiro URL控制過濾器規則 -->
<property name="filterChainDefinitions">
<value>
/login.html* = anon /user_login.action* = anon /validatecode.jsp* = anon /css/** = anon /js/** = anon /images/** = anon /services/** = anon /pages/base/courier.html* = perms[courier:list] /pages/base/area.html* = roles[base] /** = authc </value> </property> </bean> <!-- 安全管理器 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="bosRealm" /> <property name="cacheManager" ref="shiroCacheManager" /> </bean> <!-- 配置Realm --> <bean id="bosRealm" class="cn.itcast.bos.realm.BosRealm"> <!-- 緩存區的名字 就是 ehcache.xml 自定義 cache的name --> <property name="authorizationCacheName" value="bos" /> </bean> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <!-- 開啓shiro註解模式 --> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor" > <property name="proxyTargetClass" value="true" /> </bean> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager"/> </bean> </beans>
例如:
@Override @CacheEvict(value = "standard", allEntries = true) public void save(Standard standard) { standardRepository.save(standard); } @Override @Cacheable(value = "standard", key = "#pageable.pageNumber+'_'+#pageable.pageSize") public Page<Standard> findPageData(Pageable pageable) { return standardRepository.findAll(pageable); }
https://www.cnblogs.com/sdream/p/5966668.html