SpringBoot缓存篇Ⅱ --- 整合Redis以及序列化机制
一.Redis环境搭建
系统默认是使用ConcurrentMapCacheManager,然后获取和创建ConcurrentMapCache类型的缓存组件,再将数据保存在ConcurrentMap中
开发中使用缓存中间件:redis,memcached,ehcache
1.搭建redis环境
在linux上安装redis(推荐使用docker)。docker安装redis的技巧:使用国内镜像可以加速下载。
docker pull registry.docker-cn.com/library/redis
2.使用docker启动redis
docker run -p 6379:6379 --name myredis -d registry.docker-cn.com/library/redis
3.引入redis的starter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
4.配置redis
spring.redis.host=172.**.**.**
二.RedisTemplate的使用
SpringBoot底层整合了spring-data-redis,里面的StringRedisTemplate以及RedisTemplate都已经注入到容器中,使用的时候直接从容器中取出来即可。其中StringRedisTemplate封装了redis对字符串的一些常用操作,RedisTemplate封装了一些对象的常用操作。
1.StringRedisTemplate的使用
public void testStringRedis() {
//redis保存数据
stringRedisTemplate.opsForValue().append("msg","hello");
//读取数据
String msg = stringRedisTemplate.opsForValue().get("msg");
System.out.println(msg);
//list存储数据
stringRedisTemplate.opsForList().leftPush("mylist","1");
stringRedisTemplate.opsForList().leftPush("mylist","2");
stringRedisTemplate.opsForList().leftPush("mylist","3");
String mylist = stringRedisTemplate.opsForList().leftPop("mylist"); //删除并查询最顶层的list数据
System.out.println(mylist);
}
2.RedisTemplate的使用
public void testRedis() {
employeeRedisTemplate.opsForValue().set("emp-02",employeeMapper.getEmployeeById(2));
}
使用set方法保存查询到的员工对象,执行完毕后发现有错误,这是因为Emp对象没有被序列化,没有序列化的对象是无法存入redis数据库。所以需要Emp实体类实现Serializable接口,将对象序列化再次执行发现有数据存储到数据库中,但是是以序列化的方式存储的。

这样存储数据是有了,但是存在数据库里很不直观,查询数据的人无法知道自己存了什么数据进去,那么如何解决序列化对象的问题呢?
redis存取序列化对象的解决方式
方式一:将数据以json形式保存,将对象转为json,转成json对象后,就会以json的形式存储到数据库中。
方式二:改变默认的序列化规则,由于默认使用jdk的序列化器,切换使用json的序列化器即可解决序列化问题
@Configuration
public class MyRedisConfig {
@Bean
public RedisTemplate<Object, Employee> empRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Employee> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer<Employee> serializer = new Jackson2JsonRedisSerializer(Employee.class);
template.setDefaultSerializer(serializer);
return template;
}
}
使用的时候注入该类,使用这个类来调用set方法即可将Emp对象存到数据库里
@Autowired
RedisTemplate<Object, Employee> employeeRedisTemplate;
public void testRedis() {
employeeRedisTemplate.opsForValue().set("emp-02",employeeMapper.getEmployeeById(2));
}
三.测试Redis缓存
1.默认使用ConcurrentMapCacheManager缓存组件来实际给缓存中存取数据。引入redis的starter之后,容器中保存的是RedisCacheManager,开启debug日志报告,可以搜索已经开启了的 org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration,原来的SimpleCacheConfiguration将不再匹配。


