Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RedisTemplate keys(String pattern) method is giving empty set

Using org.springframework.data.redis.core.RedisTemplate for storing data in redis server. I have the keys in pattern similar to "abc@xyz@pqr". Wanted to get all the keys which have the starting letters as "abc", and was using RedisTemplate.keys(String pattern) method for the same as Below:

Set<String> redisKeys = redisTemplate.keys("(abc).*");

for (String key : redisKeys) {
    System.out.println(key);
}

But its always giving me empty set.

// tried this pattern also 
Set<String> redisKeys = redisTemplate.keys("abc*");

Please help me out.

like image 828
Virat Mishra Avatar asked Mar 09 '23 13:03

Virat Mishra


1 Answers

Make sure to use StringRedisSerializer to serialize keys. Spring Data Redis defaults to JdkSerializationRedisSerializer which does not allow glob-style search because of the way it works.

Check out the reference documentation for more details.

like image 189
mp911de Avatar answered Mar 25 '23 01:03

mp911de