您的位置:首页 > 编程语言 > Java开发

Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig使用

2016-09-12 17:01 513 查看


@Cacheable、@CachePut、@CacheEvict 注释介绍


表 1. @Cacheable 作用和配置方法

@Cacheable 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存
@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”)

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

--////////////////////////////////////////////////////////////////////////////////

表 2. @CachePut 作用和配置方法

@CachePut 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用
@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”)

表 3. @CacheEvict 作用和配置方法

@CachEvict 的作用 主要针对方法配置,能够根据一定的条件对缓存进行清空
@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)
--------------

额外补充:@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

importorg.springframework.stereotype.Service;  

importcom.springcache.annotation.Cacheable;  

@Service  

@Cacheable  

public class MemcachedService{  

  @Cacheable(name="remote",key="'USER_NAME_'+#args[0]")  

public String storeUserName(String accountId,String name)  

{  

  return name;  

}  

  @Cacheable(name="remote")  

 public String storeUserAddress(String accountId,String address){  

   return address;  

  }  

}  

不知道你们注意到一个问题没有,就是所有的@Cacheable()里面都有一个name=“xxx”的属性,这显然如果方法多了,写起来也是挺累的,如果可以一次性声明完 那就省事了,

所以,有了@CacheConfig这个配置,
@CacheConfig
 is
a class-level annotation that allows to share the cache names,不过不用担心,如果你在你的方法写别的名字,那么依然以方法的名字为准。

[html] view
plain copy

@CacheConfig("books")  

public class BookRepositoryImpl implements BookRepository {  

  

    @Cacheable  

    public Book findBook(ISBN isbn) {...}  

}  

当然还有另外一个情况,@Cacheable(name="remote",key="'USER_NAME_'+#args[0]"
,conditional=“xxx”,allEntries=true,beforeInvocation=true) ,像这样的配置就很长,

<pre class="programlisting" name="code" style="white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); line-height: 1.4; font-size: 15px; padding: 6px 10px; border: 1px solid rgb(204, 204, 204); clear: both; overflow: auto; font-family: Consolas, 'Liberation Mono', Courier, monospace; background-color: rgb(248, 248, 248);"><em><span class="hl-annotation" style="color: gray;"><span class="crayon-sy" style="font-family: inherit; white-space: pre; height: inherit; font-size: inherit !important; font-weight: inherit !important; line-height: inherit !important; color: rgb(51, 51, 51) !important;">@</span><span class="crayon-e" style="font-family: inherit; white-space: pre; height: inherit; font-size: inherit !important; font-weight: inherit !important; line-height: inherit !important; color: rgb(0, 78, 208) !important;">Cacheable</span><span class="crayon-sy" style="font-family: inherit; white-space: pre; height: inherit; font-size: inherit !important; font-weight: inherit !important; line-height: inherit !important; color: rgb(51, 51, 51) !important;">(</span><span class="crayon-v" style="font-family: inherit; white-space: pre; height: inherit; font-size: inherit !important; font-weight: inherit !important; line-height: inherit !important; color: rgb(0, 45, 122) !important;">name</span><span class="crayon-h" style="font-family: inherit; white-space: pre; height: inherit; font-size: inherit !important; font-weight: inherit !important; line-height: inherit !important; color: rgb(0, 111, 224) !important;"> </span><span class="crayon-o" style="font-family: inherit; white-space: pre; height: inherit; font-size: inherit !important; font-weight: inherit !important; line-height: inherit !important; color: rgb(0, 111, 224) !important;">=</span><span class="crayon-h" style="font-family: inherit; white-space: pre; height: inherit; font-size: inherit !important; font-weight: inherit !important; line-height: inherit !important; color: rgb(0, 111, 224) !important;"> </span><span class="crayon-s" style="font-family: inherit; white-space: pre; height: inherit; font-size: inherit !important; font-weight: inherit !important; line-height: inherit !important; color: rgb(0, 128, 0) !important;">"book"</span><span class="crayon-sy" style="font-family: inherit; white-space: pre; height: inherit; font-size: inherit !important; font-weight: inherit !important; line-height: inherit !important; color: rgb(51, 51, 51) !important;">,</span></span></em><span style="line-height: 1.4; color: gray; font-weight: inherit;"><em> key="#isbn"</em></span><span class="crayon-h" style="font-family: inherit; font-style: italic; height: inherit; font-size: inherit !important; line-height: inherit !important; font-weight: inherit !important; color: rgb(0, 111, 224) !important;">,conditional=“xxx”,<span style="color: rgb(85, 85, 85); font-size: 13px; line-height: 15.36px;">allEntries=true,<span style="line-height: 15.36px;">beforeInvocation=true</span></span></span><span class="crayon-sy" style="font-family: inherit; font-style: italic; height: inherit; font-size: inherit !important; line-height: inherit !important; font-weight: inherit !important;">) </span><em><span class="hl-annotation" style="color: gray;"><span class="crayon-h" style="font-family: inherit; white-space: pre; height: inherit; font-size: inherit !important; font-weight: inherit !important; line-height: inherit !important; color: rgb(0, 111, 224) !important;">
</span></span></em><span class="hl-keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span> Book findBook(ISBN isbn, <span class="hl-keyword" style="color: rgb(127, 0, 85); font-weight: bold;">boolean</span> checkWarehouse, <span class="hl-keyword" style="color: rgb(127, 0, 85); font-weight: bold;">boolean</span> includeUsed)



