public class RedisTest {
public static void main(String[] args) {
JedisTestUtil jedisUtil = JedisTestUtil.getInstance();
Jedis jedis = jedisUtil.getJedis();
jedis.flushAll();
for (int i = 0; i < 10; i++) {
jedis.set("key_" + i, "key");
}
for (int i = 0; i < 10; i++) {
jedis.set("hua_" + i, "hua_");
}
String cursor = ScanParams.SCAN_POINTER_START;
String key = "key*";
ScanParams scanParams = new ScanParams();
scanParams.match(key);// 匹配以 key * 为前缀的 key
scanParams.count(2);
while (true) {
//使用scan命令获取500条数据,使用cursor游标记录位置,下次循环使用
ScanResult<String> scanResult = jedis.scan(cursor, scanParams);
cursor = scanResult.getStringCursor();// 返回0 说明遍历完成
scanResult.getResult().forEach(s -> System.out.println(s));
if ("0".equals(cursor)) {
break;
}
}
}
}