生环境产 redis禁用keys()的解决办法

@[TOC](生环境产 redis禁用keys()的解决办法)

    /**
     * 删除某种格式的key
     *
     * @param pattern key模式
     * @return
     * @author huarimin
     * @date 2023年8月15日14:43:10
     */
    public void delKeyPattern(String pattern) {
        // eg pattern="abc*" 模糊匹配所有abc*
        RedisConnectionFactory connectionFactory = stringRedisTemplate.getConnectionFactory();
        try {
            assert connectionFactory != null;
            RedisConnection connection = connectionFactory.getConnection();
            if (connection instanceof RedisClusterConnection) {
                log.info("redis 集群模式 执行集群删除");
                delKeyPatternByCluster(pattern);
            }else {
                log.info("redis 单节点模式 执行单节点删除");
                delKeyPatternBySingle(pattern);
            }
        }catch (Exception e){
            log.error("delKeyPattern error", e);
            throw e;
        }
    }
    /**
     * 描述:删除某种格式的key 单机
     *
     * @author huarimin
     * @date 2023年8月15日14:43:10
     */
    private void delKeyPatternBySingle(String pattern) {
        Set<String> keys = stringRedisTemplate.execute((RedisCallback<Set<String>>) connection -> {
            Set<String> stringSet = new HashSet<>();
            // 放在try中自动释放cursor
            try (Cursor<byte[]> cursor = connection.scan(new ScanOptionsBuilder().match(pattern).count(1000).build())) {
                while (cursor.hasNext()) {
                    stringSet.add(new String(cursor.next()));
                }
            } catch (IOException e) {
                log.error("getTotalKeys cursor close", e);
            }
            return stringSet;
        });
        if (!CollectionUtils.isEmpty(keys)) {
            stringRedisTemplate.delete(keys);
        }
    }
    /**
     * 描述:删除某种格式的key 集群
     *
     * @author huarimin
     * @date 2023年8月15日14:43:10
     */
    private void delKeyPatternByCluster(String pattern) {
        ArrayList<byte[]> keyList = Lists.newArrayList();
        RedisConnectionFactory connectionFactory = stringRedisTemplate.getConnectionFactory();
        try {
            assert connectionFactory != null;
            RedisConnection connection = connectionFactory.getConnection();
            if (connection instanceof RedisClusterConnection) {
                // 获取RedisAdvancedClusterCommands
                LettuceClusterConnection con = (LettuceClusterConnection) stringRedisTemplate.getConnectionFactory().getClusterConnection();
                RedisAdvancedClusterAsyncCommands<byte[], byte[]> nativeConnection = (RedisAdvancedClusterAsyncCommands) con.getNativeConnection();
                RedisAdvancedClusterCommands<byte[], byte[]> sync = nativeConnection.getStatefulConnection().sync();

                // Cursor及pattern设置
                KeyScanCursor<byte[]> scanCursor = null;
                ScanArgs scanArgs = new ScanArgs();
                scanArgs.match(pattern);

                do {
                    if (scanCursor == null) {
                        scanCursor = sync.scan(scanArgs);
                    } else {
                        scanCursor = sync.scan(scanCursor, scanArgs);
                    }
                    keyList.addAll(scanCursor.getKeys());
                } while (!scanCursor.isFinished());

                keyList.forEach(bytes -> stringRedisTemplate.delete(new String(bytes)));
            } else {
                // 冗余 防止调用出错
                log.info("未找到集群配置 执行单节点删除");
                delKeyPatternBySingle(pattern);
            }
        } catch (Exception e) {
            log.error("delKeyPatternByCluster error", e);
            throw e;
        }
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值