2011-12-16 119 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

相關問題