我的版本说明:
redis服务端版本:redis_version:2.8.19
jedis:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.0</version>
</dependency>
1、网上关于scan的资料很少,我把自己遇到的先总结下:
scan时间复杂度:O(1)
遍历一次所有的key:O(N),N代表次数
先贴下jedis实现代码:
// 遍历redis所有key
public static void scanSM() {
Jedis jedis = null;
ScanParams scanParams = new ScanParams();
// 设置每次scan个数
scanParams.count(scanCount);
String scanRet = "0";
try {
jedis = RedisUtil.getConn();
do {
List<String> retList = new ArrayList<>();
ScanResult ret = jedis.scan(scanRet, scanParams.match(ConstantDefinition.SM + "*"));
scanRet = ret.getStringCursor();
retList.addAll(ret.getResult());
ScanParseThread scanParseThread = Application.scanParseThreadMap.get(ConstantDefinition.SCAN_PARSE_NAME);
LinkedBlockingQueue<List<String>> lbq = scanParseThread.getLbq();
lbq.put(retList);
} while (!scanRet.equals("0"));
} catch (Exception e) {
logger.error(e);
} finally {
if (null != jedis) {
jedis.close();
}
}
}