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->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