2010-05-18 15:11:09 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "hiredis.h"
|
|
|
|
|
|
|
|
int main(void) {
|
2010-05-18 15:45:36 +00:00
|
|
|
unsigned int j;
|
2010-09-24 16:48:07 +00:00
|
|
|
redisContext *c;
|
2010-05-18 15:11:09 +00:00
|
|
|
redisReply *reply;
|
|
|
|
|
2010-10-31 11:57:32 +00:00
|
|
|
c = redisConnect((char*)"127.0.0.1", 6379);
|
2010-11-02 15:36:38 +00:00
|
|
|
if (c->err) {
|
|
|
|
printf("Connection error: %s\n", c->errstr);
|
2010-05-18 15:11:09 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PING server */
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"PING");
|
2010-10-31 11:57:32 +00:00
|
|
|
printf("PONG: %s\n", reply->str);
|
2010-05-18 15:11:09 +00:00
|
|
|
freeReplyObject(reply);
|
|
|
|
|
|
|
|
/* Set a key */
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"SET %s %s", "foo", "hello world");
|
2010-10-31 11:57:32 +00:00
|
|
|
printf("SET: %s\n", reply->str);
|
2010-05-18 15:11:09 +00:00
|
|
|
freeReplyObject(reply);
|
|
|
|
|
|
|
|
/* Set a key using binary safe API */
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"SET %b %b", "bar", 3, "hello", 5);
|
2010-10-31 11:57:32 +00:00
|
|
|
printf("SET (binary API): %s\n", reply->str);
|
2010-05-18 15:11:09 +00:00
|
|
|
freeReplyObject(reply);
|
|
|
|
|
2010-05-18 16:13:25 +00:00
|
|
|
/* Try a GET and two INCR */
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"GET foo");
|
2010-10-31 11:57:32 +00:00
|
|
|
printf("GET foo: %s\n", reply->str);
|
2010-05-18 16:13:25 +00:00
|
|
|
freeReplyObject(reply);
|
|
|
|
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"INCR counter");
|
2010-05-18 16:13:25 +00:00
|
|
|
printf("INCR counter: %lld\n", reply->integer);
|
|
|
|
freeReplyObject(reply);
|
|
|
|
/* again ... */
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"INCR counter");
|
2010-05-18 16:13:25 +00:00
|
|
|
printf("INCR counter: %lld\n", reply->integer);
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
2010-05-18 15:45:36 +00:00
|
|
|
/* Create a list of numbers, from 0 to 9 */
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"DEL mylist");
|
2010-05-18 16:13:25 +00:00
|
|
|
freeReplyObject(reply);
|
2010-05-18 15:45:36 +00:00
|
|
|
for (j = 0; j < 10; j++) {
|
|
|
|
char buf[64];
|
|
|
|
|
|
|
|
snprintf(buf,64,"%d",j);
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"LPUSH mylist element-%s", buf);
|
2010-05-18 15:45:36 +00:00
|
|
|
freeReplyObject(reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Let's check what we have inside the list */
|
2010-09-24 16:48:07 +00:00
|
|
|
reply = redisCommand(c,"LRANGE mylist 0 -1");
|
2010-05-18 15:45:36 +00:00
|
|
|
if (reply->type == REDIS_REPLY_ARRAY) {
|
|
|
|
for (j = 0; j < reply->elements; j++) {
|
2010-10-31 11:57:32 +00:00
|
|
|
printf("%u) %s\n", j, reply->element[j]->str);
|
2010-05-18 15:45:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
2010-05-18 15:11:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|