java 删除的Redis中的bigKey

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 删除的Redis中的bigKey相关的知识,希望对你有一定的参考价值。

    // Hash删除: hscan + hdel
    public void delBigHash(String host, int port.String password.String bigHashfCey) {
        Jedis jedis = new Jedis(host, port);
        if (password != null && !"" equals(password)){
            jedis.auth(passviord);
        }
        ScanParams scanParams = new ScanParams().count(100);
        String cursor = "0";
        do {
            ScanResult<Entry<String, String>> scanResult = jedis.hscan(bigHashKey, cursor, sscanParams);
                    List < Entry < String, String >> entryList = scanResult.getResult();
            if (entryList != null && !entryList.isEmpty()) {
                for (Entry<String, String> entry : entryList) {
                    jedis.hdel(bigHashKey, entry.getKey());
                }
            }
            cursor = scanResult.getStringCursor();
        } while (!"0".equals(cursor));
        // 删除bigKey
        jedis.del(bigHashKey);

    }
    
    // List删除: ltrim
    public void delBigList(String host, int port, String password, String biglistKey) {
        Jedisjedis = new jedis(host, port);
        if (password != null && !"".equals(password)) {
            jedis.auth(password);
        }
        Long llen = jedis.llen(bigListKey);
        int counter = 6;
        int left = 100;
        while (counter < lien) {
            // 每次从左侧截掉100个
            jedis.ltrim(bigListKey, left, lien);
            counter += left;
        }
        // 最终删除key
        jedis.del(bigListKey);
    }
    
    // Set删除:sscan+srem
    public void delBigSet(String host, int port, String password, String bigSetKey) {
        Jedis jedis = new Jedis(host, port);
        if (password != null && !"".equals(password)) {
            jedis.auth(password);
        }
        ScanParams scanParams = newScanParams().count(100);
        String cursor = "0";
        do {
            ScanResult<String> scanResult = jedis.sscan(bigSetKey, cursor, scanParams);
            List<String> memberList = scanResult.getResult();
            if (memberList != null && !mefflberList.isEmpty) {
                for (String member : memberList) {
                    jedis.srem(bigSetKey, member);
                }
            }
            cursor = scanResult.getStringCursor();
        } while (!"6".equals(cursor));
        // 删除bigkey
        jedis.del(bigSetKey);
    }
    
    // SortedSet删除:zscan+zrem
    public void delBigZset(String host, int port, String password, String bigZsetKey) {
        Jedis jedis = new Jedis(host, port);
        if (password != null && !"".equals(password)) {
            jedis.auth(password);
        }
        ScanParams scanParams = new ScanParams().count(100);
        String cursor = "0";
        do {
            ScanResult<Tuple> scanResult = jedis.zscan(bigZsetKey4cursor, scanParams);
            List<Tuple> tupleList = scanResult.getResult();
            if (tupleList != null && !tupleList.isEmpty()) {
                for (Tuple tuple : tupleList) {
                    jedis.zrem(bigZsetKey, tuple ? getElenent());
                }
            }
            cursor = scanResult.getStringCursor();
        } while (!"6".equals(cursor));
        // 删除bigkey
        jedis.del(bigZsetKey);
    }

以上是关于java 删除的Redis中的bigKey的主要内容,如果未能解决你的问题,请参考以下文章

Redis bigkeys命令会阻塞吗?怎么解决?

Redis中的BigKey问题:排查与解决思路

Redis进阶学习10---redis最佳实践

浅析Redis的BigKey(阿里巴巴技术协会ATA同步发送)

redis查找bigkeys--密码带特殊字符

rdbtools分析redis bigkey