猪猪吧博客2 外汇 正文

A scan in Redis Cluster mode requires to reuse the resulting cursor from the previous scan invocation

Lettuce 集群翻页获取keys的时候报错

A scan in Redis Cluster mode requires to reuse the resulting cursor from the previous scan invocation


调用:

KeyScanCursor<String> scanCursor = null;
do {
scanCursor = redisTemplatePersion.scan(scanCursor, options);
List<String> keys = scanCursor.getKeys();
System.out.println(keys);
} while (!scanCursor.isFinished());


实现:

public KeyScanCursor<K> scan(ScanCursor scanCursor, ScanOptions options) {
StatefulConnection statefulConnection = LettuceConnectionFactory.getRedisConnection();
if (statefulConnection instanceof StatefulRedisConnection) {
StatefulRedisConnection statefulRedisConnection = (StatefulRedisConnection) statefulConnection;
RedisCommands redisCommands = statefulRedisConnection.sync();
ScanArgs scanArgs = LettuceConverters.toScanArgs(options);
return redisCommands.scan(scanCursor,scanArgs);
} else if (statefulConnection instanceof StatefulRedisClusterConnection) {
StatefulRedisClusterConnection statefulRedisClusterConnection = (StatefulRedisClusterConnection) statefulConnection;
RedisAdvancedClusterCommands redisAdvancedClusterCommands = statefulRedisClusterConnection.sync();
ScanArgs scanArgs = LettuceConverters.toScanArgs(options);
if(null==scanCursor){
           return redisAdvancedClusterCommands.scan(scanArgs);
       }
       return redisAdvancedClusterCommands.scan(scanCursor,scanArgs);

}
return null;
}


这里红色部分就是重点,传递过来的scanCursor为null值必须调用第一个

redisAdvancedClusterCommands.scan(scanArgs)

,调用第二个方法

redisAdvancedClusterCommands.scan(scanCursor,scanArgs)

翻页到第二页就会报错:

A scan in Redis Cluster mode requires to reuse the resulting cursor from the previous scan invocation

本文转载自互联网,版权归原作者所有,转载目的在于传递更多的信息,并不代表本网站的观点和立场。 如发现本站文章存在内容、版权或其它问题,烦请联系,我们将及时删除。

评论列表

    快捷回复: