jedis中scan的實現

個人版本說明:redis

redis服務端版本:redis_version:2.8.19spa

jedis:code

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.0</version>
</dependency>

一、網上關於scan的資料不多,我把本身遇到的先總結下:blog

scan時間複雜度:O(1)get

遍歷一次全部的key:O(N),N表明次數it

先貼下jedis實現代碼:io

    // 遍歷redis全部key
    public static void scanSM() {
        Jedis jedis = null;
        ScanParams scanParams = new ScanParams();
        // 設置每次scan個數
        scanParams.count(scanCount); 
        String scanRet = "0";
        try {
            jedis = RedisUtil.getConn();
            do {
                List<String> retList = new ArrayList<>();
                ScanResult ret = jedis.scan(scanRet, scanParams.match(ConstantDefinition.SM + "*"));
                scanRet = ret.getStringCursor();
                retList.addAll(ret.getResult());
                ScanParseThread scanParseThread = Application.scanParseThreadMap.get(ConstantDefinition.SCAN_PARSE_NAME);
                LinkedBlockingQueue<List<String>> lbq = scanParseThread.getLbq();
                lbq.put(retList);
            } while (!scanRet.equals("0"));
        } catch (Exception e) {
            logger.error(e);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
    }    
相關文章
相關標籤/搜索