2011-12-16 179 views
0

我想知道我是否可以在redis中保存C结构。但我不知道如何得到它,因为hiredis中的example.c没有提到。我们可以通过hiredis保存Redis中的C结构吗?我可以保存但不能得到它

我使用二进制安全字符串将结构保存到Redis。我得到了一个+ OK,这意味着我保存正确。

保存代码是在这里

... 
reply = redisCommand(c, "HMSET %s stat %b", rcvgetattr.pathname, sndgetattr.stbuf, sizeof(struct stat)); 
printf("Save status %s\n", reply->str);//that shows +OK 
freeReplyObject(reply); 

,然后当我试图让我的数据备份,我用

... 
reply = redisCommand(c, "HMGET %s stat", rcvgetattr.pathname); 
printf("status %s\n", reply->str); 
freeReplyObject(reply); 

因为我不知道哪个部分包含我的结构,所以我用gdb并尝试找出它。我使用disp (struct stat)reply->strdisp (struct stat)reply->element->str等命令来查看数据是否与我刚刚保存的数据相同。但我失败了。

有谁知道它的数据存储在哪里?

回答

1

我认为你遇到的问题是HMGET返回一个数组,而不是一个字符串。尝试使用reply->element[0]->str

此示例代码还可以帮助

typedef struct mytest { 
    int myInt; 
    long myLong; 
} mytest; 

// ... 
mytest t; 
t.myInt = 5; t.myLong = 123451; 
reply = redisCommand(c, "HMSET %s stat %b", "mykey", &t, sizeof(mytest)); 
printf("Save status %s\n", reply->str);//that shows +OK 
freeReplyObject(reply); 

reply = redisCommand(c, "HMGET %s stat", "mykey"); 
mytest* response = reply->element[0]->str; 
printf("status %d %ld\n", response->myInt, response->myLong); 
freeReplyObject(reply); 
+0

已经解决了!它是 - > str,但是当使用%b时,我需要传递一个指针给它,而不是结构本身 – bxshi 2011-12-16 12:37:08

相关问题