luaでresty.redis使ってるんですが、どうも安定しないのでhiredisを使おうと思う。
まずはhsetとhgetallのサンプル書いて動作確認してみた。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hiredis/hiredis.h>
int main(void) {
char *key1 = "aaaa";
char *key2 = "bbbb";
char *val = "1111";
redisReply *reply;
int i;
redisContext *c = redisConnect("127.0.0.1", 6379);
if (c->err) {
printf("redisConnect Error: %s\n", c->errstr);
return (-1);
}
// HSET
reply = redisCommand(c, "HSET %s %s %s", key1, key2, val);
freeReplyObject(reply);
// HGETALL
reply = redisCommand(c, "HGETALL %s", key1);
if ( reply->type == REDIS_REPLY_ERROR ) {
printf("redisCommand Error: %s\n", c->errstr);
}
else if (reply->type == REDIS_REPLY_ARRAY) {
printf("hgetall: %s\n", key1);
for ( i=0; i<reply->elements; i++ ) {
if ( (i % 2) == 0 ) {
printf("hkey: %s, ", reply->element[i]->str);
}
else {
printf("value: %s\n", reply->element[i]->str);
}
}
}
freeReplyObject(reply);
return (0);
}
$ ./a.out
hgetall: aaaa
hkey: bbbb, value: 1111