Spring RedisTemplate Scan
2024-10-30 16:10:48 3
keys
不能用, 那就只能用scan
了
public static Set<String> scan(RedisTemplate<String, String> 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(100).build());
while (cursor.hasNext()) {
keysTmp.add(new String(cursor.next(), StandardCharsets.UTF_8));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return keysTmp;
});
}
链接是否需要关闭
org.springframework.data.redis.core.RedisCallback#doInRedis
文档说不需要关闭链接
游标是否需要关闭
jedis不需要
org.springframework.data.redis.core.ScanCursor#doClose
jedis关闭游标会间接关闭链接, 导致报错
lettuce还没测, 看网上有些人说可以关闭游标.
实际使用还是以源码为准, 做好覆盖测试吧