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)
相关文章
相关标签/搜索