ERR unknown command keys
切换redis后发现keys命令出现报错,经查询原因如下:
原因可能是:keys命令用于全局查询 一般不建议使用所以能在redis.conf中配置了置换该命令
解决方案: 检查redis.conf 中是否有该命令禁用配置
rename-command FLUSHALL "" 清空所有的库数据
rename-command FLUSHDB "" 清空当前库数据
rename-command KEYS "" 查询所有库数据 (因为数据量大时 执行此操作会消耗大量的资源 一般不建议使用该命令)
如果有注释掉即可
但是因为使用的是统一部署的redis,无法进行修改操作,于是只好修改涉及kes命令的操作
使用scan获取redisKey
@Autowired
public RedisTemplate redisTemplate;
public Set<String> scan(String matchKey) {
Set<String> keys = new HashSet<>();
ScanOptions scanOptions = ScanOptions.scanOptions().match(matchKey).count(1000).build();
Cursor<byte[]> cursor = (Cursor<byte[]>) redisTemplate.execute(redisConnection -> redisConnection.scan(scanOptions), true);
RedisSerializer redisSerializer = redisTemplate.getKeySerializer();
while (cursor.hasNext()) {
keys.add(String.valueOf(redisSerializer.deserialize(cursor.next())));
}
return keys;
}