Spring中緩存註解@Cacheable、@CachePut、@CacheEvict 註釋介紹

@Cacheable、@CachePut、@CacheEvict 註釋介紹

@Cacheable 做用和配置方法html

@Cacheable 的做用 主要針對方法配置,可以根據方法的請求參數對其結果進行緩存spring

@Cacheable 主要的參數
value 緩存的名稱,在 spring 配置文件中定義,必須指定至少一個 例如:
@Cacheable(value=」mycache」) 或者 
@Cacheable(value={」cache1」,」cache2」}
key 緩存的 key,能夠爲空,若是指定要按照 SpEL 表達式編寫,若是不指定,則缺省按照方法的全部參數進行組合 例如:
@Cacheable(value=」testcache」,key=」#userName」)
condition 緩存的條件,能夠爲空,使用 SpEL 編寫,返回 true 或者 false,只有爲 true 才進行緩存 例如:
@Cacheable(value=」testcache」,condition=」#userName.length()>2」)

------------------------------------------------------------express

--////////////////////////////////////////////////////////////////////////////////緩存

@CachePut 做用和配置方法spa

@CachePut 的做用 主要針對方法配置,可以根據方法的請求參數對其結果進行緩存,和 @Cacheable 不一樣的是,它每次都會觸發真實方法的調用.net

@CachePut 主要的參數
value 緩存的名稱,在 spring 配置文件中定義,必須指定至少一個 例如:
@Cacheable(value=」mycache」) 或者 
@Cacheable(value={」cache1」,」cache2」}
key 緩存的 key,能夠爲空,若是指定要按照 SpEL 表達式編寫,若是不指定,則缺省按照方法的全部參數進行組合 例如:
@Cacheable(value=」testcache」,key=」#userName」)
condition 緩存的條件,能夠爲空,使用 SpEL 編寫,返回 true 或者 false,只有爲 true 才進行緩存 例如:
@Cacheable(value=」testcache」,condition=」#userName.length()>2」)

 

//////////////////////////////////////////////////////code

 

@CacheEvict 做用和配置方法htm

@CachEvict 的做用 主要針對方法配置,可以根據必定的條件對緩存進行清空blog

@CacheEvict 主要的參數
value 緩存的名稱,在 spring 配置文件中定義,必須指定至少一個 例如:
@CachEvict(value=」mycache」) 或者 
@CachEvict(value={」cache1」,」cache2」}
key 緩存的 key,能夠爲空,若是指定要按照 SpEL 表達式編寫,若是不指定,則缺省按照方法的全部參數進行組合 例如:
@CachEvict(value=」testcache」,key=」#userName」)
condition 緩存的條件,能夠爲空,使用 SpEL 編寫,返回 true 或者 false,只有爲 true 才清空緩存 例如:
@CachEvict(value=」testcache」,
condition=」#userName.length()>2」)
allEntries 是否清空全部緩存內容,缺省爲 false,若是指定爲 true,則方法調用後將當即清空全部緩存 例如:
@CachEvict(value=」testcache」,allEntries=true)
beforeInvocation 是否在方法執行前就清空,缺省爲 false,若是指定爲 true,則在方法尚未執行的時候就清空緩存,缺省狀況下,若是方法執行拋出異常,則不會清空緩存 例如:
@CachEvict(value=」testcache」,beforeInvocation=true)

 

--------------ci

額外補充:@cache(「something");這個至關於save()操做,@cachePut至關於Update()操做,只要他標示的方法被調用,那麼都會緩存起來,而@cache則是先看下有沒已經緩存了,而後再選擇是否執行方法。@CacheEvict至關於Delete()操做。用來清除緩存用的。

這寫配置的聲明須要配置好了@enableCache纔有用,具體的配置能夠看這篇文章

http://blog.csdn.net/sanjay_f/article/details/47363845

若是忘記了SpEL怎麼用了, do yourself a favor and read Chapter 9, Spring Expression Language (SpEL):

  1. importorg.springframework.stereotype.Service;  
  2. importcom.springcache.annotation.Cacheable;  
  3. @Service  
  4. @Cacheable  
  5. public class MemcachedService{  
  6.   @Cacheable(name="remote",key="'USER_NAME_'+#args[0]")  
  7. public String storeUserName(String accountId,String name)  
  8. {  
  9.   return name;  
  10. }  
  11.   @Cacheable(name="remote")  
  12.  public String storeUserAddress(String accountId,String address){  
  13.    return address;  
  14.   }  
  15. }  

不知道大家注意到一個問題沒有,就是全部的@Cacheable()裏面都有一個name=「xxx」的屬性,這顯然若是方法多了,寫起來也是挺累的,若是能夠一次性聲明完 那就省事了,

因此,有了@CacheConfig這個配置,@CacheConfig is a class-level annotation that allows to share the cache names,不過不用擔憂,若是你在你的方法寫別的名字,那麼依然以方法的名字爲準。

  1. @CacheConfig("books")  
  2. public class BookRepositoryImpl implements BookRepository {  
  3.   
  4.     @Cacheable  
  5.     public Book findBook(ISBN isbn) {...}  
  6. }  


 

固然還有另一個狀況,@Cacheable(name="remote",key="'USER_NAME_'+#args[0]" ,conditional=「xxx」,allEntries=true,beforeInvocation=true) ,像這樣的配置就很長,

@Cacheable(name = "book", key="#isbn",conditional=「xxx」,allEntries=true,beforeInvocation=true) 
public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)

這樣的配置很長,並且有可能聲明在不少個方法的,因此咱們很想精簡點,容易配置些。因此

@findBookByIsbnervice
public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)

 

新建一個文件findBookByIsbn, 內容以下

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Cacheable(cacheNames="books", key="#isbn")
public @interface findBookByIsbn {
}
相關文章
相關標籤/搜索