【问题标题】:Strange Redis SET command behaviour奇怪的 Redis SET 命令行为
【发布时间】:2012-07-03 20:49:07
【问题描述】:

我正在编写一个简单的程序来测试 Redis:

#include <hiredis.h>
#include <cstdio>

int main()
{
    redisContext * c = redisConnect("127.0.0.1", 6379);

    redisReply * reply;

    reply = redisCommand(c, "FLUSHALL");
    freeReplyObject(reply);

    for(long int i = 0; i <= 1000000; i++)
    {   
        char query[64];
        sprintf(query, "SET %ld \"string%ld\"", i, i); 

        reply = redisCommand(c, query);
        freeReplyObject(reply);

        if( !(i % 100000) )
        {   
            reply = redisCommand(c, "DBSIZE");
            int res = reply->integer;
            freeReplyObject(reply);

            printf("%s\n", query);
            printf("dbsize: %d\n", res);
        }   
    }   

    redisFree(c);
}

它应该将 1000000 个键放入 db,但输出如下:

SET 0 "string0"
dbsize: 1
SET 100000 "string100000"
dbsize: 100001
SET 200000 "string200000"
dbsize: 200001
SET 300000 "string300000"
dbsize: 300001
SET 400000 "string400000"
dbsize: 400001
SET 500000 "string500000"
dbsize: 500001
SET 600000 "string600000"
dbsize: 600001
SET 700000 "string700000"
dbsize: 700001
SET 800000 "string800000"
dbsize: 779479
SET 900000 "string900000"
dbsize: 779479
SET 1000000 "string1000000"
dbsize: 779479

所以在某一时刻(通常大约在 600000 个键之后)Redis 停止添加记录。这种行为的原因是什么?

【问题讨论】:

    标签: redis


    【解决方案1】:

    您不检查代码中的错误,因此很难诊断问题。您应该在释放回复对象之前分析输出。

    最可能的原因是您的 Redis 实例已配置最大内存限制,而您已达到此限制。您可能需要检查配置文件,或使用以下命令:

    config get maxmemory
    

    零值表示没有限制。否则,限制以字节数表示。

    【讨论】:

    • 谢谢,这真的很有帮助。错误检查没有用 - 没有错误。
    猜你喜欢
    • 2015-03-28
    • 1970-01-01
    • 2012-04-05
    • 2012-06-21
    • 2022-01-05
    • 2015-12-07
    • 2015-01-11
    • 2012-10-22
    • 2019-01-18
    相关资源
    最近更新 更多