目录
概念
在 Spring Data Redis 中,序列化和反序列化是关键操作,选择合适的序列化方式可以提高性能和兼容性。根据具体需求,可以使用默认的序列化方式,或自定义序列化器来满足特定要求。
1. 序列化和反序列化的基本概念
- 序列化: 将 Java 对象转换为字节数组,以便存储在 Redis 中。
- 反序列化: 将字节数组转换回 Java 对象,以便在应用程序中使用。
2. Spring Data Redis 默认序列化
Spring Data Redis 默认使用 JdkSerializationRedisSerializer 进行序列化和反序列化。它将对象序列化为字节数组,并在反序列化时恢复为对象。
API函数
SpringDataRedis 中提供了 RedisTemplate 工具类,其中封装了各种对Redis的操作。并且讲不同的数据类型的操作API封装到了不同的类型中。
API | 返回值类型 | 说明 |
redisTemplate.opsForValue() | ValueOperations | 操作String类型数据 |
redisTemplate.opsForHash() | HashOperations | 操作Hash类型数据 |
redisTemplate.opsForList() | ListOperations | 操作List类型的数据 |
redisTemplate.opsForSet() | SetOperations | 操作Set类型数据 |
redisTemplate.opsForZSet() | ZSetOperations | 操作SortedSet类型数据 |
redisTemplate | 通用的命令 |
SpringDataRedis序列化
在Idea里创建一个spring initializr + maven 文件,勾选 Lombok,springredis
- 引入spring-boot-satrter-data-redis依赖
<!-- redis依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- common-pool 连接池依赖--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <!-- Jackson依赖--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency>
- 在application.yml配置Redis信息
spring: redis: host: 127.0.0.1 port: 6379 lettuce: pool: max-active: 8 max-idle: 8 min-idle: 0 max-wait: 100ms
- 注入RedisTemplate
@Configuration public class RedisConfig { @Bean public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){ //创建RedisTemplate的对象 RedisTemplate<String,Object> template = new RedisTemplate<>(); //设置连接工厂 template.setConnectionFactory(connectionFactory); //创建Json序列化工具 GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer(); //设置Key的序列化 template.setKeySerializer(RedisSerializer.string()); template.setHashKeySerializer(RedisSerializer.string()); //设置Value的序列化 template.setValueSerializer(jsonRedisSerializer); template.setHashValueSerializer(jsonRedisSerializer); //返回 return template; } }
4.测试类
package org.example.redisspringdata;
import org.example.redis.pojo.User;
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 RedisSpringdataApplicationTests {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Test
void testString() {
//写入一条String数据
redisTemplate.opsForValue().set("name","虎哥");
//获取string的数据
Object name = redisTemplate.opsForValue().get("name");
System.out.println("name = "+ name);
}
@Test
void testSaveUser(){
//写入数据
redisTemplate.opsForValue().set("user:100",new User("虎哥",21));
//获取数据
User o = (User) redisTemplate.opsForValue().get("user:100");
System.out.println("o=" +o);
}
}
SpringDataRedis反序列化
package org.example.redisspringdata;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.redis.pojo.User;
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 RedisStringTest {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void testString() {
//写入一条String数据
stringRedisTemplate.opsForValue().set("name","虎哥");
//获取string的数据
Object name = stringRedisTemplate.opsForValue().get("name");
System.out.println("name = "+ name);
}
private static final ObjectMapper mapper = new ObjectMapper();
@Test
void testSaveUser() throws JsonProcessingException {
//创建对象
User user = new User("薛之谦",35);
//手动序列化
String json = mapper.writeValueAsString(user);
//写入数据
stringRedisTemplate.opsForValue().set("user:200",json);
//获取数据
String jsonUser = stringRedisTemplate.opsForValue().get("user:200");
//手动序列化
User user1= mapper.readValue(jsonUser,User.class);
System.out.println("user1=" +user1);
}
}