/**
* redis工具类 扫描指定key,替换掉"redisTemplate.keys()"
*/
public abstract class RedisHelper {
/**
* scan 实现
* @param redisTemplate redisTemplate
* @param pattern 表达式,如:abc*,找出所有以abc开始的键
*/
public Set<String> scan(RedisTemplate<String, Object> redisTemplate, String pattern) {
return redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
Set<String> keysTmp = new HashSet<>();
try (Cursor<byte[]> cursor = connection.scan(new ScanOptions.ScanOptionsBuilder()
.match(pattern)
.count(10000).build())) {
while (cursor.hasNext()) {
keysTmp.add(new String(cursor.next(), "Utf-8"));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return keysTmp;
});
}
}
RedisTemplate.keys() 替换为Scan 扫描指定Key
最新推荐文章于 2024-07-19 16:49:51 发布