]> git.saurik.com Git - redis.git/blob - deps/hiredis/example.c
2506f39cd7be5225b0a23e4ff95b1c50a308061d
[redis.git] / deps / hiredis / example.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "hiredis.h"
6
7 int main(void) {
8 unsigned int j;
9 redisContext *c;
10 redisReply *reply;
11
12 c = redisConnect((char*)"127.0.0.1", 6379);
13 if (c->err) {
14 printf("Connection error: %s\n", c->errstr);
15 exit(1);
16 }
17
18 /* PING server */
19 reply = redisCommand(c,"PING");
20 printf("PING: %s\n", reply->str);
21 freeReplyObject(reply);
22
23 /* Set a key */
24 reply = redisCommand(c,"SET %s %s", "foo", "hello world");
25 printf("SET: %s\n", reply->str);
26 freeReplyObject(reply);
27
28 /* Set a key using binary safe API */
29 reply = redisCommand(c,"SET %b %b", "bar", 3, "hello", 5);
30 printf("SET (binary API): %s\n", reply->str);
31 freeReplyObject(reply);
32
33 /* Try a GET and two INCR */
34 reply = redisCommand(c,"GET foo");
35 printf("GET foo: %s\n", reply->str);
36 freeReplyObject(reply);
37
38 reply = redisCommand(c,"INCR counter");
39 printf("INCR counter: %lld\n", reply->integer);
40 freeReplyObject(reply);
41 /* again ... */
42 reply = redisCommand(c,"INCR counter");
43 printf("INCR counter: %lld\n", reply->integer);
44 freeReplyObject(reply);
45
46 /* Create a list of numbers, from 0 to 9 */
47 reply = redisCommand(c,"DEL mylist");
48 freeReplyObject(reply);
49 for (j = 0; j < 10; j++) {
50 char buf[64];
51
52 snprintf(buf,64,"%d",j);
53 reply = redisCommand(c,"LPUSH mylist element-%s", buf);
54 freeReplyObject(reply);
55 }
56
57 /* Let's check what we have inside the list */
58 reply = redisCommand(c,"LRANGE mylist 0 -1");
59 if (reply->type == REDIS_REPLY_ARRAY) {
60 for (j = 0; j < reply->elements; j++) {
61 printf("%u) %s\n", j, reply->element[j]->str);
62 }
63 }
64 freeReplyObject(reply);
65
66 return 0;
67 }