当前位置:网站首页>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
边栏推荐
- What is a proxy IP pool and how to build it?
- Introduction to micro build low code zero Foundation (lesson 2)
- 世界读书日 | 技术人不要错过的好书(IT前沿技术)
- Startup of openstack service
- Unicorn bio raised $3.2 million to turn prototype equipment used to grow meat into commercial products
- go语言打怪通关之 ⌈互斥锁和状态协程⌋
- EBS:PO_ EMPLOYEE_ HIERARCHIES_ ALL
- LeetCode 349. Intersection of two arrays (simple, array) Day12
- 动态代理ip的测试步骤有哪些?
- [NK] Niuke monthly race 48 D
猜你喜欢
Analyze the advantages and disadvantages of tunnel proxy IP.
有哪些业务会用到物理服务器?
What categories do you need to know before using proxy IP?
They are all intelligent in the whole house. What's the difference between aqara and homekit?
89 logistic regression user portrait user response prediction
How to write the resume of Software Test Engineer so that HR can see it?
006_redis_SortedSet类型
Use Xdebug breakpoint debugging in postman
动态代理ip的测试步骤有哪些?
世界读书日 | 技术人不要错过的好书(IT前沿技术)
随机推荐
一加一为什么等于二
A simple and open source navigation website source code
Quel est le fichier makefile?
005_redis_set集合
How to configure iptables to realize local port forwarding
Esp32 message queue using FreeRTOS
如何对代理IP进行分类?
Leetcode39 combined sum
Realize linear regression with tensorflow (including problems and solutions in the process)
Unicorn bio raised $3.2 million to turn prototype equipment used to grow meat into commercial products
What should I pay attention to when using proxy IP?
PTA: 浪漫倒影 [二叉树重建] [深度优先遍历]
世界读书日 | 技术人不要错过的好书(IT前沿技术)
如何选择一台好的拨号服务器?
有哪些业务会用到物理服务器?
Redis memory recycling strategy
【Chrome扩展程序】content_script的跨域问题
[Dahua cloud native] micro service chapter - service mode of five-star hotels
[hands on learning] network depth v2.1 Sequence model
Some tips for using proxy IP.