From 20f047965c1382219616e27afa51d72c0001895c Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 27 Sep 2018 11:22:43 +0200 Subject: Modules: hellodict example WIP #1: GET command. --- src/modules/hellodict.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/modules/hellodict.c b/src/modules/hellodict.c index d6a7ef547..b99f9f075 100644 --- a/src/modules/hellodict.c +++ b/src/modules/hellodict.c @@ -54,6 +54,20 @@ int cmd_SET(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { return RedisModule_ReplyWithSimpleString(ctx, "OK"); } +/* HELLODICT.GET + * + * Return the value of the specified key, or a null reply if the key + * is not defined. */ +int cmd_GET(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { + if (argc != 2) return RedisModule_WrongArity(ctx); + RedisModuleString *val = RedisModule_DictGet(Keyspace,argv[1],NULL); + if (val == NULL) { + return RedisModule_ReplyWithNull(ctx); + } else { + return RedisModule_ReplyWithString(ctx, val); + } +} + /* This function must be present on each Redis module. It is used in order to * register the commands into the Redis server. */ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { @@ -67,6 +81,10 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) cmd_SET,"write deny-oom",1,1,0) == REDISMODULE_ERR) return REDISMODULE_ERR; + if (RedisModule_CreateCommand(ctx,"hellodict.get", + cmd_GET,"readonly",1,1,0) == REDISMODULE_ERR) + return REDISMODULE_ERR; + /* Create our global dictionray. Here we'll set our keys and values. */ Keyspace = RedisModule_CreateDict(NULL); -- cgit v1.2.1