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