@Test
public void deleteKeys() {
List<String> keys = redisUtils.keys(RedisKeys.getStoreKey("*"));
for (String key : keys) {
redisUtils.delete(key);
}
}
public List<String> keys(String pattern) {
List<String> keys = new ArrayList<>();
this.scan(pattern, item -> {
String key = new String(item, StandardCharsets.UTF_8);
keys.add(key);
});
return keys;
}
private void scan(String pattern, Consumer<byte[]> consumer) {
this.redisTemplate.execute((RedisConnection connection) -> {
try (Cursor<byte[]> cursor = connection.scan(ScanOptions.scanOptions().count(Long.MAX_VALUE).match(pattern).build())) {
cursor.forEachRemaining(consumer);
return null;
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}