如何通过Spring Boot从Redis缓存中获取所有密钥? [英] How do I get all the keys from a redis cache via Spring Boot?
问题描述
我有一个服务方法,该方法通过Spring Boot通过Redis缓存进行缓存(使用 compile("org.springframework.boot:spring-boot-starter-data-redis:1.5.6.RELEASE")
):
I have a service method that caches with redis cache via Spring Boot (using compile("org.springframework.boot:spring-boot-starter-data-redis:1.5.6.RELEASE")
):
@Cacheable( value = "test" )
public CacheTestObject getTestObject(String name)
{
return new CacheTestObject( name );
}
这可行,但是当我尝试获取所有密钥(并在那里查看)时,我得到:
This works but when i try to get all the keys (and see it there) I get:
//This returns 0 keys
( ( RedisTemplate ) cacheManager.getCache("test").getNativeCache() ).keys( "*" )
但这返回true:
( ( RedisTemplate ) cacheManager.getCache("test").getNativeCache() ).hasKey( "Joe" )
以下是全部内容:
//TRUE
( ( RedisTemplate ) cacheManager.getCache("test").getNativeCache() ).hasKey( "Joe" )
//TRUE
( ( RedisTemplate ) cacheManager.getCache("test").getNativeCache() ).hasKey( "J*e" )
//FALSE
( ( RedisTemplate ) cacheManager.getCache("test").getNativeCache() ).hasKey( "Jo*" )
//FALSE
( ( RedisTemplate ) cacheManager.getCache("test").getNativeCache() ).hasKey( "*oe" )
//FALSE
( ( RedisTemplate ) cacheManager.getCache("test").getNativeCache() ).hasKey( "*" )
这是为什么?为什么不能以模式""获得所有键?甚至是"J "?
Why is this? Why can't I get all keys with the pattern ""? Or even "J"?
如果我进行任何模式查询,其中"*"是第一个或最后一个,则不会返回任何键.
If I do any pattern query where "*" is first or last, it returns no keys.
推荐答案
请确保自动连接到cacheManager的redisTemplate使用正确的序列化程序.如果是字符串键:
Make sure that redisTemplate autowired into your cacheManager uses correct serializers. In case of string keys:
redisTemplate.setKeySerializer(new StringRedisSerializer())
这篇关于如何通过Spring Boot从Redis缓存中获取所有密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!