redis集群模糊获取缓存redisKey

发布时间:2023年12月18日

redis cluster集群删除指定模糊redisKey的信息

**public int deleteRedisKey(String key){
        AtomicReference<Integer> result = new AtomicReference<>(0);
        busnessLogger.info("开始删除指定业务的模糊Key,deleteRedisKey:{}",key);
        try{
            Set<HostAndPort> haps = new HashSet<HostAndPort>();
            if(StringUtils.isNotBlank(redisClusterAddress)){
                String[] addresses = redisClusterAddress.split(",");
                for (String address : addresses) {
                    String[] ipAndPort = address.split(":");
                    HostAndPort hap = new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1]));
                    haps.add(hap);
                }
            }else{
                busnessLogger.error("加载融资租赁Redis集群地址是空的!");
                throw new Exception("加载融资租赁Redis集群地址是空的!");
            }
            GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
            genericObjectPoolConfig.setMaxWaitMillis(Long.valueOf(genericObjectPoolConfigMaxWaitMillis));
            genericObjectPoolConfig.setMaxTotal(Integer.valueOf(genericObjectPoolConfigMaxTotal));
            genericObjectPoolConfig.setMinIdle(Integer.valueOf(genericObjectPoolConfigMinIdle));
            genericObjectPoolConfig.setMaxIdle(Integer.valueOf(genericObjectPoolConfigMaxIdle));
            JedisCluster jedisCluster = new JedisCluster(haps, Integer.valueOf(jedisClusterTimeout),Integer.valueOf(jedisClusterTimeout), Integer.valueOf(jedisClusterTimeoutMaxAttempts), passWd,genericObjectPoolConfig);

            jedisCluster.getClusterNodes().values().stream().forEach(pool->{
                String scanCursor = "0";
                List<String> scanResult = new ArrayList<>();
                do {
                    Jedis resource = pool.getResource();
                    ScanResult<String> scan = resource.scan(scanCursor, new ScanParams().match(key).count(1000000));
                    scanResult = scan.getResult();
                    for (String delete:scanResult) {
                        jedisCluster.del(delete);
                        result.getAndSet(result.get() + 1);
                    }
                    scanCursor = scan.getStringCursor();
                    resource.close();
                } while (!scanCursor.equals("0") && !scanResult.isEmpty());
//                Jedis jedis = pool.getResource();
//                ScanResult<String> scanResult = jedis.scan("0",new ScanParams().match(key));
//                while (!scanResult.isCompleteIteration()) {
//                    long t1 = System.currentTimeMillis();
//                    List<String> list = scanResult.getResult();
//                    for (String delete:list) {
//                        jedis.del(delete);
//                        result.getAndSet(result.get() + 1);
//                    }
//                    scanResult = jedis.scan(scanResult.getCursor(),new ScanParams().match(key));
//                    long t2 = System.currentTimeMillis();
//                }
          });
            jedisCluster.close();
            }catch (Exception e){
                busnessLogger.error("加载前置征信平台Redis集群异常!", e);
                throw new RuntimeException(e.getMessage());
            }
        busnessLogger.info("指定业务Key删除的个数:{}",result.get());
        return result.get();
    }**

这里面有两个需要特别注意:
1.ScanParam里面的count要设置大一点。
2.如果遍历做删除的时候,如果是集群,要用jedisCluster去删除。否则会删除不了。

文章来源:https://blog.csdn.net/zhaoyuhong__/article/details/135001267
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。