Better description of redisCommandArgv() #1138
Replies: 2 comments 2 replies
-
I can add an example that uses In the meantime, I created a simple function that uses it. Essentially you are just sending two arrays with the command and arguments you wish to execute on Redis. The first array is the arguments themselves, and the second array is the length of each argument. In general using this command will be faster than the static void example_argv_command(redisContext *c, size_t n) {
char **argv, tmp[24];
size_t *argvlen;
redisReply *reply;
/* We're allocating two additional elements for command and key */
argv = malloc(sizeof(*argv) * (2 + n));
argvlen = malloc(sizeof(*argvlen) * (2 + n));
/* First the command */
argv[0] = (char*)"RPUSH";
argvlen[0] = sizeof("RPUSH") - 1;
/* Now our key */
argv[1] = (char*)"argvlist";
argvlen[1] = sizeof("argvlist") - 1;
/* Now add the entries we wish to add to the list */
for (size_t i = 2; i < (n + 2); i++) {
argvlen[i] = snprintf(tmp, sizeof(tmp), "argv-element-%zu", i - 2);
argv[i] = strdup(tmp);
}
/* Execute the command using redisCommandArgv. We're sending the arguments with
* two explicit arrays. One for each argument's string, and the other for its
* length. */
reply = redisCommandArgv(c, n + 2, (const char **)argv, (const size_t*)argvlen);
if (reply == NULL || c->err) {
fprintf(stderr, "Error: Couldn't execute redisCommandArgv\n");
exit(1);
}
if (reply->type == REDIS_REPLY_INTEGER) {
printf("%s reply: %lld\n", argv[0], reply->integer);
}
freeReplyObject(reply);
/* Clean up */
for (size_t i = 2; i < (n + 2); i++) {
free(argv[i]);
}
free(argv);
free(argvlen);
} |
Beta Was this translation helpful? Give feedback.
-
Thank you Michael, Your example is passing a set of string values for a list object... I was wondering if it's possible to pass different types of data (number vs string), with redisCommandArgv() ... I mean, to execute a command like:
=> Is this possible? Background: I want to write a C API for our solution on top of hiredis (basically, it's a set of wrapper functions to hiredis API) Seb |
Beta Was this translation helpful? Give feedback.
-
Hi,
Can someone write a more detailed description of redisCommandArgv() usage (or a sample)?
I don't get the idea.
Has redisCommandArgv() to be used with redisCommand() ?
Found no example in "examples".
I will dig into the source code but would be nice to have a better description in the README.
Thanks!
Seb
Beta Was this translation helpful? Give feedback.
All reactions