当前位置:网站首页>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
边栏推荐
- New book recommendation - IPv6 technology and application (Ruijie version)
- Consider defining a bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs‘
- Introduction to micro build low code zero Foundation (lesson 2)
- How to call out services in idea and display the startup class in services
- Tp6 Alibaba Cloud SMS Window message Curl Error 60: SSL Certificate Problem: Unable to get local issuer Certificate
- tp6阿里云短信 window 报 cURL error 60: SSL certificate problem: unable to get local issuer certificate
- go语言打怪通关之 ⌈互斥锁和状态协程⌋
- Shardingsphere read write separation
- LeetCode 283. Move zero (simple, array) Day12
- 在使用代理IP前需要了解哪些分类?
猜你喜欢

Realize linear regression with tensorflow (including problems and solutions in the process)

Leetcode39 combined sum

Want to experience homekit smart home? Why don't you take a look at this smart ecosystem

How to set computer IP?

How to write the resume of Software Test Engineer so that HR can see it?

每日一题(2022-04-22)——旋转函数

想体验HomeKit智能家居?不如来看看这款智能生态

What is a proxy IP pool and how to build it?

【Chrome扩展程序】content_script的跨域问题

如何设置电脑ip?
随机推荐
PTA: 点赞狂魔
Applet reads files
搭建网站是用物理机还是云主机好?
What is a proxy IP pool and how to build it?
[NK] Niuke monthly race 48 D
LeetCode 447. Number of boomerangs (permutation and combination problem)
想体验HomeKit智能家居?不如来看看这款智能生态
Arduino esp8266 network upgrade OTA
Applet canvas canvas half ring
How to write the resume of Software Test Engineer so that HR can see it?
Shardingsphere read write separation
How to choose a good dial-up server?
Gray scale range corresponding to colors (red, yellow, green, blue, purple, pink, brick red and magenta) in HSV color space
006_redis_jedis快速入门
[Dahua cloud native] micro service chapter - service mode of five-star hotels
我国科学家揭示突破水稻产量瓶颈新机制
go语言打怪通关之 ⌈互斥锁和状态协程⌋
Tp6 Alibaba Cloud SMS Window message Curl Error 60: SSL Certificate Problem: Unable to get local issuer Certificate
[hands on learning] network depth v2.1 Sequence model
Hyperscan -- 2 compilation