spring redis 模糊查找key
用法
Set<String> keySet = stringRedisTemplate.keys("keyprefix:"+"*");
- 需要使用StringRedisTemplate,或自定义keySerializer为StringRedisSerializer的redisTemplate
- redis里模糊查询key允许使用的通配符:
* 任意多个字符
? 单个字符
[] 括号内的某1个字符
源码
- org.springframework.data.redis.core.RedisTemplate
public Set<K> keys(K pattern) {
byte[] rawKey = rawKey(pattern);
Set<byte[]> rawKeys = execute(connection -> connection.keys(rawKey), true);
return keySerializer != null ? SerializationUtils.deserialize(rawKeys, keySerializer) : (Set<K>) rawKeys;
}