当前位置:网站首页>010_ StringRedisTemplate
010_ StringRedisTemplate
2022-04-23 02:08:00 【_ yummy_】
010_StringRedisTemplate
The above disadvantage is that it will use memory space to occupy bytecode
1、 To save memory space , We don't use it JSON Serialization to handle value, Instead, use a unified String Serializer , It is required that only String Type of key and value.
2、 When you want to store Java When the object , Manually complete the serialization and deserialization of objects
3、Spring One is provided in StringRedisTemplate class , its key as well as value All default to String The way . It eliminates our custom RedisTemplate The process of .
The code tests are as follows .
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 {
// Inject Redis
@Autowired
private StringRedisTemplate stringRedisTemplate;
/** * Store string test */
@Test
void contextLoads() {
// Write a piece of String data
stringRedisTemplate.opsForValue().set("lover"," Li Yifeng ");
// obtain String data
Object name = stringRedisTemplate.opsForValue().get("lover");
System.out.println("lover="+name);
}
private static final ObjectMapper mapper=new ObjectMapper();
/** * Storage object test */
@Test
void testSaveUser() throws JsonProcessingException {
// Create the object first
User user=new User(" Li Yifeng ",21);
// Manual serialization
String json = mapper.writeValueAsString(user);
// Write data
stringRedisTemplate.opsForValue().set("user:101",json);
// get data
String o = stringRedisTemplate.opsForValue().get("user:101");
// Manual deserialization
User user1 = mapper.readValue(o, User.class);
System.out.println(user1);
}
}
summary :
About RedisTemplate Two serialization practices
- programme 1:
(1) Customize RedisTemplate
(2) modify RedisTemplate serializers GenericJackson2JsonRedisSerializer - programme 2:
(1) Use StringRedisTemplate
(2) write in Redis when , You need to manually serialize the object to Json
(3) Read Redis when , Read manually Json Deserialize to object
版权声明
本文为[_ yummy_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230202506531.html
边栏推荐
- leetcode:27. Remove element [count remove]
- Unicorn bio raised $3.2 million to turn prototype equipment used to grow meat into commercial products
- What businesses use physical servers?
- Shardingsphere sub database and sub table
- 从开源爱好者到 Apache 董事,一共分几步?
- arduino esp8266 网络升级 OTA
- 2018 China Collegiate Programming Contest - Guilin Site J. stone game
- 013_基于Session实现短信验证码登录流程分析
- Heap overflow of kernel PWN basic tutorial
- 010_StringRedisTemplate
猜你喜欢
Thinkphp内核开发盲盒商城源码v2.0 对接易支付/阿里云短信/七牛云存储
Introduction to micro build low code zero Foundation (lesson 2)
想体验HomeKit智能家居?不如来看看这款智能生态
Gray scale range corresponding to colors (red, yellow, green, blue, purple, pink, brick red and magenta) in HSV color space
有哪些业务会用到物理服务器?
动态代理ip的测试步骤有哪些?
代理IP可用率是不是等同于代理IP的效率?
App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]
006_redis_SortedSet类型
拨号服务器是什么,有什么用处?
随机推荐
想体验HomeKit智能家居?不如来看看这款智能生态
Multithreading technology core
Today will finally write system out. Println()
007_Redis_Jedis连接池
Quel est le fichier makefile?
What business scenarios will the BGP server be used in?
Tp6 Alibaba Cloud SMS Window message Curl Error 60: SSL Certificate Problem: Unable to get local issuer Certificate
R language advanced | generalized vector and attribute analysis
011_RedisTemplate操作Hash
Easyswool environment configuration
假如404页面是这样的 | 每日趣闻
Go language ⌈ mutex and state coordination ⌋
JDBC cannot connect to MySQL, and the error is access denied for user 'root' @ '* * *' (using password: Yes)
Talk about biology live broadcast: Dr. Wang Ziyuan, a lake view biology, exploring hepatitis B with gene therapy
leetcode:27. Remove element [count remove]
Common formatting problems after word writing
Makefile文件是什麼?
世界读书日 | 技术人不要错过的好书(IT前沿技术)
Hyperscan -- 2 compilation
What are the common proxy IP problems?