long start = System.currentTimeMillis(); //需要匹配的key String patternKey = "pay:*"; ScanOptions options = ScanOptions.scanOptions() .count(10000) //这里指定每次扫描key的数量 .match(patternKey).build(); RedisSerializer<String> redisSerializer = (RedisSerializer<String>) redisTemplate.getKeySerializer(); Cursor cursor = (Cursor) redisTemplate.executeWithStickyConnection(redisConnection -> new ConvertingCursor<>(redisConnection.scan(options), redisSerializer::deserialize)); List<String> result = new ArrayList<>(); while(cursor.hasNext()){ result.add(cursor.next().toString()); } //切记这里一定要关闭,否则会耗尽连接数。 cursor.close(); log.info("scan扫描共耗时:{} ms key数量:{}",System.currentTimeMillis()-start,result.size());
转自:https://blog.csdn.net/qq_40847065/article/details/113696400