springboot2.x 的 RedisCacheManager变化
springboot2.x 的 RedisCacheManager变化
springboot2.x 的 RedisCacheManager变化
由于最近在学着使用redis做缓存,使用的是springboot2.x来搭建的项目。
看了看网上的一些教程,但是大多数教程都是基于1.x的版本来讲解的,但是springboot2.x之后发生了一些变动,网上想找一些资料不太容易。
springboot配置缓存过期时间,网上大部分资料是使用ReidsCacheManager来进行自定义的配置
以下是大部分网上的代码(这也是基于springboot1.x的版本可以使用的)
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager cacheManager= new RedisCacheManager(redisTemplate);
cacheManager.setDefaultExpiration(60);
Map<String,Long> expiresMap=new HashMap<>();
expiresMap.put("Product",5L);
cacheManager.setExpires(expiresMap);
return cacheManager;
}
然而在springboot2.x中,RedisCacheManager已经没有了单参数的构造方法
以下是springboot2.x版本下 RedisCacheManager的大部分方法
可以发现原来1.x版本的构造方法已经没有了,新的构造方法如图所示。
本人没有看1.x的源码,我发现这里有一个RedisCacheManagerBuilder的内部类,从名字就不难发现这是一个用来构造RedisCacheManager的建造模式的应用吧。
所以以下是本人使用的RedisCacheManager的构造方法(如果定制化要求的话可以修改其中的配置就可以),这里我只设置了缓存失效时间为一小时,如需其他配置可以到RedisCacheConfiguration这个类中去寻找一下。
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofHours(1)); // 设置缓存有效期一小时
return RedisCacheManager
.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(redisCacheConfiguration).build();
}
之前的代码是这样的
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); RedisCachePrefix cachePrefix = new RedisCachePrefix() {
@Override
public byte[] prefix(String s) {
return new StringRedisSerializer().serialize("XXX:".concat(s));
}
};
cacheManager.setUsePrefix(true);
cacheManager.setCachePrefix(cachePrefix);
cacheManager.setDefaultExpiration(30 * 60);
return cacheManager;
}
springboot2.x 的 RedisCacheManager变化的更多相关文章
- 关于springboot2.x 的 RedisCacheManager变化
springboot配置缓存过期时间,大部分是使用ReidsCacheManager来进行自定义的配置 以下是大部分网上的代码(这也是基于springboot1.x的版本可以使用的) @Beanpub ...
- SpringBoot 1.X版本设置Https访问以及跨域https访问的问题
最近在做的一个项目中出现了Https域向非Https域发送ajax请求无法通过的问题 Mixed Content: The page at was loaded over HTTPS, but req ...
- springboot 2 集成 redis 缓存 序列化
springboot 缓存 为了实现是在数据中查询数据还是在缓存中查询数据,在application.yml 中将mybatis 对应的mapper 包日志设置为debug . spring: dat ...
- 本月16日SpringBoot2.2发布,有哪些变化先知晓
本月(2019年10月16日)Spring Boot 2.2已经正式发布了!在此篇文章中,将给大家介绍一下2.2版为大家带来了哪些重要的新变化.笔者用心书写,希望阅读完成之后转发关注,你的支持是我不竭 ...
- spring boot 1.x.x 到 spring boot 2.x.x 的那些变化
spring boot1 到 spring boot2的配置变化很大,迁移项目到spring boot2过程中发现以下变化 1.java 的 redis 配置添加了属性jedis 旧版 spring: ...
- Springboot2新特性概述
官方说明: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Release-Notes 起码 JDK 8 和支持 ...
- springboot2.0.3源码篇 - 自动配置的实现,发现也不是那么复杂
前言 开心一刻 女儿: “妈妈,你这么漂亮,当年怎么嫁给了爸爸呢?” 妈妈: “当年你爸不是穷嘛!‘ 女儿: “穷你还嫁给他!” 妈妈: “那时候刚刚毕业参加工作,领导对我说,他是我的扶贫对象,我年轻 ...
- springBoot2.0+redis+fastJson+自定义注解实现方法上添加过期时间
springBoot2.0集成redis实例 一.首先引入项目依赖的maven jar包,主要包括 spring-boot-starter-data-redis包,这个再springBoot2.0之前 ...
- RedisCacheManager设置Value序列化器技巧
CacheManager基本配置 请参考博文:springboot2.0 redis EnableCaching的配置和使用 RedisCacheManager构造函数 /** * Construct ...
随机推荐
- ./autogen.sh: 4: autoreconf: not found
./autogen.sh: 4: autoreconf: not found 是在不同版本的 tslib 下执行 autogen.sh 产生.它们产生的原因一样,是因为没有安装 automake ...
- linux3 源代码安装
源代码安装: 通过yum就不会有依赖关系(安装mysql是安装mysqlServer).Rpm更新的时候,如果没有安装软件就回去安装. 应用程序和系统命令会放在不同的目录中(bin,sbin,usr/ ...
- redis一些笔记
base 字典: hget/hset 在redis字典中值只能是字符串,使用渐进式进行rehash.在rehash的过程中,会保留两个hash结构:查询时会同时查询两个结构:逐渐完成hash的迁移. ...
- 10.19-10.20 test
2016 10.19-10.20 两天 题目by mzx Day1: T1:loverfinding 题解:hash #include<iostream> #include<cst ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路
题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...
- Oracle数据查看被锁住的用户
//lock_date是被锁住时间,如果为空证明这个用户没有被锁住 select username,lock_date from dba_users where username='GFMIS'; ...
- 测试工程师面试题之:给你印象最深的Bug
有人看到别人在侵淫面试技巧,什么<程序员面试宝典>,或者<面试测试工程师须知>等等,就会嗤之以鼻.他会觉得这不是“投机取巧”吗,最重要的还是踏实提高自己的能力. 非常同意这种看 ...
- 阿里云数据库产品HybridDB简介——OLAP数据库,支持行列混合存储,基于数据库Greenplum的开源版本,并且吸收PostgreSQL精髓
为什么会有HybridDB的诞生?它经历了怎样的研发历程?它的应用场景和情况是怎样的?带着这些问题,InfoQ对阿里云的数据库专家兼Postgres中国社区/中国用户会主席萧少聪先生进行了采访,以下文 ...
- win10环境变量path误删(windows找不到文件‘%windir%\systempropertiesadvanced.exe’)的解决办法
具体步骤如下:1. 首先重新启动电脑,点击开始按钮,选择电源,此时按住键盘上的shift键,再点击“重启”,选择疑难解答->再选择高级选项->再选择启动设置->然后点击重启-> ...
- [SHOI 2017] 寿司餐厅
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4873 [算法] 注意到题目中的限制条件可表述为 : 若选择区间[L , R] , 则 ...