Spring緩存註解使用

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

表 1. @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

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

表 2. @CachePut 做用和配置方法app

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

@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」)

 

//////////////////////////////////////////////////////ui

 

表 3. @CacheEvict 做用和配置方法this

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

@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)

 

--------------.net

額外補充:@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):

 

-------------

[html] view plain copy

  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,不過不用擔憂,若是你在你的方法寫別的名字,那麼依然以方法的名字爲準。

 

[html] view plain copy

  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 {
}

 

 

 

-------------------------------

Features summary

For those who are familiar with Spring’s caching annotations, the following table describes the main differences between the Spring annotations and the JSR-107 counterpart:

Table 35.3. Spring vs. JSR-107 caching annotations

Spring JSR-107 Remark

@Cacheable

@CacheResult

Fairly similar. @CacheResult can cache specific exceptions and force the execution of the method regardless of the content of the cache.

@CachePut

@CachePut

While Spring updates the cache with the result of the method invocation, JCache requires to pass it as an argument that is annotated with @CacheValue. Due to this difference, JCache allows to update the cache before or after the actual method invocation.

@CacheEvict

@CacheRemove

Fairly similar. @CacheRemove supports a conditional evict in case the method invocation results in an exception.

@CacheEvict(allEntries=true)

@CacheRemoveAll

See @CacheRemove.

@CacheConfig

@CacheDefaults

Allows to configure the same concepts, in a similar fashion.

 

--------------

 

 

關於異常

 

JCache can manage exceptions thrown by annotated methods:

this can prevent an update of the cache but it can also cache the exception as an indicator of the failure instead of calling the method again. 

Let’s assume that InvalidIsbnNotFoundException is thrown if the structure of the ISBN is invalid.

This is a permanent failure, no book could ever be retrieved with such parameter. 

The following caches the exception so that further calls with the same,

invalid ISBN, throws the cached exception directly instead of invoking the method again.

@CacheResult(cacheName="books", exceptionCacheName="failures"
             cachedExceptions = InvalidIsbnNotFoundException.class)
public Book findBook(ISBN isbn)
相關文章
相關標籤/搜索