【已解决】Spring-Data-Redis中boundValueOps()和opsForValue():boundValueOps()获取成功opsForValue()失败

文章目录

    • 示例
    • 环境搭建:可跳过
    • 正文代码
      • 1
      • 2
      • 3
      • 4
    • 结论:源码

示例

【已解决】Spring-Data-Redis中boundValueOps()和opsForValue():boundValueOps()获取成功opsForValue()失败_第1张图片

环境搭建:可跳过

maven


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-data-redisartifactId>
dependency>


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-testartifactId>
    <scope>testscope>
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);

运行结果:
【已解决】Spring-Data-Redis中boundValueOps()和opsForValue():boundValueOps()获取成功opsForValue()失败_第2张图片
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);

运行结果:
【已解决】Spring-Data-Redis中boundValueOps()和opsForValue():boundValueOps()获取成功opsForValue()失败_第3张图片
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);

【已解决】Spring-Data-Redis中boundValueOps()和opsForValue():boundValueOps()获取成功opsForValue()失败_第4张图片

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);

【已解决】Spring-Data-Redis中boundValueOps()和opsForValue():boundValueOps()获取成功opsForValue()失败_第5张图片

结论:源码

  1. 实际对象是使用StringRedisSerializer进行序列化的
  2. 至于boundValueOps使用get()方法无参时会报错,但是使用get(0,-1)不会报错原因在于这两个重载函数的返回值类型不同,实则使用get(0,-1)得不偿失,还是使用string的序列化方式
    【已解决】Spring-Data-Redis中boundValueOps()和opsForValue():boundValueOps()获取成功opsForValue()失败_第6张图片

【已解决】Spring-Data-Redis中boundValueOps()和opsForValue():boundValueOps()获取成功opsForValue()失败_第7张图片

你可能感兴趣的:(Java,spring,redis,前端)