1、背景
redis: 目前用的官方的 Redis cluster集群方案
cli: 目前用的 spring data redis 以及 Luttuce(替代 jedis,集群下 luttuce相比 jedis更好)
2、目的
想删除一些pattern匹配的key
线上因为 屏蔽了 keys, flushdb这样高危的命令
所以 目前使用 scan 匹配出 key 然后一一删除,无奈 实际执行时,报异常如下:
InvalidDataAccessApiUsageException: Scan is not supported across multiple nodes within a cluster
翻源码,确实 cluster不支持直接 scan cluster,求问 怎么实现下?
当redis以 主-从 模式时 是这样写的:
public void flushdb() {
ScanOptions scanOptions = new ScanOptions.ScanOptionsBuilder().match("user_slash*").count(1000).build();
redisTemplate.execute((RedisCallback<Object>) connection -> {
Cursor<byte[]> cursor = connection.scan(scanOptions);
while (cursor.hasNext()) {
connection.del(cursor.next());
}
return null;
});
}
spring data redis github上没找到 issues
获取所有节点,分别使用scan命令读取;
cursor使用完记得close(),否则导致连接不释放