我是靠谱客的博主 合适花卷,最近开发中收集的这篇文章主要介绍redis-cli中的--bigkeys选项是怎么找大键的,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

在使用redis-cli的时候,有个bigkey选项可以输出大键的信息,redis是怎么筛选的?
下面是具体的代码
redis-cli.c

static void findBigKeys(void) {
#存放5中类型的最大的值,与当前的大小进行对比
    unsigned long long biggest[5] = {0,0,0,0,0};
    unsigned long long samples = 0; 采样次数
    redisReply *reply1, *reply2, *reply3 = NULL;
    char *sizecmd, *typename[] = {"string","list","set","hash","zset"}; 查看这5中数据类型
    char *typeunit[] = {"bytes","items","members","fields","members"};
    int type;

    printf("n# Press ctrl+c when you have had enough of it... :)n");
    printf("# You can use -i 0.1 to sleep 0.1 sec every 100 sampled keysn");
    printf("# in order to reduce server load (usually not needed).nn");
    while(1) {
        /* Sample with RANDOMKEY */
        reply1 = redisCommand(context,"RANDOMKEY");返回随机key
        if (reply1 == NULL) {
            fprintf(stderr,"nI/O errorn");
            exit(1);
        } else if (reply1->type == REDIS_REPLY_ERROR) {
            fprintf(stderr, "RANDOMKEY error: %sn",
                reply1->str);
            exit(1);
        } else if (reply1->type == REDIS_REPLY_NIL) {
            fprintf(stderr, "It looks like the database is empty!n");
            exit(1);
        }

        /* Get the key type */
        reply2 = redisCommand(context,"TYPE %s",reply1->str); 得到key的类型
        assert(reply2 && reply2->type == REDIS_REPLY_STATUS);
        samples++;

        /* Get the key "size" */ 针对不同的类型计算大小的方法
        if (!strcmp(reply2->str,"string")) {
            sizecmd = "STRLEN"; 
            type = TYPE_STRING;
        } else if (!strcmp(reply2->str,"list")) {
            sizecmd = "LLEN";
            type = TYPE_LIST;
        } else if (!strcmp(reply2->str,"set")) {
            sizecmd = "SCARD";
            type = TYPE_SET;
        } else if (!strcmp(reply2->str,"hash")) {
            sizecmd = "HLEN";
            type = TYPE_HASH;
        } else if (!strcmp(reply2->str,"zset")) {
            sizecmd = "ZCARD";
            type = TYPE_ZSET;
        } else if (!strcmp(reply2->str,"none")) {
            freeReplyObject(reply1);
            freeReplyObject(reply2);
            freeReplyObject(reply3);
            continue;
        } else {
            fprintf(stderr, "Unknown key type '%s' for key '%s'n",
                reply2->str, reply1->str);
            exit(1);
        }

        reply3 = redisCommand(context,"%s %s", sizecmd, reply1->str);获取key大小
        if (reply3 && reply3->type == REDIS_REPLY_INTEGER) {
            if (biggest[type] < reply3->integer) {
                printf("Biggest %-6s found so far '%s' with %llu %s.n",
                    typename[type], reply1->str,
                    (unsigned long long) reply3->integer,
                    typeunit[type]);
                biggest[type] = reply3->integer;
            }
        }

        if ((samples % 1000000) == 0)
            printf("(%llu keys sampled)n", samples);

        if ((samples % 100) == 0 && config.interval)
            usleep(config.interval);

        freeReplyObject(reply1);
        freeReplyObject(reply2);
        if (reply3) freeReplyObject(reply3);
    }
}

可见redis在计算大key的时候不是以key的占用内存大小来计算的,string是使用STRLEN方法来计算,list是llen来计算。

最后

以上就是合适花卷为你收集整理的redis-cli中的--bigkeys选项是怎么找大键的的全部内容,希望文章能够帮你解决redis-cli中的--bigkeys选项是怎么找大键的所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部