雷迪斯 : Is there a limit to the number of keys that i can store?

标签 redis jedis

首先是上下文,我正在尝试将 Redis 用作具有持久性支持的内存存储。我需要在 Redis 哈希中存储大量对象(数百万)。

同时,我不希望我的redis实例消耗太多内存。所以我将 redis.conf 中的 maxmemory 属性设置为 100mb。我已将 maxmemory-policy 设置为 allkeys-random 持久化模式为 AOF,fysnc 为每秒。

现在我面临的问题是,每次我尝试在散列中存储超过 20 万个对象时,散列都会重置(即散列中所有现有的键值都消失)。我通过对 redis-cli 中的哈希使用 hlen 命令来确认这一点。

在我试图存储的对象下方找到

public class Employee implements Serializable {

private static final long serialVersionUID = 1L;
int id;
String name;
String department;
String address;

    /* Getters and Setters */

    /* Hashcode - Generates hashcode (key) for each object */
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((address == null) ? 0 : address.hashCode());
    result = prime * result + ((department == null) ? 0 : department.hashCode());
    result = prime * result + id;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
}
}

此外,在下面找到存储到 redis 的代码(我使用 Jedis 与 Redis 交互)

    JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "localhost");
    Jedis jedis = (Jedis) jedisPool.getResource();

    System.out.println("Starting....");
    for(int i=0;i<1000000;i++) {

             /* Converting object to byte array */
        Employee employee = new Employee(i, "Arun Jolly", "IT", "SomeCompany");
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(employee);
        byte[] value = byteArrayOutputStream.toByteArray();

        /* Creating key in byte array format using hashCode() */
        ByteBuffer buffer = ByteBuffer.allocate(128);
        buffer.putInt(employee.hashCode());
        byte[] field = buffer.array();

            /* Specyfying the Redis Hash in byte array format */ 
        String tableName = "Employee_Details";
        byte[] key = tableName.getBytes();

        jedis.hset(key, field, value);
        System.out.println("Stored Employee "+i);
    }

我错过了什么吗?

这是否意味着一旦达到 maxmemory,redis 就不会换出到磁盘(它是否试图将所有键值保存在内存中?)这是否意味着我必须根据增加量逐步增加 maxmemory 限制我可能必须存储的键值对的数量?

最佳答案

Am I missing something?

是的。 Redis 是一个纯内存存储,具有持久性选项。一切都必须适合内存。

Does this mean that redis does not swap out to the disk once the maxmemory is reached.

准确地说。

Is it trying to hold all the key values in memory?

键和值,是的。

Does it mean that I have to incrementally increase the max memory limit according to the increase in the number of key-value pairs that I might have to store?

是的,您需要预先决定分配给 Redis 的内存量。

如果您的内存有限,则使用基于磁盘的存储会更好。

关于雷迪斯 : Is there a limit to the number of keys that i can store?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17805847/

相关文章:

ruby - 在 ruby​​ 中的特定时间后使变量过期

spring - 使用 Spring Data Redis 进行文本搜索

java - 绝地武士-Redis : Does maximum number of clients increases chances of response time from Redis?

error-handling - Spring Redis 错误处理

redis - 删除 100 万个 key 时读取超时异常

session - Spring boot 2.0/Tomcat 8.5 - 日志文件中的 session 属性

java - RedisTemplate 过期不起作用

Redis 可用性和 CAP 定理

Redis 本地主机限制和成本

redis - 为什么 Redis key 不会过期?