2.RedisCacheManager帮我们创建RedisCache来作为缓存组件,RedisCache通过操作redis来操作缓存数据,原来的缓存替换为redis缓存,注解配置都一样,区别是缓存的内容都存到配置好的redis数据库了。
3.默认保存数据k-v都是object,利用序列化保存,所以需要反序列化,将其保存为json
1)、引入了redis的starter,cacheManager变为RedisCacheManager
2)、默认创建的RedisCacheManager操作redis的时候使用的是RedisTemplate<Object,Object>
3)、RedisTemplate<Object,Object>默认使用jdk的序列化机制,所以会乱码
4)、自定义CacheManager(springboox1.x的版本和这个有区别,这边给出的是2.x的例子)
//容器会自动检测到这个CacheManager,并替换原来自带的CacheManager
@Primary //若配置多个缓存管理器需要有一个默认的缓存管理器
@Bean
public RedisCacheManager myCacheManager(RedisConnectionFactory redisConnectionFactory){
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
//.entryTtl(Duration.ofHours(1)); // 设置缓存有效期一小时
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om); // 配置序列化(解决乱码的问题)
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues(); RedisCacheManager cacheManager = RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(config)
.build();
return cacheManager;
}
注意多个缓存管理器时,若需要引入缓存管理器可以在类注解上@CacheConfig(cacheNames = "emp",cacheManager = "myCacheManager") 配置。
使用编码的方式进行缓存
上面讲的都是采用注解的方式进行缓存的,实际生产过程中也可以采用编码的方式进行缓存。
1) 注入缓存管理器
@Qualifier("myCacheManager")
@Autowired
RedisCacheManager myCacheManager;
2) 编码缓存
public Department getDept(Integer id){
Department department = departmentMapper.getDepartmentById(id);
Cache dept = myCacheManager.getCache("dept"); //获取某个缓存
dept.put("dept:1",department);
return departmentMapper.getDepartmentById(id);
}
SpringBoot缓存篇Ⅱ --- 整合Redis以及序列化机制的更多相关文章
- 实例讲解Springboot以Template方式整合Redis及序列化问题
1 简介 之前讲过如何通过Docker安装Redis,也讲了Springboot以Repository方式整合Redis,建议阅读后再看本文效果更佳: (1) Docker安装Redis并介绍漂亮的可 ...
- Spring整合Redis&JSON序列化&Spring/Web项目部署相关
几种JSON框架用法和效率对比: https://blog.csdn.net/sisyphus_z/article/details/53333925 https://blog.csdn.net/wei ...
- 【快学springboot】11.整合redis实现session共享
前言 这里都是基于前面的项目基础上的.springboot整合redis非常的方便,这也是springboot的宗旨,简化配置.这篇文章就教大家如何使用springboot整合redis来实现sess ...
- springboot 2.x整合redis,spring aop实现接口缓存
pox.xml: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- Redis缓存篇(二)淘汰机制:缓存满了怎么办?
上一讲提到,缓存的容量总是小于后端数据库的.随着业务系统的使用,缓存数据会撑满内存空间,该怎么处理呢? 本节我们来学习内存淘汰机制.在Redis 4.0之前有6种内存淘汰策略,之后又增加2种,一共8种 ...
- SpringBoot + MySQL + MyBatis 整合 Redis 实现缓存操作
本地安装 Redis Redis 安装:https://www.cnblogs.com/oukele/p/11373052.html 项目结构: SpringBootRedis 工程项目结构如下: ...
- SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件
原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...
- SpringBoot消息篇Ⅲ --- 整合RabbitMQ
知识储备: 关于消息队列的基本概念我已经在上一篇文章介绍过了(传送门),本篇文章主要讲述的是SpringBoot与RabbitMQ的整合以及简单的使用. 一.安装RabbitMQ 1.在linux上 ...
- SpringBoot分布式篇Ⅷ --- 整合SpringCloud
SpringCloud是一个分布式的整体解决方案.Spring Cloud为开发者提供了在分布式系统(配置管理,服务发现,熔断,路由,微代理,控制总线,一次性token,全局锁,leader选举.分布 ...
随机推荐
- Raid相关操作与注意事项记录
Raid相关操作与注意事项 Raid5 SATA盘组成的Raid5,在保护数据的前提下达到高性能: RAID要有BBU Current Cache Policy采用WriteBack,No Write ...
- Kubernetes Clusters
1. 创建集群 Kubernetes集群 Kubernetes协调一个高可用的计算机集群,作为一个单独的单元来一起工作.有了这种抽象,在Kubernetes中你就可以将容器化的应用程序部署到集群中, ...
- eclipse中部署web项目时报错java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener的解决方法
解决方案: 1.右键点击项目--选择Properties,选择Deployment Assembly,在右边点击Add按钮,在弹出的窗口中选择Java Build Path Entries 2.点击N ...
- CodeForces - 1228D
乍一看,嗯,图论题,不错: 结果,这尼玛是模拟???? 传送链接:https://codeforces.com/contest/1228/problem/D 看了大佬的代码瞬间就明白了许多!!! #i ...
- 基础之Lamada和Stream的邂逅
show me the code and take to me,做的出来更要说的明白 GitHub项目JavaHouse同步收录 喜欢就点个赞呗! 你的支持是我分享的动力! 引入 是否有遇到看不懂身边 ...
- C++版本的UnEscape 解析\uxxxx\uxxxx编码字符
解析类似于这种Unicode编码格式的字符串 \u5b55\u5987\u88c5\u590f\u88c52018\u65b0\u6b3e\u5bbd\u677e\u77ed\u8896\u4e2d\ ...
- 洛谷$P4249\ [WC2007]$剪刀石头布 网络流
正解:网络流 解题报告: 传送门$QwQ$ 题目大意其实就说有一个$n$个节点的有向完全图,然后部分边的方向已经给定了,要求确定所有边的方向使三元环数目有$max$.这里三元环的定义是说三条边的方向一 ...
- map类型转为实体类
BareBaseRequest fromJson = JSON.parseObject(JSON.toJSONString(map), BareBaseRequest.class);
- 【接口测试】使用httpClient获取cookies+携带获取的cookies访问get接口
数据准备 在本机或者远端机器安装部署moco-runner(参考:https://blog.csdn.net/qq_32706349/article/details/80472445) 这里我们只需要 ...
- 剑指Offer-60~68题
60. \(n\) 个骰子的点数 题目描述: 扔 \(n\) 个骰子,向上面的数字之和为 \(S\).给定 \(n\),请列出所有可能的 \(S\) 值及其相应的概率. 示例: 输入:n = 1 输出 ...