示例
环境搭建:可跳过
maven
<!--Redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--Web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
application.properties
server.port=6012
# Redis数据库索引(默认为0)
spring.redis.database=2
# Redis服务器地址
spring.redis.host=
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
测试用例
@SpringBootTest
public class SystemApplicationTest {
@Resource
private RedisTemplate<String, String> redisTemplate;
@Test
void contextLoads() {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
String cacheValue = redisTemplate.boundValueOps("access_token:login:token:7d792ff4-bf32-49d6-adfc-1a01649ea342").get();
System.out.println(cacheValue);
String cacheValue2 = redisTemplate.opsForValue().get("access_token:login:token:7d792ff4-bf32-49d6-adfc-1a01649ea342");
System.out.println(cacheValue2);
}
}
正文代码
1
redisTemplate.setKeySerializer(new StringRedisSerializer());
//差异在这一行
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
String cacheValue = redisTemplate.boundValueOps("access_token:login:token:7d792ff4-bf32-49d6-adfc-1a01649ea342").get(0,-1);
System.out.println(cacheValue);
String cacheValue2 = redisTemplate.opsForValue().get("access_token:login:token:7d792ff4-bf32-49d6-adfc-1a01649ea342");
System.out.println(cacheValue2);
运行结果:
cacheValue被正确序列化
cacheValue2未被正确序列化
2
redisTemplate.setKeySerializer(new StringRedisSerializer());
//差异在这一行
redisTemplate.setValueSerializer(new StringRedisSerializer());
String cacheValue = redisTemplate.boundValueOps("access_token:login:token:7d792ff4-bf32-49d6-adfc-1a01649ea342").get(0,-1);
System.out.println(cacheValue);
String cacheValue2 = redisTemplate.opsForValue().get("access_token:login:token:7d792ff4-bf32-49d6-adfc-1a01649ea342");
System.out.println(cacheValue2);
运行结果:
cacheValue和cacheValue2都被正确序列化
3
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
//和2差异在于这一行的get
String cacheValue = redisTemplate.boundValueOps("access_token:login:token:7d792ff4-bf32-49d6-adfc-1a01649ea342").get();
System.out.println(cacheValue);
String cacheValue2 = redisTemplate.opsForValue().get("access_token:login:token:7d792ff4-bf32-49d6-adfc-1a01649ea342");
System.out.println(cacheValue2);
4
redisTemplate.setKeySerializer(new StringRedisSerializer());
//和3差异在于这一行的get
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
String cacheValue = redisTemplate.boundValueOps("access_token:login:token:7d792ff4-bf32-49d6-adfc-1a01649ea342").get();
System.out.println(cacheValue);
String cacheValue2 = redisTemplate.opsForValue().get("access_token:login:token:7d792ff4-bf32-49d6-adfc-1a01649ea342");
System.out.println(cacheValue2);
结论:源码
- 实际对象是使用StringRedisSerializer进行序列化的
- 至于boundValueOps使用get()方法无参时会报错,但是使用get(0,-1)不会报错原因在于这两个重载函数的返回值类型不同,实则使用get(0,-1)得不偿失,还是使用string的序列化方式