简体   繁体   English

使用 Jedis 从 redis 缓存中获取所有键值对的有效方法

[英]Efficient way to get all the key value pair from redis cache using Jedis

I have a redis cache database that holds 80k records.我有一个包含 80k 记录的 redis 缓存数据库。 I need to get this entire data( key and value) in my application.我需要在我的应用程序中获取整个数据(键和值)。 I am using Jedis Client and here is the code I have used:我正在使用 Jedis 客户端,这是我使用的代码:

private static JedisPool jedisPool;
public RedisClient(String ip, int port, int timeout, String password) {
        try {
            if (jedisPool == null) {
                JedisPoolConfig poolConfig = new JedisPoolConfig();
                jedisPool = new JedisPool(poolConfig, ip, port, timeout, password);
            }
        } catch (Exception e) {
            log.error("Malformed server address", e);
        }
    }

 public String get(final String key) {
        try (Jedis jedis = jedisPool.getResource()) {
           String value = jedis.get(key);
            return value;
        } catch (Exception ex) {
            log.error("Exception caught in get", ex);
        }
        return null;
    }


 public void populateRedisMap(RedisClient redisClient) {
        RedisIterator iterator = redisClient.iterator(Integer.parseInt(scanCount), matchPattern, scan);

        List<String> keys = iterator.next();
        while (iterator.hasNext()) {
            keys.addAll(iterator.next());
        }

        log.info("Keys count:{}", keys.size());

        for (String key : keys) {
            if(redisClient.get(key) instanceof String) {
                String value = redisClient.get(key);
                redisMap.put(key, value);
            }
            else{
                Object keyValue = redisClient.get(key);
                log.info("Value {0} of Type {1} found for key {2}", keyValue, keyValue.getClass(), key);
            }
        }

        log.info("Records count in Redis Map:{}", redisMap.size());
    }

The above code works.上面的代码有效。 I get 80k keys and it loops through each key to get the value and the hash map is populated with key and value.我得到 80k 个键,它遍历每个键以获取值,并且 hash map 填充了键和值。 I haven't added the code for the Iterator here, I didn't think it is relevant to my question but I can added it if needed.我没有在这里添加迭代器的代码,我认为它与我的问题无关,但如果需要我可以添加它。

My issue here is the performance, it takes a long time (>30 mins) for this hash map to be populated.我的问题是性能,填充此 hash map 需要很长时间(> 30 分钟)。 The iterator part to get the list of keys takes few seconds, it is the for loop to get the values that take time.获取键列表的迭代器部分需要几秒钟,获取值需要时间的是 for 循环。 My question is, is there a more efficient way to get all the key value data from redis cache?我的问题是,有没有更有效的方法从 redis 缓存中获取所有键值数据? I couldn't find any other command I could use in Redis documentation but wanted to check if I am missing something.我在 Redis 文档中找不到可以使用的任何其他命令,但想检查我是否遗漏了什么。

You can chunk your keys into arrays [Note1] of keys and call mget .您可以将您的keys分块为密钥的 arrays [注 1]并调用mget

    public List<String> get(final String[] keys) {
        try (Jedis jedis = jedisPool.getResource()) {
            List<String> values = jedis.mget(keys);
            return values;
        } catch (Exception ex) {
            log.error("Exception caught in mget", ex);
        }
        return null;
    }

Note1: The arrays can be of size 100 or 1000 or any other size depending on your system and preference.注意 1:arrays 的尺寸可以为 100 或 1000 或任何其他尺寸,具体取决于您的系统和偏好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM