2010-05-18 15:11:09 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-04-19 20:39:26 +00:00
|
|
|
#include <hiredis.h>
|
2020-08-07 17:26:38 +00:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <winsock2.h> /* For struct timeval */
|
|
|
|
#endif
|
2010-05-18 15:11:09 +00:00
|
|
|
|
2013-07-11 08:25:48 +00:00
|
|
|
int main(int argc, char **argv) {
|
2018-12-05 12:02:26 +00:00
|
|
|
unsigned int j, isunix = 0;
|
2010-09-24 16:48:07 +00:00
|
|
|
redisContext *c;
|
2010-05-18 15:11:09 +00:00
|
|
|
redisReply *reply;
|
2013-07-11 08:25:48 +00:00
|
|
|
const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1";
|
2018-12-05 12:02:26 +00:00
|
|
|
|
|
|
|
if (argc > 2) {
|
|
|
|
if (*argv[2] == 'u' || *argv[2] == 'U') {
|
|
|
|
isunix = 1;
|
|
|
|
/* in this case, host is the path to the unix socket */
|
|
|
|
printf("Will connect to unix socket @%s\n", hostname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-11 08:25:48 +00:00
|
|
|
int port = (argc > 2) ? atoi(argv[2]) : 6379;
|
2010-05-18 15:11:09 +00:00
|
|
|
|
2011-02-04 14:26:28 +00:00
|
|
|
struct timeval timeout = { 1, 500000 }; // 1.5 seconds
|
2018-12-05 12:02:26 +00:00
|
|
|
if (isunix) {
|
|
|
|
c = redisConnectUnixWithTimeout(hostname, timeout);
|
|
|
|
} else {
|
|
|
|
c = redisConnectWithTimeout(hostname, port, timeout);
|
|
|
|
}
|
2013-01-22 09:16:30 +00:00
|
|
|
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");
|
|
|
|
}
|
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-12-02 18:30:21 +00:00
|
|
|
printf("PING: %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 */
|
2013-07-11 06:05:39 +00:00
|
|
|
reply = redisCommand(c,"SET %b %b", "bar", (size_t) 3, "hello", (size_t) 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];
|
|
|
|
|
2015-10-05 13:22:00 +00:00
|
|
|
snprintf(buf,64,"%u",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);
|
|
|
|
|
2013-01-24 14:24:01 +00:00
|
|
|
/* Disconnects and frees the context */
|
|
|
|
redisFree(c);
|
|
|
|
|
2010-05-18 15:11:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|