记录使用redisTemplate的scan方式删除批量key
public void deleteKeysByScan(String pattern) {
long start = System.currentTimeMillis();
LOGGER.error("清理{}类外呼记录缓存的定时任务开始执行", pattern);
String param = pattern + "*";
Set<String> keys = this.getValuesForStringByScan(param);
redisTemplate.delete(keys);
LOGGER.error("清理{}类外呼记录缓存的定时任务执行结束,耗时:{}", pattern, (System.currentTimeMillis() - start));
}
public Set<String> getValuesForStringByScan(String patten) {
return redisTemplate.execute(connect -> {
Set<String> binaryKeys = new HashSet<>();
Cursor<byte[]> cursor = connect.scan(new ScanOptions.ScanOptionsBuilder().match(patten).count(200000).build());
while (cursor.hasNext() && binaryKeys.size() < 200000) {
binaryKeys.add(new String(cursor.next()));
}
return binaryKeys;
}, true);
}