2017-03-09 49 views
0

我使用hiredis庫redisCommand做這樣的事情,以插入的Redis列表中的空白字符串:HiRedis ::如何使用LPUSH

LPUSH list1 a b "" c d "" e 

哪裏「」意味着我要插入到空元素名單。當我從redis上的命令行執行它時工作正常,但是當我將它作爲hiredis上的命令傳遞時,它不起作用,並且元素最終變爲「」而不是空的。 任何解決辦法?

以下是我叫redisCommand:

reply = (redisReply *) redisCommand(c,"LPUSH list1 a b c "" c d "" e); 

我試圖把單引號,反斜槓等也

+0

你怎麼叫redisComand?任何示例? –

+0

添加了示例 –

回答

0

您可以使用Redis的中二進制安全字符串。 使用LPUSH命令不斷向列表添加二進制字符串,如下所示:

redisReply * reply = redisCommand(context,「LPUSH list1%b%b%b」,「a」,strlen(「a」), 「」,0,「b」,strlen(「b」));

輸出將是:

127.0.0.1:6379> lrange list1 0 -1 
1) "b" 
2) "" 
3) "a" 

HTH, Swanand

+0

問題是該列表非常龐大,我只想連接到redis一次 –

0

如果你想推到列表是固定的元素個數:使用redisCommand與格式化參數

const char *list = "list-name"; 
const char *non_empty_val = "value"; 
const char *empty_val = ""; 
/* or use %b to push binary element, as the other answer mentioned. */ 
redisReply *reply = (redisReply*)redisCommand(redis, 
          "lpush %s %s %s", list, non_empty_val, empty_val); 

如果要素的數量是動態的:使用redisCommandArgv

int argc = 4; /* number of arguments including command name. */ 

const char **argv = (const char**)malloc(sizeof(const char**) * argc); 
argv[0] = strdup("lpush"); 
argv[1] = strdup(list); 
argv[2] = strdup(non_empty_val); 
argv[3] = strdup(empty_val); 

/* specify the length of each argument. */ 
size_t *argv_len = (size_t*)malloc(sizeof(size_t) * argc); 
for (int i = 0; i < argc; ++i) 
    argv_len[i] = strlen(argv[i]); 

redisReply *reply = (redisReply*)redisCommandArgv(redis, argc, argv, argv_len);