aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/hiredis/example.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@rambler-co.ru>2011-12-09 19:13:00 +0300
committerVsevolod Stakhov <vsevolod@rambler-co.ru>2011-12-09 19:13:00 +0300
commit7ca658607cdeebe0cecbeb310cf2c99af800306f (patch)
tree0fdca11c2ac98ed15ae54414cd92cd1be8697f85 /contrib/hiredis/example.c
parent46ceb4ad592937ac210d1fcdfe5ee0f53d317b39 (diff)
downloadrspamd-7ca658607cdeebe0cecbeb310cf2c99af800306f.tar.gz
rspamd-7ca658607cdeebe0cecbeb310cf2c99af800306f.zip
Add detecting of libhiredis for communicating with kvstorage.
Add internal hiredis if it is not found in system.
Diffstat (limited to 'contrib/hiredis/example.c')
-rw-r--r--contrib/hiredis/example.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/contrib/hiredis/example.c b/contrib/hiredis/example.c
new file mode 100644
index 000000000..90ff9ed5e
--- /dev/null
+++ b/contrib/hiredis/example.c
@@ -0,0 +1,68 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "hiredis.h"
+
+int main(void) {
+ unsigned int j;
+ redisContext *c;
+ redisReply *reply;
+
+ struct timeval timeout = { 1, 500000 }; // 1.5 seconds
+ c = redisConnectWithTimeout((char*)"127.0.0.2", 6379, timeout);
+ if (c->err) {
+ printf("Connection error: %s\n", c->errstr);
+ 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", 3, "hello", 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);
+
+ return 0;
+}