本文將構建一個普通工程來講明spring註解緩存的使用方式,關於如何在web應用中使用註解緩存,請參見:java
一.簡介
在spring的modules包中提供對許多第三方緩存方案的支持,包括:
EHCache
OSCache(OpenSymphony)
JCS
GigaSpaces
JBoss Cache
等等。
將這些第三方緩存方案配置在spring中很簡單,網上有許多介紹,這裏只重點介紹如何配置基於註解的緩存配置。
本文將經過例舉EHCache和OSCache詳細介紹如何使用spring配置基於註解的緩存配置,注意這裏的緩存是方法級的。
二.依賴
在開始介紹如何進行緩存配置前先介紹一下EHCache和OSCache的jar依賴。
EHCache:
ehcache-core-1.7.2.jar
jakarta-oro-2.0.8.jar
slf4j-api-1.5.8.jar
slf4j-jdk14-1.5.8.jar
OSCache:
oscache-2.4.1.jar
此外,二者都須要的jar以下:
cglib-nodep-2.1_3.jar
commons-logging.jar
log4j-1.2.15.jar
spring-modules-cache.jar
spring.jar
三.配置 web
兩種緩存在spring配置文件中均可以使用兩種配置方式,一種是spring2.0之前的徹底基於bean的複雜配置,一種是使用後來的基於命名空間的簡單配置,兩種配置效果相同,分別介紹以下:spring
EHCache:
1)普通配置api
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- aop代理,這個是必須地,不然緩存不起做用 --> <bean id="autoproxy" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /> <!-- EhCache 管理工廠 用於指定ehcache配置文件路徑 --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /> </bean> <bean id="cacheProviderFacade" class="org.springmodules.cache.provider.ehcache.EhCacheFacade"> <property name="cacheManager" ref="cacheManager" /> </bean> <!-- 1.5+ Annotation 基於註解查找被緩存的業務方法 --> <bean id="cachingAttributeSource" class="org.springmodules.cache.annotations.AnnotationCachingAttributeSource"></bean> <!-- 緩存攔截器:定義了緩存模塊,ehcache只須要指定配置文件中的緩存名稱 --> <bean id="cachingInterceptor" class="org.springmodules.cache.interceptor.caching.MetadataCachingInterceptor"> <property name="cacheProviderFacade" ref="cacheProviderFacade" /> <property name="cachingAttributeSource" ref="cachingAttributeSource" /> <property name="cachingModels"> <props> <prop key="testCaching">cacheName=testCache</prop> </props> </property> </bean> <!-- 基於註解查找緩存業務方法的AOP通知器 --> <bean id="cachingAttributeSourceAdvisor" class="org.springmodules.cache.interceptor.caching.CachingAttributeSourceAdvisor"> <constructor-arg ref="cachingInterceptor" /> </bean> <!-- 基於註解查找觸發緩存刷新動做的業務方法 --> <bean id="flushingAttributeSource" class="org.springmodules.cache.annotations.AnnotationFlushingAttributeSource"></bean> <!-- 刷新攔截器:定義了刷新策略,基於那個模塊ID,刷新相應的緩存 --> <bean id="flushingInterceptor" class="org.springmodules.cache.interceptor.flush.MetadataFlushingInterceptor"> <property name="cacheProviderFacade" ref="cacheProviderFacade" /> <property name="flushingAttributeSource" ref="flushingAttributeSource" /> <property name="flushingModels"> <map> <entry key="testFlushing"> <bean class="org.springmodules.cache.provider.ehcache.EhCacheFlushingModel"> <property name="cacheNames"> <list> <value>testCache</value> </list> </property> <!-- 報錯,應該是不能直接設置cacheName <property name="cacheName" value="testCache"/> --> </bean> </entry> </map> </property> </bean> <!-- 基於註解查找刷新緩存業務方法的AOP通知器 --> <bean id="flushingAttributeSourceAdvisor" class="org.springmodules.cache.interceptor.flush.FlushingAttributeSourceAdvisor"> <constructor-arg ref="flushingInterceptor" /> </bean> <!-- 測試對象 --> <bean id="testCache" class="com.TestCache"/> </beans>
2)命名空間配置緩存
<?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:ehcache="http://www.springmodules.org/schema/ehcache" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"> <!-- 這裏能夠不須要配置這個 <bean id="autoproxy" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /> --> <ehcache:config configLocation="classpath:ehcache.xml" id="cacheProvider" /> <ehcache:annotations providerId="cacheProvider"> <ehcache:caching cacheName="testCache" id="testCaching" /> <ehcache:flushing cacheNames="testCache" id="testFlushing" /> </ehcache:annotations> <bean id="testCache" class="com.TestCache"/> </beans>
OSCache:
1)普通配置 app
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 這個是必須地,不然緩存不起做用 --> <bean id="autoproxy" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /> <!-- 緩存管理工廠:使用OSCache緩存管理,配置了OSCache使用的配置文件路徑 --> <bean id="cacheManager" class="org.springmodules.cache.provider.oscache.OsCacheManagerFactoryBean"> <property name="configLocation" value="classpath:oscache.properties" /> </bean> <!-- 緩存提供:OSCache --> <bean id="cacheProviderFacade" class="org.springmodules.cache.provider.oscache.OsCacheFacade"> <property name="cacheManager" ref="cacheManager" /> </bean> <!-- 1.5+ Annotation 基於註解查找被緩存的業務方法 --> <bean id="cachingAttributeSource" class="org.springmodules.cache.annotations.AnnotationCachingAttributeSource"></bean> <!-- 緩存攔截器:定義了緩存模塊,以及相應的刷新策略,以及緩存所屬羣組 --> <bean id="cachingInterceptor" class="org.springmodules.cache.interceptor.caching.MetadataCachingInterceptor"> <property name="cacheProviderFacade" ref="cacheProviderFacade" /> <property name="cachingAttributeSource" ref="cachingAttributeSource" /> <property name="cachingModels"> <props> <prop key="testCaching">refreshPeriod=86400;cronExpression=0 1 * * *;groups=pb_test</prop> </props> </property> </bean> <!-- 基於註解查找緩存業務方法的AOP通知器 --> <bean id="cachingAttributeSourceAdvisor" class="org.springmodules.cache.interceptor.caching.CachingAttributeSourceAdvisor"> <constructor-arg ref="cachingInterceptor" /> </bean> <!-- 基於註解查找觸發緩存刷新動做的業務方法 --> <bean id="flushingAttributeSource" class="org.springmodules.cache.annotations.AnnotationFlushingAttributeSource"></bean> <!-- 刷新攔截器:定義了刷新策略,基於那個模塊ID,刷新相應的緩存羣組 --> <bean id="flushingInterceptor" class="org.springmodules.cache.interceptor.flush.MetadataFlushingInterceptor"> <property name="cacheProviderFacade" ref="cacheProviderFacade" /> <property name="flushingAttributeSource" ref="flushingAttributeSource" /> <property name="flushingModels"> <props> <prop key="testFlushing">groups=pb_test</prop> </props> </property> </bean> <!-- 基於註解查找刷新緩存業務方法的AOP通知器 --> <bean id="flushingAttributeSourceAdvisor" class="org.springmodules.cache.interceptor.flush.FlushingAttributeSourceAdvisor"> <constructor-arg ref="flushingInterceptor" /> </bean> <bean id="testCache" class="com.TestCache"/> </beans>
2)命名空間配置ide
<?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:oscache="http://www.springmodules.org/schema/oscache" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springmodules.org/schema/oscache http://www.springmodules.org/schema/cache/springmodules-oscache.xsd"> <!-- 這裏能夠不須要配置這個 <bean id="autoproxy" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /> --> <oscache:config configLocation="classpath:oscache.properties" id="cacheProvider"/> <oscache:annotations providerId="cacheProvider"> <oscache:caching id="testCaching" groups="pb_test" cronExpression="0 1 * * *" refreshPeriod="86400"/> <oscache:flushing id="testFlushing" groups="pb_test"/> </oscache:annotations> <bean id="testCache" class="com.TestCache"/> </beans>
四.註解函數
@Cacheable:聲明一個方法的返回值應該被緩存測試
例如:@Cacheable(modelId = "testCaching")
@CacheFlush:聲明一個方法是清空緩存的觸發器
例如:@CacheFlush(modelId = "testCaching")
五.測試
這是用使用一個帶有main函數的類來進行測試,代碼以下:
/* * COPYRIGHT Beijing NetQin-Tech Co.,Ltd. * **************************************************************************** * 源文件名: TestCache.java * 功能: (描述文件功能) * 版本: @version 1.0 * 編制日期: 2010-2-24 * 說明: (描述使用文件功能時的制約條件) * 修改歷史: (主要歷史變更緣由及說明) * YYYY-MM-DD | Author | Change Description * 2010-2-24 | hanqunfeng | Created */ package com; import net.sf.ehcache.CacheManager; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springmodules.cache.annotations.CacheFlush; import org.springmodules.cache.annotations.Cacheable; import com.opensymphony.oscache.general.GeneralCacheAdministrator; public class TestCache { /** * 描述 : <描述函數實現的功能>. <br> *<p> * @param args */ static String context = null; static ApplicationContext applicationContext; static{ context = "applicationContext-ehcache.xml";//ehcache簡單配置(命名空間) // context = "applicationContext-ehcache_full.xml";//ehcache完整配置 // context = "applicationContext-oscache.xml";//oscache簡單配置(命名空間) // context = "applicationContext-oscache_full.xml";//oscache完整配置 applicationContext = new ClassPathXmlApplicationContext(context); } public static void main(String[] args) { TestCache test = (TestCache)applicationContext.getBean("testCache"); System.out.println(test.getName(0)); System.out.println(test.getName(0)); System.out.println(test.getName(0)); test.flush(); // test.OSFlushAll(); // test.EHFlushAll(); System.out.println(test.getName(0)); System.out.println(test.getName(0)); System.out.println(test.getName(0)); } @Cacheable(modelId = "testCaching") public String getName(int i){ System.out.println("Processing testCaching"); return "nihao:"+i; } @CacheFlush(modelId = "testFlushing") public void flush(){ System.out.println("Processing testFlushing"); } /** * 描述 : <OSCache刷新所有緩存>. <br> *<p> 問題:flushAll() 後不會再緩存數據 */ public void OSFlushAll(){ GeneralCacheAdministrator cacheAdmin = (GeneralCacheAdministrator)applicationContext.getBean("cacheManager"); cacheAdmin.flushAll(); System.out.println("Processing OSFlushingAll"); } /** * 描述 : <清空指定組名稱的緩存>. <br> *<p> * @param groupName */ public void OSFlushGroup(String groupName){ GeneralCacheAdministrator cacheAdmin = (GeneralCacheAdministrator)applicationContext.getBean("cacheManager"); cacheAdmin.flushGroup(groupName);//清除該組的緩存:pb_test System.out.println("Processing OSFlushingGroup:"+groupName); } /** * 描述 : <EHCache刷新所有緩存>. <br> *<p> success */ public void EHFlushAll(){ CacheManager cacheAdmin = (CacheManager)applicationContext.getBean("cacheManager"); cacheAdmin.clearAll(); System.out.println("Processing EHFlushingAll"); } /** * 描述 : <清空指定名稱的緩存>. <br> *<p> * @param cacheName */ public void EHFlushCache(String cacheName){ CacheManager cacheAdmin = (CacheManager)applicationContext.getBean("cacheManager"); cacheAdmin.getCache(cacheName).flush();//清除單個緩存:testCache System.out.println("Processing EHFlushingCacheName:"+cacheName); } }
測試結果
Processing testCaching
nihao:0
nihao:0
nihao:0
Processing testFlushing
Processing testCaching
nihao:0
nihao:0
nihao:0
六.緩存配置文件
ehcache.xml