Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig的介绍和redi的整合使用
来源:互联网 发布:大数据平台 架构设计 编辑:程序博客网 时间:2024/04/28 18:31
@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):
-------------
- 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,不过不用担心,如果你在你的方法写别的名字,那么依然以方法的名字为准。- @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) ,像这样的配置就很长,
@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)
如果要在项目中和redis整合使用的话:我们在/resource下面新建一个redis的配置properity文件,内容如下
redisProperity文件
redis.host=127.0.0.1//redis的服务器地址redis.port=6379redis.pass=123456//密码redis.default.db=0//链接数据库redis.timeout=100000//客户端超时时间单位是毫秒redis.maxActive=300// 最大连接数redis.maxIdle=100//最大空闲数接着我们写配置文件,具体如下,楼主喜欢java版的,xml的木有。
- @Configuration
- @EnableCaching
- @PropertySource("classpath:redisProperity")
- public class CacheConfiger {
- private static final String PROPERTY_REDIS_NAME_HOST= "redis.host";
- private static final String PROPERTY_REDIS_PROT= "redis.port";
- @Resource
- private Environment env;
- @Bean
- public CacheManager cacheManager(RedisTemplate redisTemplate) {
- return new RedisCacheManager(redisTemplate);
- }
- @Bean
- public JedisConnectionFactory redisConnectionFactory() {
- JedisConnectionFactory jedisConnectionFactory =
- new JedisConnectionFactory();
- jedisConnectionFactory.afterPropertiesSet();
- jedisConnectionFactory.setHostName(env.
- getRequiredProperty(PROPERTY_REDIS_NAME_HOST));
- jedisConnectionFactory.setPort(Integer.parseInt(env.
- getRequiredProperty(PROPERTY_REDIS_PROT)));
- return jedisConnectionFactory;
- }
- @Bean
- public RedisTemplate<String, String> redisTemplate(
- RedisConnectionFactory redisCF) {
- RedisTemplate<String, String> redisTemplate =
- new RedisTemplate<String, String>();
- redisTemplate.setConnectionFactory(redisCF);
- redisTemplate.afterPropertiesSet();
- return redisTemplate;
- }
- }
阅读全文
0 0
- Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig的介绍和redi的整合使用
- 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注解@Cacheable、@CachePut、@CacheEvict的使用
- Spring缓存注解@Cacheable,@CachePut , @CacheEvict介绍
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用
- Spring 缓存注解(@Cacheable、@CachePut、@CacheEvict)使用
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- 段寄存器的引用
- 执行脚本文件出现bad interpreter:No such file or directory的原因
- Android 样式开发
- 恢复sybase数据库
- meanshift算法学习(三):自己实现meanshift
- Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig的介绍和redi的整合使用
- linux常用命令
- vue项目打包到tomcat下运行
- 欢迎使用CSDN-markdown编辑器
- Leaflet加载图片-imageoverlay
- MySQL Query Profiler(性能分析工具)的使用方法
- JAVA NIO 概述及例子
- IOS实现3DTouch功能
- 【数据结构】【计算机视觉】并查集(disjoint set)结构介绍