当前位置:网站首页>二 RedisTemplate的序列和反序列化机制讲解
二 RedisTemplate的序列和反序列化机制讲解
2022-07-28 19:46:00 【橘子ꦿ.๓】
RedisTemplate的序列和反序列化机制讲解
上集问题
同个key为啥获取不到值,核心就是序列化机制导致key不一样
什么是序列化
把对象转换为字节序列的过程称为对象的序列化。
把字节序列恢复为对象的过程称为对象的反序列化。
对象的序列化主要有两种用途
把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中
在网络上传送对象的字节序列。
Redis为什么要序列化
性能可以提高,不同的序列化方式性能不一样
可视化工具更好查看
采用默认的jdk方式会乱码(POJO类需要实现Serializable接口)
采用JSON方式则不用,且可视化工具更好查看
自定义redis序列化方式,提供了多种可选择策略
JdkSerializationRedisSerializer
POJO对象的存取场景,使用JDK本身序列化机制
默认机制 ObjectInputStream/ObjectOutputStream进行序列化操作
StringRedisSerializer
Key或者value为字符串
Jackson2JsonRedisSerializer
利用jackson-json工具,将pojo实例序列化成json格式存储
GenericFastJsonRedisSerializer
另一种javabean与json之间的转换,同时也需要指定Class类型
代码测试 ,如果一个类不实现Serializable则无法存进redis中,实现后才能存进去,去RDM看是乱码
#测试
public class User implements Serializable {
private Integer id;
private String name;
}
@SpringBootTest
class SpringbootRedisApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void contextLoads() {
}
@Test
public void testSet(){
// redisTemplate.opsForValue().set("name1","aaaaaaaaaaa");
User user=new User();
user.setId(1);
user.setName("admin");
redisTemplate.opsForValue().set("user:1",user);
}
边栏推荐
- 面向千元级5G手机市场,联发科天玑700发布
- Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
- Confession of a graduate student: why am I addicted to opengauss community?
- MySQL
- The ref value ‘xxx‘ will likely have changed by the time this effect function runs. If this ref......
- Capture video by buffering
- Kubeadm搭建kubernetes集群
- 牛客打开摄像头几秒后画面消失 | 相机打开画面一闪一闪
- 微服务架构下的系统集成
- 八、QOS队列调度与报文丢弃
猜你喜欢

Young freshmen yearn for more open source | here comes the escape guide from open source to employment!

What functions does MySQL have? Don't look everywhere. Just look at this.

Buuctf questions upload labs record pass-01~pass-10

MySQL 是如何归档数据的呢?

八、QOS队列调度与报文丢弃

Maxwell is an easy-to-use software for capturing MySQL data in real time

Applet container technology improves mobile R & D efficiency by 500%

BUUCTF做题Upload-Labs记录pass-01~pass-10

Sharkteam completes the safety audit of flow ecological NFT market matrixmarket

CVPR 2022 | 网络中批处理归一化估计偏移的深入研究
随机推荐
[Zhou Zhou has a prize] cloud native programming challenge "edge container" track invites you to fight!
面向千元级5G手机市场,联发科天玑700发布
Automatic filling of spare parts at mobile end
Jiuxin intelligence officially joined opengauss community
Paging function (board)
百度搜索符合预期,但涉及外链黑帽策略,什么原因?
Analysis of critical path
C#流程控制语句
Guanghetong & Qualcomm Internet of things technology open day successfully held
云安全核心技术
Redis cache avalanche, cache penetration, cache breakdown
【Bluetooth蓝牙开发】八、BLE协议之传输层
The 35 required questions in MySQL interview are illustrated, which is too easy to understand
Nacos principle
Pytorch学习记录(四):过拟合、卷积神经网络CNN
What is low code? Which platforms are suitable for business personnel? Is it reliable to develop the system?
Four methods of multi-threaded sequential operation. Ask casually during the interview
LeetCode链表问题——142.环形链表II(一题一文学会链表)
Confession of a graduate student: why am I addicted to opengauss community?
移动端空余部位自动填充