【问题标题】:How to get all Keys from Redis using redis template如何使用 redis 模板从 Redis 获取所有密钥
【发布时间】:2013-10-06 13:03:31
【问题描述】:

我已经被这个问题困扰了很长一段时间。我想使用 redis 模板从 redis 获取密钥。 我试过 this.redistemplate.keys("*"); 但这并没有得到任何东西。即使使用该模式,它也不起作用。

您能否建议最好的解决方案。

【问题讨论】:

    标签: spring redis


    【解决方案1】:

    我只是整合了答案,我们在这里看到了。

    当我们使用 RedisTemplate 时,这里有两种从 Redis 获取密钥的方法。

    1.直接来自 RedisTemplate

    Set<String> redisKeys = template.keys("samplekey*"));
    // Store the keys in a List
    List<String> keysList = new ArrayList<>();
    Iterator<String> it = redisKeys.iterator();
    while (it.hasNext()) {
           String data = it.next();
           keysList.add(data);
    }
    

    注意:你应该已经在你的 bean 中配置了带有 StringRedisSerializer 的 redisTemplate

    如果你使用基于 java 的 bean 配置

    redisTemplate.setDefaultSerializer(new StringRedisSerializer());
    

    如果你使用基于 spring.xml 的 bean 配置

    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
    
    <!-- redis template definition -->
    <bean
        id="redisTemplate"
        class="org.springframework.data.redis.core.RedisTemplate"
        p:connection-factory-ref="jedisConnectionFactory"
        p:keySerializer-ref="stringRedisSerializer"
        />
    

    2。来自 JedisConnectionFactory

    RedisConnection redisConnection = template.getConnectionFactory().getConnection();
    Set<byte[]> redisKeys = redisConnection.keys("samplekey*".getBytes());
    List<String> keysList = new ArrayList<>();
    Iterator<byte[]> it = redisKeys.iterator();
    while (it.hasNext()) {
           byte[] data = (byte[]) it.next();
           keysList.add(new String(data, 0, data.length));
    }
    redisConnection.close();
    

    如果你不明确关闭这个连接,你会遇到https://stackoverflow.com/a/36641934/3884173中所说的底层jedis连接池的耗尽。

    【讨论】:

      【解决方案2】:

      尝试:

      Set<byte[]> keys = RedisTemplate.getConnectionFactory().getConnection().keys("*".getBytes());
      
      Iterator<byte[]> it = keys.iterator();
      
      while(it.hasNext()){
      
          byte[] data = (byte[])it.next();
      
          System.out.println(new String(data, 0, data.length));
      }
      

      【讨论】:

        【解决方案3】:

        试试redisTemplate.setKeySerializer(new StringRedisSerializer());

        【讨论】:

        • 恕我直言,这不是最好的解决方案,因为与 HashOperations 一起使用时可能会产生一些错误
        • 是的,它确实给了我问题。在这种情况下,您不能输入字符串以外的类型值。
        【解决方案4】:

        避免使用keys 命令。当它针对大型数据库执行时,它可能会破坏性能。

        您应该改用scan 命令。以下是你可以做到的:

        RedisConnection redisConnection = null;
        try {
            redisConnection = redisTemplate.getConnectionFactory().getConnection();
            ScanOptions options = ScanOptions.scanOptions().match("myKey*").count(100).build();
        
            Cursor c = redisConnection.scan(options);
            while (c.hasNext()) {
                logger.info(new String((byte[]) c.next()));
            }
        } finally {
            redisConnection.close(); //Ensure closing this connection.
        }
        

        或者使用Redisson Redis Java 客户端更简单:

        Iterable<String> keysIterator = redisson.getKeys().getKeysByPattern("test*", 100);
        for (String key : keysIterator) {
            logger.info(key);
        }
        

        【讨论】:

        • 我在 redis 缓存中有键列表(数据类型为列表),我想一次性获取所有键。我们可以使用 redisson 做到这一点吗?
        【解决方案5】:

        它确实有效,但似乎不推荐?因为我们不能在生产中使用 Keys 命令。我假设 RedisTemplate.getConnectionFactory().getConnection().keys 正在调用 redis Keys 命令。有哪些替代方案?

        【讨论】:

          【解决方案6】:

          我使用的是redisTemplate.keys(),但它不起作用。所以我使用了绝地武士,它奏效了。以下是我使用的代码。

              Jedis jedis = new Jedis("localhost", 6379);
              Set<String> keys = jedis.keys("*".getBytes());
              for (String key : keys) {
                  // do something
              } // for
          

          【讨论】:

          • API 文档说这仅用于调试,是真的吗?
          • 应该是 "Set keys = jedis.keys("*".getBytes());"
          【解决方案7】:

          解决办法可以是这样的

          String pattern = "abc"+"*";
          Set<String> keys = jedis.keys(pattern);
          for (String key : keys) {
              jedis.keys(key);
          } 
          

          或者您可以使用jedis.hscan()ScanParams 代替。

          【讨论】:

          • 不要在生产环境中使用keys,危险
          【解决方案8】:

          试试

          import org.springframework.data.redis.core.RedisTemplate;
          import org.apache.commons.collections.CollectionUtils;
          
          String key = "example*";
          Set keys = redisTemplate.keys(key); 
          if (CollectionUtils.isEmpty(keys)) return null;
          List list = redisTemplate.opsForValue().multiGet(keys);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-07-03
            • 1970-01-01
            • 2016-09-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多