Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig使用
来源:互联网 发布:faker瑞文数据 编辑:程序博客网 时间:2024/04/28 22:24
@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)
importorg.springframework.stereotype.Service;importcom.springcache.annotation.Cacheable;@Service@Cacheablepublic 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; }}
@CacheConfig
is a class-level annotation that allows to share the cache names,不过不用担心,如果你在你的方法写别的名字,那么依然以方法的名字为准。@CacheConfig("books")public class BookRepositoryImpl implements BookRepository { @Cacheable public Book findBook(ISBN isbn) {...}}
@Cacheable(name = "book", key="#isbn",conditional=“xxx”,allEntries=true,beforeInvocation=true) public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)
@findBookByIsbnervicepublic Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)
@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
@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)
- Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig使用
- Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig使用
- Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig使用
- Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig使用
- Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig使用
- Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig使用
- Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig使用
- Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig使用
- Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig使用
- Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig的介绍和redi的整合使用
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用
- Spring 缓存注解(@Cacheable、@CachePut、@CacheEvict)使用
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Android TextView跑马灯+悬浮窗口
- [leetcode 237]Delete Node in a Linked List
- Python classmethod staticmethod 进价学习
- Android技能树
- C++primer笔记待续。。。
- Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig使用
- 一起talk C栗子吧(第三十三回:C语言实例--巧用移位)
- NYOJ 17 单调递增最长子序列
- 有关stm32的英文翻译
- 廖雪峰javascrip学习笔记<进阶>
- python pychart图表初安装
- Java内存模型的历史变迁
- 职工管理系统 ——静态数组实现
- uva 1161 Objective: Berlin (最大流)