Debugging c access to Redis code

Source: Internet
Author: 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&NBSP;ARGC,&NBSP;CHAR&NBSP;**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 */   &Nbsp; 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&NBSP;API):  %s\n", &NBSP;REPLY-&GT;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;}

Debugging c access to Redis code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.