当前位置:网站首页>009_Redis_RedisTemplate入门
009_Redis_RedisTemplate入门
2022-04-23 02:02:00 【_yummy_】
009_Redis_RedisTemplate入门
1、创建一个springboot项目
springboot注入依赖优点慢呀!困了,有好心人知道创建springboot怎么样才能使导入依赖快一点呀!!!!!!求告知!
写一下配置文件。
spring:
redis:
host: 10.223.31.215
port: 6379
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: 100ms
编写测试类
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 {
//注入Redis
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
//写入一条String数据
redisTemplate.opsForValue().set("name","李易峰");
//获取String数据
Object name = redisTemplate.opsForValue().get("name");
System.out.println("name="+name);
}
}
单元测试
2、SpringDataRedis的使用步骤
- 引入依赖
- 配置redis地址信息
- 本地注入RedisTemplate
3、在客户端工具查询刚刚插入的String数据
数据在存储的时候被序列化了,默认JDK序列化方式被存储。
4、SpringDataRedis的序列化方式
RedisTemplate可以接受任意Object作为值写入Redis,只不过写入之前会把Object对象序列化成字节的形式,默认采用的是JDK序列化,也就是上图我们看到的结果。其存在的缺点如下。
- 可读性差
- 内存占用较大
5、序列化操作
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配置类 * @version: V1.0 * @date: 2022/4/20 23:34 */
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){
//创建对象
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
//设置连接工厂
template.setConnectionFactory(connectionFactory);
//设置key的序列化
GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer=new GenericJackson2JsonRedisSerializer();
template.setKeySerializer(RedisSerializer.string());
template.setHashKeySerializer(RedisSerializer.string());
//设置value的序列化
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashKeySerializer(jackson2JsonRedisSerializer);
//返回
return template;
}
}
测试对象
@Test
void testSaveUser(){
//写入数据
redisTemplate.opsForValue().set("user:100",new User("李易峰",18));
User o = (User) redisTemplate.opsForValue().get("user:100");
System.out.println(o);
}
版权声明
本文为[_yummy_]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43890515/article/details/124309552
边栏推荐
- Longest common subsequence (record path version)
- 一些使用代理IP的小技巧。
- 使用代理IP是需要注意什么?
- Error in face detection and signature of Tencent cloud interface
- Shardingsphere introduction and sub table usage
- Network jitter tool clumsy
- Dynamic batch processing and static batch processing of unity
- PID refinement
- keil mdk中文乱码,两种解决方法,字体不再难看
- A simple and open source navigation website source code
猜你喜欢
2022 low voltage electrician examination questions and answers
Nanny level tutorial on building personal home page (II)
The sixth season of 2022, the perfect children's model IPA national race leads the yuanuniverse track
Leetcode40 - total number of combinations II
Leetcode 112 Total path (2022.04.22)
什么是api接口?
[hands on learning] network depth v2.1 Sequence model
拨号服务器是什么,有什么用处?
FL studio20. 8 the latest Chinese version installation and download graphic tutorial
Cc2541 emulator CC debugger tutorial
随机推荐
What categories do you need to know before using proxy IP?
2022.4.20-----leetcode.388
How to classify proxy IP?
About how to import C4d animation into lumion
配置iptables实现本地端口转发的方法详解
Shardingsphere sub database and sub table
easyswoole环境配置
Use of j-link RTT
[hands on learning] network depth v2.1 Sequence model
Is the sinking coffee industry a false prosperity or the eve of a broken situation?
The leader / teacher asks to fill in the EXCEL form document. How to edit the word / Excel file on the mobile phone and fill in the Excel / word electronic document?
keil mdk中文乱码,两种解决方法,字体不再难看
Implementation of Base64 encoding / decoding in C language
C语言中如何“指名道姓”的进行初始化
postman里面使用 xdebug 断点调试
Use of push() and pop()
App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]
How to change the size of SVG pictures without width in openlayer
Esp32 message queue using FreeRTOS
校园转转二手市场源码