当前位置:网站首页>二 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);
}
边栏推荐
- 百度搜索符合预期,但涉及外链黑帽策略,什么原因?
- C # detailed steps for connecting to MySQL database
- Pytorch学习记录(四):过拟合、卷积神经网络CNN
- Ijcai2022 tutorial | dialogue recommendation system
- ctfshow 网络迷踪做题记录(2)
- Bus, protocol, specification, interface, data acquisition and control system in industrial communication field
- The development of smart home industry pays close attention to edge computing and applet container technology
- Reading and writing basic data types in protobuf
- 百度搜索为什么只抓取,却不展现页面?
- 微服务架构下的系统集成
猜你喜欢

Cloud security core technology

Quii Cordova plugin telerik imagepicker plug-in multi image upload out of sequence
![[input ID number] is replaced by an asterisk, and input is cut into multiple small squares (similar)](/img/f0/7e3ea94e02a42b6055c40b58d1e39c.png)
[input ID number] is replaced by an asterisk, and input is cut into multiple small squares (similar)

实现瀑布流效果

Attribute based encryption simulation and code implementation (cp-abe) paper: ciphertext policy attribute based encryption

LeetCode链表问题——142.环形链表II(一题一文学会链表)

How to understand data mesh

Database -- use of explain

Several skills of API interface optimization

CVPR 2022 | 网络中批处理归一化估计偏移的深入研究
随机推荐
How NPM switches Taobao source images
The greatest romance of programmers~
Cloud security core technology
Introduction to blue team: efficiency tools
[tidb] importing TXT documents into the database is really efficient
Paging function (board)
C#流程控制语句
Why on earth is it not recommended to use select *?
C#连接MySql数据库详细步骤
Ctfshow network lost track record (2)
属性基加密仿真及代码实现(CP-ABE)论文:Ciphertext-Policy Attribute-Based Encryption
职场高薪 |「中高级测试」面试题
不用Swagger,那我用啥?
(PMIC)全、半桥驱动器CSD95481RWJ PDF 规格
华为发布首款电驱动系统DriveONE:充电10分钟续航200km
Achieve waterfall effect
SSM-使用@Async和创建ThreadPoolTaskExecutor线程池
微服务架构下的系统集成
What is ci/cd| Achieve faster and better software delivery
What is low code? Which platforms are suitable for business personnel? Is it reliable to develop the system?