spring boot中spring cache 整合redis
2017-12-14 16:26
836 查看
配置 spring cache 和 redis 的整合 @Bean public RedisCacheManager cacheManager(){ RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate(redisConnectionFactory)); redisCacheManager.setDefaultExpiration(5l); // 设置默认的缓存过期时间(单位:秒) Map<String, Long> expiresMap = new HashMap<String, Long>(); // 对某些命名空间单独设置过期时间 expiresMap.put("demo1", 5l); expiresMap.put("demo2", 10l); redisCacheManager.setExpires(expiresMap); return redisCacheManager; } 启用 spring cache 注解 @EnableCaching 使用: 实现类:@CacheConfig(cacheNames = "current service name") 方法上:@Cacheable 注意: 1、注解一定要加在service层的实现类上,而不是controller,也不是service的interface上; 2、如果有标明@Cacheable注解的方法所在类有实现了某个接口,那么接口中一定要声明该方法,不然缓存不起作用。(原因:spring包装实现类,只有接口里的方法是可见的)。 3、如果标明@Cacheable注解的方法没所在类没有实现任何接口,只需要声明为public即可。 注解: ------------------demo-----class-------------------------- @Service @CacheConfig(cacheNames = "paomotemp") public class MainTestServiceImpl implements MainTestService{ @Autowired private BankInfoService bankInfoService; public List<BankInfo> demo1(String id, String name, Integer age){ List<BankInfo> bankInfos = bankInfoService.getBankInfos(); System.out.println(bankInfos); return bankInfos; } public List<BankInfo> demo2(BankInfo bankInfo){ List<BankInfo> bankInfos = bankInfoService.getBankInfos(); System.out.println(bankInfos); return bankInfos; } public String demo3(){ return "1"; } } ------------------demo-----class-------------------------- 方法级别的注解 @Cacheable : 用于标识方法需要缓存 value/cacheNames:表示方法缓存所属的命名空间(可用类注解@CacheConfig的cacheNames代替,进行统一管理) key:缓存名,所在命名空间内需唯一(详细:http://blog.csdn.net/fireofjava/article/details/48913335)。 自动生成: demo1() :com.jiuhongpay.weixin_service.service.MainTestServiceImpl.demo1_4sd32 末尾的 “4sd32” 分别代表id、name、age的传参 demo2() :com.jiuhongpay.weixin_service.service.MainTestServiceImpl.printBankInfo_com.jiuhongpay.weixin_service.model.BankInfo@4238646b 自定义(表达式中表示某个字段一定要用“#”号开头,不然会报错。): @Cacheable(key = "#bankInfo.bankId + #bankInfo.cardNo") demo2() :43 //实体bankInfo中的 bankId = 4,cardNo = 3 condition:根据参数控制是否需要进行缓存,例如: demo1() //id 为3 的不缓存 @Cacheable(condition = "#id != '3'") //id是string,所以匹配要有单引号 //age 为3 的不缓存 @Cacheable(condition = "#age != 3") //age是int,匹配不需要单引号 注:string 匹配值不加单引号则匹配不上,同理,Integer 匹配值加了单引号也匹配不上 demo2() //只有bankInfo.bankId 为4 并且 #bankInfo.cardNo 为'3'的进行缓存 @Cacheable(condition = "#bankInfo.bankId == 4 and #bankInfo.cardNo == '3'") 注:关系表达式可以用“and”、“or”、“&&”、“||” unless:根据返回值控制是否需要进行缓存,例如: demo3() //返回值为 "1"的不进行缓存 @Cacheable(unless = "#result == '1'") 注:#result 代表返回结果值 @CacheEvict : 用于标识删除某数据缓存 value/cacheNames:表示方法缓存所属的命名空间(必填) key:缓存名(必填) allEntries :为true时,表示全部删除。默认值为false。 例如: demo1() //进行查询的方法,需缓存 @Cacheable(key = "'bankinfo_' + #id") demo3() //进行删除的方法,对demo1()结果的缓存进行删除 @CacheEvict(value = "paomotemp", key = "'bankinfo_' + #id") @CachePut : 一般用于新增/修改方法,调用方法时会自动把相应的数据放入缓存,并每次都会请求db,用法等同于 @Cacheable ,但不会像 @Cacheable 一样,如果有缓存就直接读缓存。 类级别的注解 @CacheConfig : 用于声明命名空间 cacheNames : 一般一个实现类是一个命名空间,声明后,@Cacheable 、 @CachePut 中的 value 和 cacheNames 就可以被替代,不需要被声明 例如:demo class 中的 MainTestServiceImpl 命名规则建议: @CacheConfig cacheNames 使用每个实现类所属的接口名称,例如: @CacheConfig(cacheNames = "bankInfoService") @Cacheable value 如果使用默认的过期时间,那么不需要定义; 如果有自己特殊的自定义过期时间,则使用每个实现类所属的接口名称 + 下划线 + 时间(单位秒),例如: @Cacheable(value = "bankInfoService_10") 注:如果使用自定义的过期时间,需要去 spring cache 和 redis 的整合类中配置过期时间map。 key 方法名 + 参数,例如: @Cacheable(key = "'getById_' + #id") public BankInfo getById(Integer id);
相关文章推荐
- SpringBoot整合Redis及Redis工具类撰写
- springboot整合redis进行数据操作
- SpringBoot整合Redis(单机+集群) —— 含Spring版附件文档
- spring boot 整合redis对查询数据做缓存( 利用spring的AOP技术)
- springboot整合redis(注解形式)
- Spring Boot 整合 Redis 实现缓存操作
- Spring Boot+Shiro+Redis(redisson)整合时,采用内嵌tomcat启动错误原因分析
- spring-boot整合redis 总结
- SpringBoot开发详解(十一) -- Redis在SpringBoot中的整合
- SpringBoot整合redis哨兵主从服务
- SpringBoot整合Redis、ApachSolr和SpringSession的示例
- SpringBoot开发详解(十一) -- Redis在SpringBoot中的整合
- Java爬虫初体验:简单抓取IT之家热评(整合Spring Boot+Elasticsearch+Redis+Mybatis)
- Spring Boot 整合 Redis 实现缓存操作
- Springboot+redis 整合
- SpringBoot系列十:SpringBoot整合Redis
- Spring Boot+Shiro+Redis(redisson)整合时,采用内嵌tomcat启动错误原因分析
- SpringBoot(十二)-- 整合Redis
- springboot整合redis进行数据操作(推荐)
- Spring boot整合redis小例子