SprintBoot 2.*中redisTemplate通过scan删除keys

1 问题

其实用过redis的都明白,redis在真实项目中是很忌讳使用redisTemplate.keys("*")来获取所有key的,这样会使redis在一定时间内卡住。如果redis数据巨大的话,那等待时间很长,几乎可以说是停服了。

2 解决方法

使用scan命名

3 具体操作

查看lettuce-core源码,我们可以这样操作:

3.1 参考lettuce-core源码测试代码

我们通常是使用的集群,所以查看的是AdvancedClusterClientIntegrationTests.java

@Test
    void clusterScan() {

        RedisAdvancedClusterCommands<String, String> sync = async.getStatefulConnection().sync();
        sync.mset(KeysAndValues.MAP);

        Set<String> allKeys = new HashSet<>();

        KeyScanCursor<String> scanCursor = null;

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

        assertThat(allKeys).containsAll(KeysAndValues.KEYS);
    }

    @Test
    void clusterScanWithArgs() {

        RedisAdvancedClusterCommands<String, String> sync = async.getStatefulConnection().sync();
        sync.mset(KeysAndValues.MAP);

        Set<String> allKeys = new HashSet<>();

        KeyScanCursor<String> scanCursor = null;

        do {
            if (scanCursor == null) {
                scanCursor = sync.scan(ScanArgs.Builder.matches("a*"));
            } else {
                scanCursor = sync.scan(scanCursor, ScanArgs.Builder.matches("a*"));
            }
            allKeys.addAll(scanCursor.getKeys());
        } while (!scanCursor.isFinished());

        assertThat(allKeys)
                .containsAll(KeysAndValues.KEYS.stream().filter(k -> k.startsWith("a")).collect(Collectors.toList()));
    }

    @Test
    void clusterScanStreaming() {

        RedisAdvancedClusterCommands<String, String> sync = async.getStatefulConnection().sync();
        sync.mset(KeysAndValues.MAP);

        ListStreamingAdapter<String> adapter = new ListStreamingAdapter<>();

        StreamScanCursor scanCursor = null;

        do {
            if (scanCursor == null) {
                scanCursor = sync.scan(adapter);
            } else {
                scanCursor = sync.scan(adapter, scanCursor);
            }
        } while (!scanCursor.isFinished());

        assertThat(adapter.getList()).containsAll(KeysAndValues.KEYS);

    }

    @Test
    void clusterScanStreamingWithArgs() {

        RedisAdvancedClusterCommands<String, String> sync = async.getStatefulConnection().sync();
        sync.mset(KeysAndValues.MAP);

        ListStreamingAdapter<String> adapter = new ListStreamingAdapter<>();

        StreamScanCursor scanCursor = null;
        do {
            if (scanCursor == null) {
                scanCursor = sync.scan(adapter, ScanArgs.Builder.matches("a*"));
            } else {
                scanCursor = sync.scan(adapter, scanCursor, ScanArgs.Builder.matches("a*"));
            }
        } while (!scanCursor.isFinished());

        assertThat(adapter.getList()).containsAll(
                KeysAndValues.KEYS.stream().filter(k -> k.startsWith("a")).collect(Collectors.toList()));

    }

 

3.2 通过redisTemplate获取RedisAdvancedClusterAsyncCommands

参考:http://www.shangdixinxi.com/detail-1230282.html

public boolean tryLock(String lockKey, String clientId, long seconds) {
      return redisTemplate.execute((RedisCallback<Boolean>) redisConnection -> {
//            Jedis jedis = (Jedis) redisConnection.getNativeConnection();
          Object nativeConnection = redisConnection.getNativeConnection();
          RedisSerializer<String> stringRedisSerializer = (RedisSerializer<String>) redisTemplate.getKeySerializer();
          byte[] keyByte = stringRedisSerializer.serialize(lockKey);
          byte[] valueByte = stringRedisSerializer.serialize(clientId);

          // lettuce连接包下 redis 单机模式
          if (nativeConnection instanceof RedisAsyncCommands) {
              RedisAsyncCommands connection = (RedisAsyncCommands) nativeConnection;
              RedisCommands commands = connection.getStatefulConnection().sync();
              String result = commands.set(keyByte, valueByte, SetArgs.Builder.nx().ex(seconds));
              if (LOCK_SUCCESS.equals(result)) {
                  return true;
              }
          }
          // lettuce连接包下 redis 集群模式
          if (nativeConnection instanceof RedisAdvancedClusterAsyncCommands) {
              RedisAdvancedClusterAsyncCommands connection = (RedisAdvancedClusterAsyncCommands) nativeConnection;
              RedisAdvancedClusterCommands commands = connection.getStatefulConnection().sync();
              String result = commands.set(keyByte, valueByte, SetArgs.Builder.nx().ex(seconds));
              if (LOCK_SUCCESS.equals(result)) {
                  return true;
              }
          }

          if (nativeConnection instanceof JedisCommands) {
              JedisCommands jedis = (JedisCommands) nativeConnection;
              String result = jedis.set(lockKey, clientId, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, seconds);
              if (LOCK_SUCCESS.equals(result)) {
                  return true;
              }
          }
          return false;
      });
  }

3.3 最终结果

// String  key="*";
public void ScanAndDel(String key) {
        try {
            redisTemplate.execute((RedisCallback<Boolean>) redisConnection ->
            {
                Object nativeConnection = redisConnection.getNativeConnection();

                // Jedis
                if (nativeConnection instanceof JedisCommands) {
                    //TODO:
                }

                // lettuce 单机
                if (nativeConnection instanceof RedisAsyncCommands) {
                    //TODO:
                }

                // lettuce 集群
                if (nativeConnection instanceof RedisAdvancedClusterAsyncCommands) {
                    RedisAdvancedClusterAsyncCommands connection = (RedisAdvancedClusterAsyncCommands) nativeConnection;
                    /*RedisAdvancedClusterCommands<String, String> commands = connection.getStatefulConnection().sync();

                    KeyScanCursor<String> scanCursor = null;
                    do {
                        if (scanCursor == null) {
                            scanCursor = commands.scan(ScanArgs.Builder.matches(key));
                        } else {
                            scanCursor = commands.scan(scanCursor, ScanArgs.Builder.matches(key));
                        }

                        List<String> byteKeys = Collections.unmodifiableList(scanCursor.getKeys());
                        if (byteKeys.size() > 0) {
                            List<String> keys = new ArrayList<>();
                            for (Object k : byteKeys) {
                                keys.add(new String((byte[])k, StandardCharsets.UTF_8));
                            }
                            redisTemplate.delete(keys);
                        }
                    } while (!scanCursor.isFinished());*/

                    RedisAdvancedClusterCommands<byte[], String> commands = connection.getStatefulConnection().sync();

                    KeyScanCursor<byte[]> scanCursor = null;
                    do {
                        if (scanCursor == null) {
                            scanCursor = commands.scan(ScanArgs.Builder.matches(matches));
                        } else {
                            scanCursor = commands.scan(scanCursor, ScanArgs.Builder.matches(matches));
                        }

                        List<byte[]> byteKeys = scanCursor.getKeys();
                        if (byteKeys.size() > 0) {
                            byte[][] keys = new byte[byteKeys.size()][];
                            for (int i = 0; i < byteKeys.size(); i++) {
                                keys[i] = byteKeys.get(i);
                            }

                            commands.del(keys);
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                            }
                        }
                    } while (!scanCursor.isFinished());
                }
                return true;
            });
        } catch (Exception e) {
        }
    }

当然,可以是使用ListStreamingAdapter进行操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值