public Set<String> scan(String key) {
return redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
Set<String> keys = Sets.newHashSet();
JedisCommands commands = (JedisCommands) connection.getNativeConnection();
MultiKeyCommands multiKeyCommands = (MultiKeyCommands) commands;
ScanParams scanParams = new ScanParams();
scanParams.match("*" + key + "*");
scanParams.count(1000); // 这个不是返回结果的数量,应该是每次scan的数量
ScanResult<String> scan = multiKeyCommands.scan("0", scanParams);
while (null != scan.getCursor()) {
LogManager.info("scan="+scan.getCursor()+" Result="+JsonTool.encode(scan.getResult()));
keys.addAll(scan.getResult()); // 这一次scan match到的结果
if (!scan.getCursor().equals("0")) { // 不断拿着新的cursor scan,最终会拿到所有匹配的值
scan = multiKeyCommands.scan(scan.getCursor(), scanParams);
continue;
} else {
break;
}
}
return keys;
});
}
StringRedisTemplate使用scan
最新推荐文章于 2023-11-30 21:59:16 发布