redis获取所有key的方法(从redis中获取数据)学会了吗

随心笔谈11个月前发布 admin
66 0

public static Set<String> getAllKey(String match,int count){
//返回集
Set<String> binKeys=new HashSet<>();
//封装scan查询参数
ScanParams param=new ScanParams().match(match).count(count);
//获取查询对象
RedisCacheServiceImpl redisCacheSer=(RedisCacheServiceImpl)RedisCacheService;
JedisCluster jedisClu=redisCacheSer.getJedisCluster();
//getClueterNodes获取集群节点,从各个集群中获取值进行遍历
jedisClu.getClusterNodes().values().stream().forEach(pool->{
boolean done=false;//
String cur=”0″;//游标,以0开始,返回0代表一次结束
try(Jedis jedisNode=pool.getResource()){
while(!done){
ScanResult<String> scanResult=jedisNode.scan(cur,param);
cur=scanResult.getStringCursor();
if(“0”.equals(cur){done=true};
List<String> result=scanResult.getResult();
result.foreach(data->{binKeys.add(data)});
}
}
});
return binKeys;
}

© 版权声明

相关文章