redis HSCAN命令及jedis的hscan方法[通俗易懂]

参考文档:

http://doc.redisfans.com/key/scan.html

hscan是针对hash类型扫描,如果hash内条目非常多时,比如超过10万,那么根据默认的count = 10,每次扫描返回空的概率很大,此时可以根据情况把count调大:

redis HSCAN命令及jedis的hscan方法[通俗易懂]redis HSCAN命令及jedis的hscan方法[通俗易懂]

下边是对jedis的 hscan进行了封装:

@Override
	public ScanResults hscanToResltByVagueString 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.isNullcursor)) {
			scanCursor = cursor;
		}
		params.countpageSize + 50);
		do {
			ScanResult<Entry<String, String>> scanResult = jedis.hscankey, scanCursor, params.matchpattern));
			scanCursor = scanResult.getStringCursor);
			result = scanResult.getResult);
			results.addAllresult);
			if results.size) >= pageSize) {
				break;
			}
		} while !scanCursor.equals"0"));
		rsl.setCursorscanCursor);
		rsl.setResultsEntryresults);
		rsl.setResultSizeresults.size));
		return rsl;
	}

Jetbrains全家桶1年46,售后保障稳定

 实际使用:

@Test
     public void testRedisHscan) throws InterruptedException {
         
         Thread.sleep1000*40);
         
         long start = System.currentTimeMillis);
         
         DataCache cache = DataCacheFactory.getInstance);
         //ScanResults rs = cache.hscanToResltByVague"METADATA:VDITEMS", "c3ed2101aa56421d9f0eb23e4b719c29*", "0", 50000);
         List<HashMap<String, String>> lst = cache.hscanToResltByVagueSysConfig.getProperty"metadata.cache.vdItem"), "c3ed2101aa56421d9f0eb23e4b719c29*", "0", 50000).getResults);
         
        long end = System.currentTimeMillis);
         
         System.out.println"+++++++++++++++++耗时:"+end-start));
         try {
            System.out.println"+++++++++++++++++耗时:"+end-start)+"     结果:"+JSONUtils.serializeObjectlst));
        } catch IOException e) {
            // TODO Auto-generated catch block
             e.printStackTrace);
        }
     }

 

 

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注