gpt4 book ai didi

redis - HiRedis::如何使用 LPUSH 在 Redis 列表中插入一个空字符串

转载 作者:可可西里 更新时间:2023-11-01 11:28:44 29 4
gpt4 key购买 nike

我正在使用 hiredis 库的 redisCommand 来做这样的事情:

LPUSH list1 a b "" c d "" e

其中“”表示我想向列表中插入空元素。当我从 redis 的命令行执行它时它工作正常但是当我将它作为 hiredis 上的命令传递时,它不起作用并且元素最终是“”而不是空的。有解决办法吗?

这是我调用 redisCommand 的方式:

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

我也尝试过使用单引号、反斜杠等

最佳答案

If the number of elements you want to push to list is fixed: use redisCommand with the formatted parameters

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);

If the number of elements is dynamic: use 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);

关于redis - HiRedis::如何使用 LPUSH 在 Redis 列表中插入一个空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42706807/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com