这样的配置很长,而且有可能声明在很多个方法的,所以我们很想精简点,容易配置些。所以

<pre class="programlisting" name="code" style="white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); line-height: 1.4; font-size: 15px; padding: 6px 10px; border: 1px solid rgb(204, 204, 204); clear: both; overflow: auto; font-family: Consolas, 'Liberation Mono', Courier, monospace; background-color: rgb(248, 248, 248);"><em><span class="hl-annotation" style="color: gray;">@findBookByIsbnervice</span></em>
<span class="hl-keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span> Book findBook(ISBN isbn, <span class="hl-keyword" style="color: rgb(127, 0, 85); font-weight: bold;">boolean</span> checkWarehouse, <span class="hl-keyword" style="color: rgb(127, 0, 85); font-weight: bold;">boolean</span> includeUsed)



新建一个文件findBookByIsbn, 内容如下

<pre class="programlisting" name="code" style="white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); line-height: 1.4; font-size: 15px; padding: 6px 10px; border: 1px solid rgb(204, 204, 204); clear: both; overflow: auto; font-family: Consolas, 'Liberation Mono', Courier, monospace; background-color: rgb(248, 248, 248);"><em><span class="hl-annotation" style="color: gray;">@Retention(RetentionPolicy.RUNTIME)</span>
<span class="hl-annotation" style="color: gray;">@Target({ElementType.METHOD})</span>
<span class="hl-annotation" style="color: gray;">@Cacheable(cacheNames="books", key="#isbn")</span></em>
<span class="hl-keyword" style="font-weight: bold;">public</span> <em><span class="hl-annotation" style="color: gray;">@interface</span></em> <span style="font-style: italic; line-height: 1.4; font-weight: inherit;">findBookByIsbn</span> {
}



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


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

SpringJSR-107Remark
@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.
<pre class="programlisting" name="code" style="white-space: pre-wrap; word-wrap: break-word; font-size: 15px; line-height: 1.4; padding: 6px 10px; border: 1px solid rgb(204, 204, 204); clear: both; overflow: auto; font-family: Consolas, 'Liberation Mono', Courier, monospace; background-color: rgb(248, 248, 248);">@CacheResult(cacheName=<span class="hl-string" style="color: rgb(42, 0, 255);">"books"</span>, <span class="strong"><strong>exceptionCacheName="failures"</strong></span>
<span class="strong"><strong>cachedExceptions = InvalidIsbnNotFoundException.class</strong></span>)
<span class="hl-keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span> Book findBook(ISBN isbn)


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: