I develop a redis module that call the native redis DUMP command.
But when i call dump command on existing key, nothing is return but if i replace the DUMP command by GET the value is returned
Here is the code :
#include "redismodule.h"
#include <stdio.h>
#include <stdlib.h>
int mydump_command(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
REDISMODULE_NOT_USED(argc);
RedisModule_AutoMemory(ctx);
RedisModuleCallReply *rep = RedisModule_Call(ctx, "DUMP", "s", argv[1]);
const char *value = RedisModule_CallReplyStringPtr(rep, NULL);
RedisModule_Log(ctx, "warning", value);
RedisModule_ReplyWithSimpleString(ctx, value);
return REDISMODULE_OK;
}
int myget_command(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
REDISMODULE_NOT_USED(argc);
RedisModule_AutoMemory(ctx);
RedisModuleCallReply *rep = RedisModule_Call(ctx, "GET", "s", argv[1]);
const char *value = RedisModule_CallReplyStringPtr(rep, NULL);
RedisModule_Log(ctx, "warning", value);
RedisModule_ReplyWithSimpleString(ctx, value);
return REDISMODULE_OK;
}
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (RedisModule_Init(ctx, "module_test", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if (RedisModule_CreateCommand(ctx, "mydump", mydump_command, "write deny-oom", 1, 1, 1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if (RedisModule_CreateCommand(ctx, "myget", myget_command, "write deny-oom", 1, 1, 1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
return REDISMODULE_OK;
}
In redis-cli i get the following result :
127.0.0.1:6379> SET foo bar
OK
127.0.0.1:6379> GET foo
"bar"
127.0.0.1:6379> DUMP foo
"\x00\x03bar\b\x00_\x93\xa5\xdfG\x7fw/"
127.0.0.1:6379> mydump foo
127.0.0.1:6379> myget foo
bar
127.0.0.1:6379>
Note that with my custom command, redis-cli don't disply double quotes.
Can someone help me know why calling DUMP command return nothing.
The problem in your code is that you're treating the reply as a simple string, whereas Redis' may be binary. While GET returns a simple string value (as you SET foo bar), DUMPs encoding is binary. The following works:
int mydump_command(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
REDISMODULE_NOT_USED(argc);
RedisModule_AutoMemory(ctx);
RedisModuleCallReply *rep = RedisModule_Call(ctx, "DUMP", "s", argv[1]);
size_t vlen;
const char *value = RedisModule_CallReplyStringPtr(rep, &vlen);
RedisModule_ReplyWithStringBuffer(ctx, value, vlen);
return REDISMODULE_OK;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With