我正在尝试使用 SCAN http://redis.io/commands/scan遍历 redis 中存在的所有键。但是spring提供的Redis模板没有任何scan()方法。使用上面有什么技巧吗?
谢谢
最佳答案
您可以在 RedisOperations
上使用 RedisCallback
来执行此操作。
redisTemplate.execute(new RedisCallback<Iterable<byte[]>>() {
@Override
public Iterable<byte[]> doInRedis(RedisConnection connection) throws DataAccessException {
List<byte[]> binaryKeys = new ArrayList<byte[]>();
Cursor<byte[]> cursor = connection.scan(ScanOptions.NONE);
while (cursor.hasNext()) {
binaryKeys.add(cursor.next());
}
try {
cursor.close();
} catch (IOException e) {
// do something meaningful
}
return binaryKeys;
}
});
关于redis - 无法使用 redis 模板进行扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30254559/