当前位置:网站首页>010_StringRedisTemplate
010_StringRedisTemplate
2022-04-23 02:02:00 【_yummy_】
010_StringRedisTemplate
上面的缺点就是会使用内存空间去占用字节码
1、为了节省内存空间,我们并不会使用JSON序列化来处理value,而是使用统一的String序列化器,要求只能存储String 类型的key和value。
2、当要存储Java对象的时候,手动完成对象的序列化以及反序列化
3、Spring中提供了一个StringRedisTemplate类,它的key以及value都是默认为String方式。省去了我们自定义RedisTemplate的过程。
代码测试如下。
package com.ym.redisdemo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ym.redisdemo.pojo.User;
import lombok.val;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
@SpringBootTest
class RedisStringTests {
//注入Redis
@Autowired
private StringRedisTemplate stringRedisTemplate;
/** * 存储字符串测试 */
@Test
void contextLoads() {
//写入一条String数据
stringRedisTemplate.opsForValue().set("lover","李易峰");
//获取String数据
Object name = stringRedisTemplate.opsForValue().get("lover");
System.out.println("lover="+name);
}
private static final ObjectMapper mapper=new ObjectMapper();
/** * 存储对象测试 */
@Test
void testSaveUser() throws JsonProcessingException {
//先创建对象
User user=new User("李易峰",21);
//手动序列化
String json = mapper.writeValueAsString(user);
//写入数据
stringRedisTemplate.opsForValue().set("user:101",json);
//获取数据
String o = stringRedisTemplate.opsForValue().get("user:101");
//手动反序列化
User user1 = mapper.readValue(o, User.class);
System.out.println(user1);
}
}
总结:
关于RedisTemplate的两种序列化实践方案
- 方案1:
(1) 自定义RedisTemplate
(2) 修改RedisTemplate的序列化器GenericJackson2JsonRedisSerializer - 方案2:
(1) 使用StringRedisTemplate
(2) 写入Redis时,需要手动把对象序列化为Json
(3) 读取Redis时,手动把读取的Json反序列化为对象
版权声明
本文为[_yummy_]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43890515/article/details/124327664
边栏推荐
- What problems will you encounter when dialing VPS?
- Batch multiple files into one hex
- Esp32 message queue using FreeRTOS
- 单片机和4G模块通信总结(EC20)
- Leetcode 112 Total path (2022.04.22)
- What is a boolean type?
- [Dahua cloud native] micro service chapter - service mode of five-star hotels
- What is an API interface?
- 【dpdk】10. Dpdk DNS learning notes
- Longest common subsequence (record path version)
猜你喜欢
Virtual serial port function of j-link V9 using skills
Shardingsphere sub database and sub table
leetcode:27. 移除元素【count remove小操作】
How to call out services in idea and display the startup class in services
【动手学深度学习V2】循环神经网络-1.序列模型
Batch multiple files into one hex
浅析一下隧道代理IP的优缺点。
Error in face detection and signature of Tencent cloud interface
Why is one plus one equal to two
NPM yarn startup error [resolved]
随机推荐
Easyswool environment configuration
Is CICC fortune a company with CICC? Is it safe
[Leetcode每日一题]396. 旋转函数
How to configure iptables to realize local port forwarding
Some tips for using proxy IP.
J-Link RTT使用
Analyze the three functions of static proxy IP.
What should I pay attention to when using proxy IP?
Echo "new password" |passwd -- stdin user name
[tutorial] how to use GCC "zero assembly" for white whoring MDK
How to change the size of SVG pictures without width in openlayer
2022.4.22-----leetcode.396
世界读书日 | 技术人不要错过的好书(IT前沿技术)
Is the sinking coffee industry a false prosperity or the eve of a broken situation?
揭秘被Arm编译器所隐藏的浮点运算
How to call out services in idea and display the startup class in services
How to "gracefully" measure system performance
MySQL active / standby configuration binary log problem
Introduction to esp32 Bluetooth controller API
Analyze the advantages and disadvantages of tunnel proxy IP.