ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

C语言读取redis lrange 实现

2021-09-06 22:02:33  阅读:195  来源: 互联网

标签:ch socket read items redis C语言 lrange str



/*
 * Get the range of items in a list from 'start' to 'end'.
 * This function allocates memory. An array of pointers and all the strings pointed to by it
 * are dynamically allocated. redis_free_array_results() has to be called to free this
 * allocated memory by the caller.
 * */

int redis_list_get_range(char *key, int start, int end, char ***items, int *items_count) {
    /*
     *  The Redis protocol is elegantly simple. The following is a response for an array
     *  that has 3 elements (strings):
     *  Example response:
     *  *3\r\n$5\r\nHello\r\n$6\r\nLovely\r\n\$5\r\nWorld\r\n
     *
     *  What it means:
     *  *3      -> Array with 3 items
     *  $5      -> string with 5 characters
     *  Hello   -> actual string
     *  $6      -> string with 6 characters
     *  Lovely  -> actual string
     *  $5      -> string with 5 characters
     *  World   -> actual string
     *
     *  A '\r\n' (carriage return + line feed) sequence is used as the delimiter.
     *  Now, you should be able to understand why we're doing what we're doing in this function
     * */

    char cmd_buf[1024]="", start_str[16], end_str[16];
    sprintf(start_str, "%d", start);
    sprintf(end_str, "%d", end);
    sprintf(cmd_buf, "*4\r\n$6\r\nLRANGE\r\n$%ld\r\n%s\r\n$%ld\r\n%s\r\n$%ld\r\n%s\r\n", strlen(key), key, strlen(start_str), start_str, strlen(end_str), end_str);
    write(redis_socket_fd, cmd_buf, strlen(cmd_buf));

    /* Find out the length of the array */
    char ch;
    read(redis_socket_fd, &ch, 1);
    if (ch != '*')
        return -1;

    int returned_items = 0;
    while (1) {
        read(redis_socket_fd, &ch, 1);
        if (ch == '\r') {
            /* Read the next '\n' character */
            read(redis_socket_fd, &ch, 1);
            break;
        }
        returned_items = (returned_items * 10) + (ch - '0');
    }
    /* Allocate the array that will hold a pointer each for
     * every element in the returned list */
    *items_count = returned_items;
    char **items_holder = malloc(sizeof(char *) * returned_items);
    *items = items_holder;

    /*
     * We now know the length of the array. Let's loop that many iterations
     * and grab those strings, allocating a new chunk of memory for each one.
     * */
    for (int i = 0; i < returned_items; i++) {
        /* read the first '$' */
        read(redis_socket_fd, &ch, 1);
        int str_size = 0;
        while (1) {
            read(redis_socket_fd, &ch, 1);
            if (ch == '\r') {
                /* Read the next '\n' character */
                read(redis_socket_fd, &ch, 1);
                break;
            }
            str_size = (str_size * 10) + (ch - '0');
        }
        char *str = malloc(sizeof(char) * str_size + 1);
        items_holder[i] = str;
        read(redis_socket_fd, str, str_size);
        str[str_size] = '\0';
        /* Read the '\r\n' chars */
        read(redis_socket_fd, &ch, 1);
        read(redis_socket_fd, &ch, 1);
    }
}

标签:ch,socket,read,items,redis,C语言,lrange,str
来源: https://blog.csdn.net/qfzhangwei/article/details/120145929

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有