当前位置:网站首页>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
边栏推荐
- 教程】如何用GCC“零汇编”白嫖MDK
- [Dahua cloud native] micro service chapter - service mode of five-star hotels
- Realize linear regression with tensorflow (including problems and solutions in the process)
- 在使用代理IP前需要了解哪些分类?
- Challenges often faced by client project management
- 动态代理ip的测试步骤有哪些?
- 用TensorFlow实现线性回归(包括过程中出现的问题及解决方法)
- 中金财富跟中金公司是一家公司吗,安全吗
- ESP32蓝牙Bluetooth Controller API介绍
- 拨号服务器是什么,有什么用处?
猜你喜欢

搭建网站是用物理机还是云主机好?

Esp32 message queue using FreeRTOS

What is BGP server and what are its advantages?

Cc2541 emulator CC debugger tutorial

Easyswool environment configuration

Uncover floating-point operations hidden by the ARM compiler

Basic knowledge of software testing, you can meet the interviewer after reading it

Halo open source project learning (I): project launch

Digital collection platform settled in digital collection platform to develop SaaS platform of digital collection

【dpdk】10. Dpdk DNS learning notes
随机推荐
Leetcode46 Full Permutation
Unity editor hierarchy drop-down menu extension
如何选择一台好的拨号服务器?
Longest common subsequence (record path version)
Shardingsphere read write separation
2022 crane driver (limited to bridge crane) examination question bank and online simulation examination
Introduction to esp32 Bluetooth controller API
Dimension C China helping farmers in rural areas warms people's hearts the third stop is jiabaoguo farm
C语言实现Base64编码/解码
使用代理IP是需要注意什么?
Some tips for using proxy IP.
Chinese scientists reveal a new mechanism for breaking through the bottleneck of rice yield
How to write the resume of Software Test Engineer so that HR can see it?
代理IP可用率是不是等同于代理IP的效率?
Thinkphp内核开发盲盒商城源码v2.0 对接易支付/阿里云短信/七牛云存储
单片机和4G模块通信总结(EC20)
What categories do you need to know before using proxy IP?
leetcode:27. 移除元素【count remove小操作】
2022 Saison 6 perfect Kid Model IPA national race Leading the Meta - Universe Track
How to choose a good dial-up server?