【SpringBoot】springboot -- 2.0版本自定义ReidsCacheManager的改变
1. 问题发现
在1.0版本中,我们配置redis的cacheManager是这种方式:
//缓存管理器
@Bean
public CacheManager cacheManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
//设置缓存过期时间
cacheManager.setDefaultExpiration(10000);
return cacheManager;
} //缓存管理器
然而在2.0版本中,这个代码直接报错,原因是RedisCacheManager取消了1.0版本中的public RedisCacheManager(RedisOperations redisOperations)
的这个构造方法,所以我们无法再用RedisTemplate
作为参数来自定义CacheManager
。
下面看一看两个版本的差别:
1.0 版本的CacheManager构造器
![](http://upload-images.jianshu.io/upload_images/5786888-4b9cd30262f4cec9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/613/format/webp)
2.0 版本的CacheManager构造器
![](http://upload-images.jianshu.io/upload_images/5786888-0b8cd322165452c1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/961/format/webp)
RedisCacheWriter
提供了对Redis的set、setnx、get等命令的访问权限,可以由多个缓存实现共享,并负责写/读来自Redis的二进制数据。
RedisCacheConfiguration
根据名字都能想到它是提供redis的配置。
2. springboot2.0 中 CacheManager自定义配置
/**
* 缓存管理器
*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
//初始化一个RedisCacheWriter
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
//设置CacheManager的值序列化方式为json序列化
RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer();
RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair
.fromSerializer(jsonSerializer);
RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(pair);
//设置默认超过期时间是30秒
defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
//初始化RedisCacheManager
return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
}
上面的代码中,还设置了CacheManager
的值序列化方式,所以有了这个配置,可以直接在注解的形式中实现json的redis存储而不用再去多写配置。
作者:FantJ
链接:https://www.jianshu.com/p/20366ecf12ce
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
【SpringBoot】springboot -- 2.0版本自定义ReidsCacheManager的改变的更多相关文章
- springboot -- 2.0版本自定义ReidsCacheManager的改变
1. 问题发现 在1.0版本中,我们配置redis的cacheManager是这种方式: //缓存管理器 @Bean public CacheManager cacheManager(@Suppres ...
- SpringBoot入门(五)——自定义配置
本文来自网易云社区 大部分比萨店也提供某种形式的自动配置.你可以点荤比萨.素比萨.香辣意大利比萨,或者是自动配置比萨中的极品--至尊比萨.在下单时,你并没有指定具体的辅料,你所点的比萨种类决定了所用的 ...
- (30)导入时如何定制spring-boot依赖项的版本【转载】【从零开始学Spring Boot】
此文章转载地址:http://www.tuicool.com/articles/RJJvMj3 请注重作者的版权. spring-boot通过maven的依赖管理为我们写好了很多依赖项及其版本,我们可 ...
- springboot集成activiti6.0多数据源的配置
最近公司开始开发springboot的项目,需要对工作流进行集成.目前activiti已经发布了7.0的版本,但是考虑到6.0版本还是比较新而且稳定的,决定还是选择activiti6.0的版本进行集成 ...
- springboot - 返回xml error 从自定义的 ErrorController
1.概览 2.在<springboot - 返回JSON error 从自定义的 ErrorController>基础上,做如下调整: 1).新增Attribute类和Error类 pac ...
- 【原创】SpringBoot 2.7.0通过lettuce及commons-pool2 v2.9.0集成Redis踩坑记录
背景 公司的一个项目由于HTTPS证书到期,导致小程序.POS不能正常使用.所以百度了下,通过URL检测证书有效期的代码,并自行整合到一个服务中. 代码仓库:[基于SpringBoot + 企业微信 ...
- SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1
在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...
- nginx-1.12.0版本(编译安装)-自定义安装路径
nginx-1.12.0版本(编译安装)-自定义安装路径 安装路径:/application/nginx-1.12.0 1.前期准备 安装编译需要的gcc和gcc-c++ yum install -y ...
- springboot + 拦截器 + 注解 实现自定义权限验证
springboot + 拦截器 + 注解 实现自定义权限验证最近用到一种前端模板技术:jtwig,在权限控制上没有用springSecurity.因此用拦截器和注解结合实现了权限控制. 1.1 定义 ...
随机推荐
- SpringBoot 请求参数后端校验
1.例如: package com.model.user; import com.model.PageEntity;import lombok.Getter;import lombok.Setter; ...
- 论文阅读笔记四十六:Feature Selective Anchor-Free Module for Single-Shot Object Detection(CVPR2019)
论文原址:https://arxiv.org/abs/1903.00621 摘要 本文提出了基于无anchor机制的特征选择模块,是一个简单高效的单阶段组件,其可以结合特征金字塔嵌入到单阶段检测器中. ...
- centos/linux/ubuntu在局域网上网
前言:对于服务器来说,一般不会安装windowns系统,都是会安装类unix系统,在局域网或者在内网中,上网还是走代理上网 1.知道代理服务器的ip及端口 2.就两条命令 export http_p ...
- SqlServer数据库重命名报错误:5030
无法重命名 KLENN 无法用排他锁锁定该数据库,以执行该操作(错误:5030) 解决办法: 将数据库设置为单用户模式 (单用户模式指定一次只有一个用户可访问数据库,该模式通常用于维护操作. ) 1. ...
- MATLAB读视频报错 Unable to initialize the video obtain properties (videoreader in Matlab)
这个bug卡了半天,这里记录一下 Error using VideoReader/init (line ) Could not read file due to an unexpected error ...
- eclipse查看一个方法被谁引用(调用)的快捷键四种方式
1.(首推)双击选中该方法,Ctrl+Alt+H 如果你想知道一个类的方法到底被那些其他的类调用,那么请选中这个方法名,然后按“Ctrl+Alt+H”, Eclipse就会显示出这个方法被哪些方法调用 ...
- 使用Type.MakeGenericType,反射构造泛型类型
有时我们会有通过反射来动态构造泛型类型的需求,该如何实现呢?举个栗子,比如我们常常定义的泛型委托Func<in T, out TResult>,当T或TResult的类型需要根据程序上下文 ...
- php接入支付宝的流程(转载)
php接入支付宝的流程写在这里供像我一样的小白参考. 1.首先要有一个创建一个应用(选好自己想要的功能,关于支付的功能,貌似都需要签约) 2.下载SDK&Dome(网址https://doc. ...
- react和redux版本不匹配
1.页面报错Cannot read property 'shape' of undefined 2. 原因为:react版本与react-redux版本不匹配. 1.package.json文件修改该 ...
- [Doc]MongoDB用户创建与启用access-control
文档链接:https://docs.mongodb.com/manual/tutorial/enable-authentication/ Pre 个人总感觉数据库的文档结构不太友好, 不太解决问题.以 ...