package com.artfess.redis.util; import org.springframework.data.redis.core.Cursor; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ScanOptions; import java.util.HashSet; import java.util.Set; /** * redis扩展工具 * * @company 阿特菲斯信息技术有限公司 * @author heyifan * @email heyf@jee-soft.cn * @date 2020年6月17日 */ public abstract class RedisHelper { /** * scan 实现 * * @param redisTemplate redisTemplate * @param pattern 表达式 */ public static Set scan(RedisTemplate redisTemplate, String pattern) { return redisTemplate.execute((RedisCallback>) connection -> { Set keysTmp = new HashSet<>(); try (Cursor cursor = connection.scan(new ScanOptions.ScanOptionsBuilder() .match(pattern) .count(10000).build())) { while (cursor.hasNext()) { keysTmp.add(new String(cursor.next(), "Utf-8")); } } catch (Exception e) { throw new RuntimeException(e); } return keysTmp; }); } }