redis-redisTemplate fuzzy matching deleted
A few days ago a vague need to delete redis in key functions, did not think, directly
Key = String "noteUserListenedPoi: *"; redisTemplate.delete (Key); logger.info ( "Redis user listening history is empty");
It was found that no,
Later tests found fuzzy queries can be used to find the following information, change
SET <String> = redisTemplate.keys Keys ( "noteUserListenedPoi:" + "*"); redisTemplate.delete (Keys); logger.info ( "{}, a user listening history Redis emptied"
It can be used
Redis fuzzy query
Key values may be acquired in Redis command keys, the specific command format: keys pattern
The article mentions redis are allowed three wildcards fuzzy queries are: *, []?
among them:
*
: Wildcard any number of characters
?
: Wild single character
[]
: Wild one character within the brackets
public Set keys(String pattern){
return stringRedisTemplate.keys("*" + pattern + "*");
// return stringRedisTemplate.keys("?" + pattern);
// return stringRedisTemplate.keys("[" + pattern + "]");
}
Batch query
Set<String> keysList = stringRedisTemplate.keys(keys);
List<String> strings = stringRedisTemplate.opsForValue().multiGet(keysList);
single
redisTemplate.opsForValue().set(keys+1,"this is a test content!",1000,TimeUnit.SECONDS);
String content=redisTemplate.opsForValue().get(keys+1).toString();
System.out.println("---------》获取到缓存的内容为:"+content);
redisTemplate.delete(redisTemplate.keys(keys+"*"));
Object msg=redisTemplate.opsForValue().get(keys+1);