当前位置:网站首页>009_ Redis_ Getting started with redistemplate
009_ Redis_ Getting started with redistemplate
2022-04-23 02:08:00 【_ yummy_】
009_Redis_RedisTemplate introduction
1、 Create a springboot project
springboot The advantages of injecting dependency are slow ! sleepy , Well intentioned people know how to create springboot How to make the import dependency faster !!!!!! Please let me know !

Write the configuration file .
spring:
redis:
host: 10.223.31.215
port: 6379
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: 100ms
Writing test classes
package com.ym.redisdemo;
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;
@SpringBootTest
class RedisDemoApplicationTests {
// Inject Redis
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
// Write a piece of String data
redisTemplate.opsForValue().set("name"," Li Yifeng ");
// obtain String data
Object name = redisTemplate.opsForValue().get("name");
System.out.println("name="+name);
}
}
unit testing

2、SpringDataRedis How to use
- Introduce dependencies
- To configure redis Address information
- Local injection RedisTemplate
3、 Query the just inserted... In the client tool String data

The data is serialized when stored , Default JDK Serialization mode is stored .
4、SpringDataRedis How to serialize
RedisTemplate You can accept any Object Write... As a value Redis, It's just that before writing Object Objects are sequenced into bytes , The default is JDK serialize , That is the result we see in the figure above . Its disadvantages are as follows .
- Poor readability
- Large memory consumption
5、 Serialization operation
package com.ym.redisdemo.com.ym.redis.config;
import lombok.val;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
/** * @author: LYM * @description redis Configuration class * @version: V1.0 * @date: 2022/4/20 23:34 */
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){
// Create objects
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
// Set up connection factory
template.setConnectionFactory(connectionFactory);
// Set up key Serialization
GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer=new GenericJackson2JsonRedisSerializer();
template.setKeySerializer(RedisSerializer.string());
template.setHashKeySerializer(RedisSerializer.string());
// Set up value Serialization
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashKeySerializer(jackson2JsonRedisSerializer);
// return
return template;
}
}

Test object
@Test
void testSaveUser(){
// Write data
redisTemplate.opsForValue().set("user:100",new User(" Li Yifeng ",18));
User o = (User) redisTemplate.opsForValue().get("user:100");
System.out.println(o);
}


版权声明
本文为[_ yummy_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230202506572.html
边栏推荐
- Tp6 Alibaba cloud SMS window reports curl error 60: SSL certificate problem: unable to get local issuer certificate
- Makefile文件是什麼?
- 什么是api接口?
- 什么是bgp服务器,有哪些优势?
- PTA: 点赞狂魔
- How to call out services in idea and display the startup class in services
- 今天终于会写System.out.println()了
- 【汇编语言】从最底层的角度理解“堆栈”
- Esp32 message queue using FreeRTOS
- Analyze the advantages and disadvantages of tunnel proxy IP.
猜你喜欢

浅析一下隧道代理IP的优缺点。

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

012_ Access denied for user ‘root‘@‘localhost‘ (using password: YES)

009_Redis_RedisTemplate入门

Applet canvas canvas half ring

什么是bgp服务器,有哪些优势?

Dynamic memory management

What categories do you need to know before using proxy IP?

What is BGP server and what are its advantages?

How does Axure set the content of the text box to the current date when the page is loaded
随机推荐
世界读书日 | 技术人不要错过的好书(IT前沿技术)
arduino esp8266 网络升级 OTA
How to choose a good dial-up server?
[leetcode daily question] 396 Rotation function
What problems will you encounter when dialing VPS?
浅析一下隧道代理IP的优缺点。
leetcode:27. Remove element [count remove]
PTA: Romantic reflection [binary tree reconstruction] [depth first traversal]
[hands on learning] network depth v2.1 Sequence model
Lane cross domain problem
Shardingsphere sub database and sub table
002_Redis_String类型常见的操作命令
PTA: praise the crazy devil
Makefile文件是什么?
小程序 canvas 画布半圆环
Heap overflow of kernel PWN basic tutorial
Echo "new password" |passwd -- stdin user name
EBS:PO_ EMPLOYEE_ HIERARCHIES_ ALL
89 logistic回归用户画像用户响应度预测
PTA: 点赞狂魔