調試C訪問redis的代碼

來源:互聯網
上載者:User

標籤:

gcc example.c -o example -I../ -L../ -lhiredisldconfig /usr/libmv /usr/lib/hiredis/libhiredis.so.0.13 /usr/lib/libhiredis.so.0.13 #include <stdio.h>#include <stdlib.h>#include <string.h>#include "hiredis.h"int main(int argc, char **argv) {    unsigned int j;    redisContext *c;    redisReply *reply;    const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1";    int port = (argc > 2) ? atoi(argv[2]) : 6379;    struct timeval timeout = { 1, 500000 }; // 1.5 seconds    c = redisConnectWithTimeout(hostname, port, timeout);    if (c == NULL || c->err) {        if (c) {            printf("Connection error: %s\n", c->errstr);            redisFree(c);        } else {            printf("Connection error: can‘t allocate redis context\n");        }        exit(1);    }    /* PING server */    reply = redisCommand(c,"PING");    printf("PING: %s\n", reply->str);    freeReplyObject(reply);    /* Set a key */    reply = redisCommand(c,"SET %s %s", "foo", "hello world");    printf("SET: %s\n", reply->str);    freeReplyObject(reply);    /* Set a key using binary safe API */    reply = redisCommand(c,"SET %b %b", "bar", (size_t) 3, "hello", (size_t) 5);    printf("SET (binary API): %s\n", reply->str);    freeReplyObject(reply);    /* Try a GET and two INCR */    reply = redisCommand(c,"GET foo");    printf("GET foo: %s\n", reply->str);    freeReplyObject(reply);    reply = redisCommand(c,"INCR counter");    printf("INCR counter: %lld\n", reply->integer);    freeReplyObject(reply);    /* again ... */    reply = redisCommand(c,"INCR counter");    printf("INCR counter: %lld\n", reply->integer);    freeReplyObject(reply);    /* Create a list of numbers, from 0 to 9 */    reply = redisCommand(c,"DEL mylist");    freeReplyObject(reply);    for (j = 0; j < 10; j++) {        char buf[64];        snprintf(buf,64,"%d",j);        reply = redisCommand(c,"LPUSH mylist element-%s", buf);        freeReplyObject(reply);    }    /* Let‘s check what we have inside the list */    reply = redisCommand(c,"LRANGE mylist 0 -1");    if (reply->type == REDIS_REPLY_ARRAY) {        for (j = 0; j < reply->elements; j++) {            printf("%u) %s\n", j, reply->element[j]->str);        }    }    freeReplyObject(reply);    /* Disconnects and frees the context */    redisFree(c);    return 0;}

調試C訪問redis的代碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.