String roomKey = RedisConstant.RESERVE_ROOM_LOCK + RedisConstant.JOINER + roomTimeDto.getFirmid() + RedisConstant.JOINER + roomTimeDto.getDateCode() + RedisConstant.JOINER+“*”;
long start = System.currentTimeMillis();
//需要匹配的key
String patternKey = roomKey;
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());