spring-data-redis multiGet 如何处理缓存未命中?
How are cache misses handled by spring-data-redis multiGet?
我正在使用 Redis 缓存(通过 Jedis 客户端),我想使用 ValueOperations#multiGet
, which takes a Collection
of keys, and returns a List
of objects from the cache, in the same order. My question is, what happens when some of the keys are in the cache, but others are not? I am aware that underneath, Redis MGET,这将 return nil
用于任何不存在的元素在缓存中。
我找不到任何关于 ValueOperations
将如何解释此响应的文档。我假设它们会是 null
,当然可以测试它,但是围绕未记录的行为构建系统是危险的。
为了完整起见,这里是缓存客户端的配置方式:
@Bean
public RedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
redisConnectionFactory.setHostName(address);
redisConnectionFactory.setPort(port);
redisConnectionFactory.afterPropertiesSet();
return redisConnectionFactory;
}
@Bean
public ValueOperations<String, Object> someRedisCache(RedisConnectionFactory cf) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(cf);
redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate.opsForValue();
}
我正在使用spring-data-redis:2.1.4
那么,是否有关于此的任何文档或一些可靠的真相来源?
经过一番探索,答案似乎与使用的序列化程序有关——在本例中为 GenericJackson2JsonRedisSerializer
。不想挖掘太多,我只是写了一个测试来验证 Redis 返回的任何 (nil)
值都转换为 null
:
@Autowired
ValueOperations<String, SomeObject> valueOperations
@Test
void multiGet() {
//Given
SomeObject someObject = SomeObject
.builder()
.contentId("key1")
.build()
valueOperations.set("key1", someObject)
//When
List<SomeObject> someObjects = valueOperations.multiGet(Arrays.asList("key1", "nonexisting"))
//Then
assertEquals(2, someObjects.size())
assertEquals(someObject, someObjects.get(0))
assertEquals(null, someObjects.get(1))
}
因此,在 Redis 中,这:
127.0.0.1:6379> MGET "\"key1\"" "\"nonexisting\""
1) "{\"@class\":\"some.package.SomeObject\",\"contentId\":\"key1\"}"
2) (nil)
将导致 List
的 {SomeObject, null}
我正在使用 Redis 缓存(通过 Jedis 客户端),我想使用 ValueOperations#multiGet
, which takes a Collection
of keys, and returns a List
of objects from the cache, in the same order. My question is, what happens when some of the keys are in the cache, but others are not? I am aware that underneath, Redis MGET,这将 return nil
用于任何不存在的元素在缓存中。
我找不到任何关于 ValueOperations
将如何解释此响应的文档。我假设它们会是 null
,当然可以测试它,但是围绕未记录的行为构建系统是危险的。
为了完整起见,这里是缓存客户端的配置方式:
@Bean
public RedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
redisConnectionFactory.setHostName(address);
redisConnectionFactory.setPort(port);
redisConnectionFactory.afterPropertiesSet();
return redisConnectionFactory;
}
@Bean
public ValueOperations<String, Object> someRedisCache(RedisConnectionFactory cf) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(cf);
redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate.opsForValue();
}
我正在使用spring-data-redis:2.1.4
那么,是否有关于此的任何文档或一些可靠的真相来源?
经过一番探索,答案似乎与使用的序列化程序有关——在本例中为 GenericJackson2JsonRedisSerializer
。不想挖掘太多,我只是写了一个测试来验证 Redis 返回的任何 (nil)
值都转换为 null
:
@Autowired
ValueOperations<String, SomeObject> valueOperations
@Test
void multiGet() {
//Given
SomeObject someObject = SomeObject
.builder()
.contentId("key1")
.build()
valueOperations.set("key1", someObject)
//When
List<SomeObject> someObjects = valueOperations.multiGet(Arrays.asList("key1", "nonexisting"))
//Then
assertEquals(2, someObjects.size())
assertEquals(someObject, someObjects.get(0))
assertEquals(null, someObjects.get(1))
}
因此,在 Redis 中,这:
127.0.0.1:6379> MGET "\"key1\"" "\"nonexisting\""
1) "{\"@class\":\"some.package.SomeObject\",\"contentId\":\"key1\"}"
2) (nil)
将导致 List
的 {SomeObject, null}