redis HSCAN命令及jedis的hscan方法
参考文档:http://doc.redisfans.com/key/scan.html@Overridepublic ScanResults hscanToResltByVague(String key, String pattern, String cursor, int pageSize) {List<Map.Entry<String, String>> result
·
参考文档:
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();
}
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献12条内容
所有评论(0)