Java缓存学习之五:spring 对缓存的支持
? ? ?(注意標(biāo)題,Spring對(duì)緩存的支持 這里不單單指Ehcache )
從3.1開始,Spring引入了對(duì)Cache的支持。其使用方法和原理都類似于Spring對(duì)事務(wù)管理的支持。Spring Cache是作用在方法上的,其核心思想是這樣的:當(dāng)我們?cè)谡{(diào)用一個(gè)緩存方法時(shí)會(huì)把該方法參數(shù)和返回結(jié)果作為一個(gè)鍵值對(duì)存放在緩存中,等到下次利用同樣的參數(shù)來調(diào)用該方法時(shí)將不再執(zhí)行該方法,而是直接從緩存中獲取結(jié)果進(jìn)行返回。所以在使用Spring Cache的時(shí)候我們要保證我們緩存的方法對(duì)于相同的方法參數(shù)要有相同的返回結(jié)果。
???????使用Spring Cache需要我們做兩方面的事:
1、 配置Spring對(duì)Cache的支持(一種是基于一種是ConcurrentMap,另一種是Ehcache)
2、給方法添加注解來才能使用緩存
?
?
1 配置Spring對(duì)Cache的支持
1.1 聲明對(duì)Cache的支持
1.1.1??基于注解
???????配置Spring對(duì)基于注解的Cache的支持,
首先我們需要在Spring的配置文件中引入cache命名空間,
其次通過<cache:annotation-driven />就可以啟用Spring對(duì)基于注解的Cache的支持。
<?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-3.0.xsd
?????http://www.springframework.org/schema/cache
???? http://www.springframework.org/schema/cache/spring-cache.xsd">
?
???<cache:annotation-driven/>
?
</beans>
?
?????? <cache:annotation-driven/>屬性:
cache-manager屬性用來指定當(dāng)前所使用的CacheManager對(duì)應(yīng)的bean的名稱,默認(rèn)是cacheManager,所以當(dāng)我們的CacheManager的id為cacheManager時(shí)我們可以不指定該參數(shù),否則就需要我們指定了。
mode屬性,可選值有proxy和aspectj。默認(rèn)是使用proxy。當(dāng)mode為proxy時(shí),只有緩存方法在外部被調(diào)用的時(shí)候Spring Cache才會(huì)發(fā)生作用,這也就意味著如果一個(gè)緩存方法在其聲明對(duì)象內(nèi)部被調(diào)用時(shí)Spring Cache是不會(huì)發(fā)生作用的。而mode為aspectj時(shí)就不會(huì)有這種問題。另外使用proxy時(shí),只有public方法上的@Cacheable等標(biāo)注才會(huì)起作用,如果需要非public方法上的方法也可以使用Spring Cache時(shí)把mode設(shè)置為aspectj。
proxy-target-class屬性,表示是否要代理class,默認(rèn)為false。我們前面提到的@Cacheable、@cacheEvict等也可以標(biāo)注在接口上,這對(duì)于基于接口的代理來說是沒有什么問題的,但是需要注意的是當(dāng)我們?cè)O(shè)置proxy-target-class為true或者mode為aspectj時(shí),是直接基于class進(jìn)行操作的,定義在接口上的@Cacheable等Cache注解不會(huì)被識(shí)別到,那對(duì)應(yīng)的Spring Cache也不會(huì)起作用了。
需要注意的是<cache:annotation-driven/>只會(huì)去尋找定義在同一個(gè)ApplicationContext下的@Cacheable等緩存注解。
?
1.1.2??基于XML配置
???????除了使用注解來聲明對(duì)Cache的支持外,Spring還支持使用XML來聲明對(duì)Cache的支持。這主要是通過類似于aop:advice的cache:advice來進(jìn)行的。在cache命名空間下定義了一個(gè)cache:advice元素用來定義一個(gè)對(duì)于Cache的advice。其需要指定一個(gè)cache-manager屬性,默認(rèn)為cacheManager。cache:advice下面可以指定多個(gè)cache:caching元素,其有點(diǎn)類似于使用注解時(shí)的@Caching注解。cache:caching元素下又可以指定cache:cacheable、cache:cache-put和cache:cache-evict元素,它們類似于使用注解時(shí)的@Cacheable、@CachePut和@CacheEvict。下面來看一個(gè)示例:
???<cache:advice?id="cacheAdvice"?cache-manager="cacheManager">
??????<cache:caching?cache="users">
?????????<cache:cacheable?method="findById"?key="#p0"/>
?????????<cache:cacheable?method="find"?key="#user.id"/>
?????????<cache:cache-evict?method="deleteAll"?all-entries="true"/>
??????</cache:caching>
???</cache:advice>
?
???????上面配置定義了一個(gè)名為cacheAdvice的cache:advice,其中指定了將緩存findById方法和find方法到名為users的緩存中。這里的方法還可以使用通配符“*”,比如“find*”表示任何以“find”開始的方法。
???????有了cache:advice之后,我們還需要引入aop命名空間,然后通過aop:config指定定義好的cacheAdvice要應(yīng)用在哪些pointcut上。如:
???<aop:config?proxy-target-class="false">
??????<aop:advisor?advice-ref="cacheAdvice"?pointcut="execution(* com.xxx.UserService.*(..))"/>
???</aop:config>
???????上面的配置表示在調(diào)用com.xxx.UserService中任意公共方法時(shí)將使用cacheAdvice對(duì)應(yīng)的cache:advice來進(jìn)行Spring Cache處理。更多關(guān)于Spring Aop的內(nèi)容不在本文討論范疇內(nèi)。
?
1.2 ? 配置CacheManager
?????? CacheManager是Spring定義的一個(gè)用來管理Cache的接口。
Spring自身已經(jīng)為我們提供了兩種CacheManager的實(shí)現(xiàn),
一種是基于Java API的ConcurrentMap,
另一種是基于第三方Cache實(shí)現(xiàn)——Ehcache,
如果我們需要使用其它類型的緩存時(shí),我們可以自己來實(shí)現(xiàn)Spring的CacheManager接口或AbstractCacheManager抽象類。
1.2.1??基于ConcurrentMap的配置
???<bean?id="cacheManager"?class="org.springframework.cache.support.SimpleCacheManager">
??????<property?name="caches">
?????????<set>
????????????<bean?class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"?p:name="xxx"/>
?????????</set>
??????</property>
???</bean>
???????上面的配置使用的是一個(gè)SimpleCacheManager,其中包含一個(gè)名為“xxx”的ConcurrentMapCache。
?
1.2.2??基于Ehcache的配置
???<!--?Ehcache實(shí)現(xiàn)?-->
???<bean?id="cacheManager"?class="org.springframework.cache.ehcache.EhCacheCacheManager"?p:cache-manager-ref="ehcacheManager"/>
???<bean?id="ehcacheManager"?class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"?p:config-location="ehcache.xml"/>
???????上面的配置使用了一個(gè)Spring提供的EhCacheCacheManager來生成一個(gè)Spring的CacheManager,其接收一個(gè)Ehcache的CacheManager,因?yàn)檎嬲脕泶嫒刖彺鏀?shù)據(jù)的還是Ehcache。Ehcache的CacheManager是通過Spring提供的EhCacheManagerFactoryBean來生成的,其可以通過指定ehcache的配置文件位置來生成一個(gè)Ehcache的CacheManager。若未指定則將按照Ehcache的默認(rèn)規(guī)則取classpath根路徑下的ehcache.xml文件,若該文件也不存在,則獲取Ehcache對(duì)應(yīng)jar包中的ehcache-failsafe.xml文件作為配置文件。
2、方法的注解
?????? Spring為我們提供了幾個(gè)注解來支持Spring Cache。其核心主要是@Cacheable和@CacheEvict。使用@Cacheable標(biāo)記的方法在執(zhí)行后Spring Cache將緩存其返回結(jié)果,而使用@CacheEvict標(biāo)記的方法會(huì)在方法執(zhí)行前或者執(zhí)行后移除Spring Cache中的某些元素。下面我們將來詳細(xì)介紹一下Spring基于注解對(duì)Cache的支持所提供的幾個(gè)注解。
2.1????@Cacheable
?????? @Cacheable可以標(biāo)記在一個(gè)方法上,也可以標(biāo)記在一個(gè)類上。當(dāng)標(biāo)記在一個(gè)方法上時(shí)表示該方法是支持緩存的,當(dāng)標(biāo)記在一個(gè)類上時(shí)則表示該類所有的方法都是支持緩存的。對(duì)于一個(gè)支持緩存的方法,Spring會(huì)在其被調(diào)用后將其返回值緩存起來,以保證下次利用同樣的參數(shù)來執(zhí)行該方法時(shí)可以直接從緩存中獲取結(jié)果,而不需要再次執(zhí)行該方法。Spring在緩存方法的返回值時(shí)是以鍵值對(duì)進(jìn)行緩存的,值就是方法的返回結(jié)果,至于鍵的話,Spring又支持兩種策略,默認(rèn)策略和自定義策略,這個(gè)稍后會(huì)進(jìn)行說明。需要注意的是當(dāng)一個(gè)支持緩存的方法在對(duì)象內(nèi)部被調(diào)用時(shí)是不會(huì)觸發(fā)緩存功能的。@Cacheable可以指定三個(gè)屬性,value、key和condition。
2.1.1??value屬性指定Cache名稱
?????? value屬性是必須指定的,其表示當(dāng)前方法的返回值是會(huì)被緩存在哪個(gè)Cache上的,對(duì)應(yīng)Cache的名稱。其可以是一個(gè)Cache也可以是多個(gè)Cache,當(dāng)需要指定多個(gè)Cache時(shí)其是一個(gè)數(shù)組。
???@Cacheable("cache1")//Cache是發(fā)生在cache1上的
???public?User find(Integer id) {
??????returnnull;
?? }
?
???@Cacheable({"cache1",?"cache2"})//Cache是發(fā)生在cache1和cache2上的
???public?User find(Integer id) {
??????returnnull;
?? }
?
2.1.2??使用key屬性自定義key
?????? key屬性是用來指定Spring緩存方法的返回結(jié)果時(shí)對(duì)應(yīng)的key的。該屬性支持SpringEL表達(dá)式。當(dāng)我們沒有指定該屬性時(shí),Spring將使用默認(rèn)策略生成key。我們這里先來看看自定義策略,至于默認(rèn)策略會(huì)在后文單獨(dú)介紹。
???????自定義策略是指我們可以通過Spring的EL表達(dá)式來指定我們的key。這里的EL表達(dá)式可以使用方法參數(shù)及它們對(duì)應(yīng)的屬性。使用方法參數(shù)時(shí)我們可以直接使用“#參數(shù)名”或者“#p參數(shù)index”。下面是幾個(gè)使用參數(shù)作為key的示例。
???@Cacheable(value="users", key="#id")
???public?User find(Integer id) {
??????returnnull;
?? }
?
???@Cacheable(value="users", key="#p0")
???public?User find(Integer id) {
??????returnnull;
?? }
?
???@Cacheable(value="users", key="#user.id")
???public?User find(User user) {
??????returnnull;
?? }
?
???@Cacheable(value="users", key="#p0.id")
???public?User find(User user) {
??????returnnull;
?? }
?
???????除了上述使用方法參數(shù)作為key之外,Spring還為我們提供了一個(gè)root對(duì)象可以用來生成key。通過該root對(duì)象我們可以獲取到以下信息。
| 屬性名稱 | 描述 | 示例 |
| methodName | 當(dāng)前方法名 | #root.methodName |
| method | 當(dāng)前方法 | #root.method.name |
| target | 當(dāng)前被調(diào)用的對(duì)象 | #root.target |
| targetClass | 當(dāng)前被調(diào)用的對(duì)象的class | #root.targetClass |
| args | 當(dāng)前方法參數(shù)組成的數(shù)組 | #root.args[0] |
| caches | 當(dāng)前被調(diào)用的方法使用的Cache | #root.caches[0].name |
?
???????當(dāng)我們要使用root對(duì)象的屬性作為key時(shí)我們也可以將“#root”省略,因?yàn)镾pring默認(rèn)使用的就是root對(duì)象的屬性。如:
???@Cacheable(value={"users",?"xxx"}, key="caches[1].name")
???public?User find(User user) {
??????returnnull;
?? }
?
2.1.3??condition屬性指定發(fā)生的條件
???????有的時(shí)候我們可能并不希望緩存一個(gè)方法所有的返回結(jié)果。通過condition屬性可以實(shí)現(xiàn)這一功能。condition屬性默認(rèn)為空,表示將緩存所有的調(diào)用情形。其值是通過SpringEL表達(dá)式來指定的,當(dāng)為true時(shí)表示進(jìn)行緩存處理;當(dāng)為false時(shí)表示不進(jìn)行緩存處理,即每次調(diào)用該方法時(shí)該方法都會(huì)執(zhí)行一次。如下示例表示只有當(dāng)user的id為偶數(shù)時(shí)才會(huì)進(jìn)行緩存。
???@Cacheable(value={"users"}, key="#user.id", condition="#user.id%2==0")
???public?User find(User user) {
????? System.out.println("find user by user "?+ user);
??????return?user;
?? }
?
2.2 ? @CachePut
???????在支持Spring Cache的環(huán)境下,對(duì)于使用@Cacheable標(biāo)注的方法,Spring在每次執(zhí)行前都會(huì)檢查Cache中是否存在相同key的緩存元素,如果存在就不再執(zhí)行該方法,而是直接從緩存中獲取結(jié)果進(jìn)行返回,否則才會(huì)執(zhí)行并將返回結(jié)果存入指定的緩存中。@CachePut也可以聲明一個(gè)方法支持緩存功能。與@Cacheable不同的是使用@CachePut標(biāo)注的方法在執(zhí)行前不會(huì)去檢查緩存中是否存在之前執(zhí)行過的結(jié)果,而是每次都會(huì)執(zhí)行該方法,并將執(zhí)行結(jié)果以鍵值對(duì)的形式存入指定的緩存中。
?????? @CachePut也可以標(biāo)注在類上和方法上。使用@CachePut時(shí)我們可以指定的屬性跟@Cacheable是一樣的。
???@CachePut("users")//每次都會(huì)執(zhí)行方法,并將結(jié)果存入指定的緩存中
???public?User find(Integer id) {
??????returnnull;
?? }
?
2.3 ?@CacheEvict
?????? @CacheEvict是用來標(biāo)注在需要清除緩存元素的方法或類上的。當(dāng)標(biāo)記在一個(gè)類上時(shí)表示其中所有的方法的執(zhí)行都會(huì)觸發(fā)緩存的清除操作。@CacheEvict可以指定的屬性有value、key、condition、allEntries和beforeInvocation。其中value、key和condition的語義與@Cacheable對(duì)應(yīng)的屬性類似。即value表示清除操作是發(fā)生在哪些Cache上的(對(duì)應(yīng)Cache的名稱);key表示需要清除的是哪個(gè)key,如未指定則會(huì)使用默認(rèn)策略生成的key;condition表示清除操作發(fā)生的條件。下面我們來介紹一下新出現(xiàn)的兩個(gè)屬性allEntries和beforeInvocation。
2.3.1??allEntries屬性
?????? allEntries是boolean類型,表示是否需要清除緩存中的所有元素。默認(rèn)為false,表示不需要。當(dāng)指定了allEntries為true時(shí),Spring Cache將忽略指定的key。有的時(shí)候我們需要Cache一下清除所有的元素,這比一個(gè)一個(gè)清除元素更有效率。
???@CacheEvict(value="users", allEntries=true)
???public?void?delete(Integer id) {
????? System.out.println("delete user by id: "?+ id);
?? }
?
2.3.2??beforeInvocation屬性
???????清除操作默認(rèn)是在對(duì)應(yīng)方法成功執(zhí)行之后觸發(fā)的,即方法如果因?yàn)閽伋霎惓6茨艹晒Ψ祷貢r(shí)也不會(huì)觸發(fā)清除操作。使用beforeInvocation可以改變觸發(fā)清除操作的時(shí)間,當(dāng)我們指定該屬性值為true時(shí),Spring會(huì)在調(diào)用該方法之前清除緩存中的指定元素。
???@CacheEvict(value="users", beforeInvocation=true)
???public?void?delete(Integer id) {
????? System.out.println("delete user by id: "?+ id);
?? }
?
???????其實(shí)除了使用@CacheEvict清除緩存元素外,當(dāng)我們使用Ehcache作為實(shí)現(xiàn)時(shí),我們也可以配置Ehcache自身的驅(qū)除策略,其是通過Ehcache的配置文件來指定的。由于Ehcache不是本文描述的重點(diǎn),這里就不多贅述了,想了解更多關(guān)于Ehcache的信息,請(qǐng)查看我關(guān)于Ehcache的專欄。
?
2.4 @Caching
?????? @Caching注解可以讓我們?cè)谝粋€(gè)方法或者類上同時(shí)指定多個(gè)Spring Cache相關(guān)的注解。其擁有三個(gè)屬性:cacheable、put和evict,分別用于指定@Cacheable、@CachePut和@CacheEvict。
???@Caching(cacheable =?@Cacheable("users"), evict = {?@CacheEvict("cache2"),
?????????@CacheEvict(value =?"cache3", allEntries =?true) })
???public?User find(Integer id) {
??????returnnull;
?? }
?
2.5 使用自定義注解
?????? Spring允許我們?cè)谂渲每删彺娴姆椒〞r(shí)使用自定義的注解,前提是自定義的注解上必須使用對(duì)應(yīng)的注解進(jìn)行標(biāo)注。如我們有如下這么一個(gè)使用@Cacheable進(jìn)行標(biāo)注的自定義注解。
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Cacheable(value="users")
public?@interface?MyCacheable?{
?
}
???????那么在我們需要緩存的方法上使用@MyCacheable進(jìn)行標(biāo)注也可以達(dá)到同樣的效果。
???@MyCacheable
???public?User findById(Integer id) {
????? System.out.println("find user by id: "?+ id);
????? User user =?new?User();
????? user.setId(id);
????? user.setName("Name"?+ id);
??????return?user;
?? }
?
3 鍵的生成策略
???????鍵的生成策略有兩種,一種是默認(rèn)策略,一種是自定義策略。
3.1 ? 默認(rèn)策略
???????默認(rèn)的key生成策略是通過KeyGenerator生成的,其默認(rèn)策略如下:
n??如果方法沒有參數(shù),則使用0作為key。
n??如果只有一個(gè)參數(shù)的話則使用該參數(shù)作為key。
n??如果參數(shù)多余一個(gè)的話則使用所有參數(shù)的hashCode作為key。
?
???????如果我們需要指定自己的默認(rèn)策略的話,那么我們可以實(shí)現(xiàn)自己的KeyGenerator,然后指定我們的Spring Cache使用的KeyGenerator為我們自己定義的KeyGenerator。
???????使用基于注解的配置時(shí)是通過cache:annotation-driven指定的.
???<cache:annotation-driven?key-generator="userKeyGenerator"/>
??
???<bean?id="userKeyGenerator"?class="com.xxx.cache.UserKeyGenerator"/>
?
???????而使用基于XML配置時(shí)是通過cache:advice來指定的。
???<cache:advice?id="cacheAdvice"?cache-manager="cacheManager"?key-generator="userKeyGenerator">
???</cache:advice>
?
???????需要注意的是此時(shí)我們所有的Cache使用的Key的默認(rèn)生成策略都是同一個(gè)KeyGenerator。
3.2 ? 自定義策略
???????自定義策略是指我們可以通過Spring的EL表達(dá)式來指定我們的key。這里的EL表達(dá)式可以使用方法參數(shù)及它們對(duì)應(yīng)的屬性。使用方法參數(shù)時(shí)我們可以直接使用“#參數(shù)名”或者“#p參數(shù)index”。下面是幾個(gè)使用參數(shù)作為key的示例。
???@Cacheable(value="users", key="#id")
???public?User find(Integer id) {
??????returnnull;
?? }
?
???@Cacheable(value="users", key="#p0")
???public?User find(Integer id) {
??????returnnull;
?? }
?
???@Cacheable(value="users", key="#user.id")
???public?User find(User user) {
??????returnnull;
?? }
?
???@Cacheable(value="users", key="#p0.id")
???public?User find(User user) {
??????returnnull;
?? }
?
???????除了上述使用方法參數(shù)作為key之外,Spring還為我們提供了一個(gè)root對(duì)象可以用來生成key。通過該root對(duì)象我們可以獲取到以下信息。
| 屬性名稱 | 描述 | 示例 |
| methodName | 當(dāng)前方法名 | #root.methodName |
| method | 當(dāng)前方法 | #root.method.name |
| target | 當(dāng)前被調(diào)用的對(duì)象 | #root.target |
| targetClass | 當(dāng)前被調(diào)用的對(duì)象的class | #root.targetClass |
| args | 當(dāng)前方法參數(shù)組成的數(shù)組 | #root.args[0] |
| caches | 當(dāng)前被調(diào)用的方法使用的Cache | #root.caches[0].name |
?
???????當(dāng)我們要使用root對(duì)象的屬性作為key時(shí)我們也可以將“#root”省略,因?yàn)镾pring默認(rèn)使用的就是root對(duì)象的屬性。如:
???@Cacheable(value={"users",?"xxx"}, key="caches[1].name")
???public?User find(User user) {
??????returnnull;
?? }
?
?示例代碼:
<!-- cachetest 過期時(shí)間設(shè)置為10秒--><cache name="cacheTest"maxElementsInMemory="1000"eternal="false"overflowToDisk="true"timeToIdleSeconds="10"timeToLiveSeconds="20"/><cache name="cache1"maxElementsInMemory="1000"eternal="false"overflowToDisk="true"timeToIdleSeconds="3600"timeToLiveSeconds="7200"/><cache name="cache2"maxElementsInMemory="1000"eternal="false"overflowToDisk="true"timeToIdleSeconds="3600"timeToLiveSeconds="7200"/>
Service
package com.jeecg.cache;import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Caching; import org.springframework.stereotype.Service;@Service("ehcacheServiceImpl") public class EhcacheServiceImpl implements EhCacheService {/*** @Cacheable* value 緩存的名稱,在 spring 配置文件中定義,必須指定至少一個(gè)* 相當(dāng)于save()到緩存*/@Cacheable(value="cacheTest",key="#param")public String getTimestamp(String param) {Long timeStamp = System.currentTimeMillis();return timeStamp.toString();}/*** @CachePut* value 緩存的名稱,在 spring 配置文件中定義,必須指定至少一個(gè)* 相當(dāng)于update()緩存*/@CachePut(value="cacheTest",key="#param")public String testCachePut(String param){return "123456";}/*** @CacheEvict * value 緩存的名稱,在 spring 配置文件中定義,必須指定至少一個(gè)* 相當(dāng)于delete()緩存*/@CacheEvict(value="cacheTest",key="#param")public void testCacheEvict(String param) {System.out.println("清除testCacheEvict............");}/*** @Caching* 是組合操作緩存*/@Caching(cacheable = @Cacheable(value="cache1",key="1000"),evict = { @CacheEvict(value="cache2",key="2000")})public String testCaching() {System.out.println("testCaching");return "Cache1變更值";}@Cacheable(value="cache1",key="1000")public String getCache1(){System.out.println("getCache1");return "Cache1初始值";}@Cacheable(value="cache2",key="2000")public String getCache2(){System.out.println("getCache2");return "Cache2初始值";} }
controller
package com.jeecg.cache.controller;import javax.servlet.http.HttpServletRequest;import org.jeecgframework.core.common.controller.BaseController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView;import com.jeecg.cache.EhCacheService;@Controller @RequestMapping("/CachceController") public class CachceController extends BaseController {@Autowiredprivate EhCacheService ehcacheServiceImpl;@RequestMapping(params = { "cache", "page" })public ModelAndView cache(@RequestParam("page") String page,HttpServletRequest request) {ModelAndView modelAndView = new ModelAndView(page);return modelAndView;}@RequestMapping(params = "testCacheable")public void testCacheable() throws InterruptedException{System.out.println("第一次調(diào)用:" + ehcacheServiceImpl.getTimestamp("param"));Thread.sleep(2000);System.out.println("2秒之后調(diào)用:" + ehcacheServiceImpl.getTimestamp("param"));Thread.sleep(11000);System.out.println("再過11秒之后調(diào)用:" + ehcacheServiceImpl.getTimestamp("param"));}@RequestMapping(params = "testCachePut")public void testCachePut() throws InterruptedException{System.out.println("第一次調(diào)用:" + ehcacheServiceImpl.getTimestamp("param"));Thread.sleep(2000);System.out.println("2秒之后調(diào)用:" + ehcacheServiceImpl.getTimestamp("param"));System.out.println("注入cacheput");ehcacheServiceImpl.testCachePut("param");Thread.sleep(2000);System.out.println("2秒之后查看cache:" + ehcacheServiceImpl.getTimestamp("param"));}@RequestMapping(params = "testCacheEvict")public void testCacheEvict() throws InterruptedException{System.out.println("第一次調(diào)用:" + ehcacheServiceImpl.getTimestamp("param"));Thread.sleep(2000);System.out.println("2秒之后調(diào)用:" + ehcacheServiceImpl.getTimestamp("param"));ehcacheServiceImpl.testCacheEvict("param");Thread.sleep(2000);System.out.println("2秒之后查看cache:" + ehcacheServiceImpl.getTimestamp("param"));}@RequestMapping(params = "testCaching")public void testCaching() throws InterruptedException{System.out.println("testCaching:");ehcacheServiceImpl.testCaching();System.out.println("注入cache1和cache2:");ehcacheServiceImpl.getCache1();ehcacheServiceImpl.getCache2();Thread.sleep(2000);System.out.println("2秒之后查看cache1:" + ehcacheServiceImpl.getCache1() +",cache2:"+ ehcacheServiceImpl.getCache2());ehcacheServiceImpl.testCaching();Thread.sleep(2000);System.out.println("2秒之后再查看cache1:" + ehcacheServiceImpl.getCache1() +",cache2:"+ ehcacheServiceImpl.getCache2());} }
?
?
參考:
http://yunzhu.iteye.com/blog/2119094?utm_source=tuicool
http://blog.csdn.net/frankcheng5143/article/details/50776542
http://blog.csdn.net/yangfanend/article/details/7661885
http://blog.csdn.net/clj198606061111/article/details/41121437
轉(zhuǎn)載于:https://www.cnblogs.com/cac2020/p/6029666.html
總結(jié)
以上是生活随笔為你收集整理的Java缓存学习之五:spring 对缓存的支持的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个命中注定个性签名!
- 下一篇: 如何实现后台向前台传数据