基於xml的配置感覺沒有注解形式簡單明了,咱不考慮了。
進入正題之前先提個疑問,希望知道的人能告訴一下
下述介紹會有這段代碼:
@Cacheable(value="myCache", key="'get'+#userNo")
public String get(String userNo){
此處#userNo會動態的傳入?
1,spring配置文件

<bean id="cacheManager" class="org.springframework.cache.guava.GuavaCacheManager"> <property name="cacheSpecification" value="initialCapacity=512,maximumSize=4096,concurrencyLevel=16,expireAfterAccess=360s" /> </bean> <!-- 啟用緩存注解功能 --> <cache:annotation-driven order="1" cache-manager="cacheManager" proxy-target-class="true" />
@參考文章1里提供了SimpleCacheManager進行的配置
2,類中使用緩存
@參考文章1里寫的比較好,不自己寫代碼了,直接復制粘貼

package com.jadyer.service; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; /** * Cacheable注解負責將方法的返回值加入到緩存中 * CacheEvict注解負責清除緩存(它的三個參數與@Cacheable的意思是一樣的) * @see ---------------------------------------------------------------------------------------------------------- * @see value------緩存位置的名稱,不能為空,若使用EHCache則其值為ehcache.xml中的<cache name="myCache"/> * @see key--------緩存的Key,默認為空(表示使用方法的參數類型及參數值作為key),支持SpEL * @see condition--只有滿足條件的情況才會加入緩存,默認為空(表示全部都加入緩存),支持SpEL * @see ---------------------------------------------------------------------------------------------------------- * @see 該注解的源碼位於spring-context-3.2.4.RELEASE-sources.jar中 * @see Spring針對Ehcache支持的Java源碼位於spring-context-support-3.2.4.RELEASE-sources.jar中 * @see ---------------------------------------------------------------------------------------------------------- * @create Oct 3, 2013 6:17:54 PM * @author 玄玉<http://blog.csdn.net/jadyer> */ @Service public class UserService { private Map<String, String> usersData = new ConcurrentHashMap<String, String>(); public UserService(){ System.out.println("用戶數據初始化..開始"); usersData.put("2", "玄玉"); usersData.put("3", "我的博客:http://blog.csdn.net/jadyer"); System.out.println("用戶數據初始化..完畢"); } //將查詢到的數據緩存到myCache中,並使用方法名稱加上參數中的userNo作為緩存的key //通常更新操作只需刷新緩存中的某個值,所以為了准確的清除特定的緩存,故定義了這個唯一的key,從而不會影響其它緩存值 @Cacheable(value="myCache", key="'get'+#userNo") public String get(String userNo){ System.out.println("數據庫中查到此用戶號[" + userNo + "]對應的用戶名為[" + usersData.get(userNo) + "]"); return usersData.get(userNo); } @CacheEvict(value="myCache", key="'get'+#userNo") public void update(String userNo){ System.out.println("移除緩存中此用戶號[" + userNo + "]對應的用戶名[" + usersData.get(userNo) + "]的緩存"); } //allEntries為true表示清除value中的全部緩存,默認為false @CacheEvict(value="myCache", allEntries=true) public void removeAll(){ System.out.println("移除緩存中的所有數據"); } }
上述參考文章沒有講到的部分知識點,從@參考文章2找了些資料做補充

@Caching注解可以讓我們在一個方法或者類上同時指定多個Spring Cache相關的注解。其擁有三個屬性:cacheable、put和evict,分別用於指定@Cacheable、@CachePut和@CacheEvict。 @Caching(cacheable = @Cacheable("users"), evict = { @CacheEvict("cache2"), @CacheEvict(value = "cache3", allEntries = true) }) public User find(Integer id) { returnnull; } 在支持Spring Cache的環境下,對於使用@Cacheable標注的方法,Spring在每次執行前都會檢查Cache中是否存在相同key的緩存元素,如果存在就不再執行該方法,而是直接從緩存中獲取結果進行返回,否則才會執行並將返回結果存入指定的緩存中。@CachePut也可以聲明一個方法支持緩存功能。與@Cacheable不同的是使用@CachePut標注的方法在執行前不會去檢查緩存中是否存在之前執行過的結果,而是每次都會執行該方法,並將執行結果以鍵值對的形式存入指定的緩存中。 @CachePut也可以標注在類上和方法上。使用@CachePut時我們可以指定的屬性跟@Cacheable是一樣的。 @CachePut("users")//每次都會執行方法,並將結果存入指定的緩存中 public User find(Integer id) { returnnull; }
上述2步驟后,就可以愉快的使用了!~