大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全家桶1年46,售后保障稳定
参考文档:
http://doc.redisfans.com/key/scan.html
hscan是针对hash类型扫描,如果hash内条目非常多时,比如超过10万,那么根据默认的count = 10,每次扫描返回空的概率很大,此时可以根据情况把count调大:
下边是对jedis的 hscan进行了封装:
@Override
public ScanResults hscanToResltByVague(String key, String pattern, String cursor, int pageSize) {
List<Map.Entry<String, String>> result = null;
List<Map.Entry<String, String>> results = new ArrayList<Map.Entry<String, String>>();
ScanResults rsl = new ScanResults();
ScanParams params = new ScanParams();
String scanCursor = "0";
if (!RobotUtils.isNull(cursor)) {
scanCursor = cursor;
}
params.count(pageSize + 50);
do {
ScanResult<Entry<String, String>> scanResult = jedis.hscan(key, scanCursor, params.match(pattern));
scanCursor = scanResult.getStringCursor();
result = scanResult.getResult();
results.addAll(result);
if (results.size() >= pageSize) {
break;
}
} while (!scanCursor.equals("0"));
rsl.setCursor(scanCursor);
rsl.setResultsEntry(results);
rsl.setResultSize(results.size());
return rsl;
}
实际使用:
@Test
public void testRedisHscan() throws InterruptedException {
Thread.sleep(1000*40);
long start = System.currentTimeMillis();
DataCache cache = DataCacheFactory.getInstance();
//ScanResults rs = cache.hscanToResltByVague("METADATA:VDITEMS", "c3ed2101aa56421d9f0eb23e4b719c29*", "0", 50000);
List<HashMap<String, String>> lst = cache.hscanToResltByVague(SysConfig.getProperty("metadata.cache.vdItem"), "c3ed2101aa56421d9f0eb23e4b719c29*", "0", 50000).getResults();
long end = System.currentTimeMillis();
System.out.println("+++++++++++++++++耗时:"+(end-start));
try {
System.out.println("+++++++++++++++++耗时:"+(end-start)+" 结果:"+JSONUtils.serializeObject(lst));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/230809.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...