diff options
author | antirez <antirez@gmail.com> | 2018-09-27 11:22:43 +0200 |
---|---|---|
committer | antirez <antirez@gmail.com> | 2018-09-27 11:22:43 +0200 |
commit | 20f047965c1382219616e27afa51d72c0001895c (patch) | |
tree | 971c0d067a6e2b9140995dfe56165826b9728d2d | |
parent | 880ca077195bdb483d6d7e7b0f8c713dc7a16b67 (diff) | |
download | redis-20f047965c1382219616e27afa51d72c0001895c.tar.gz |
Modules: hellodict example WIP #1: GET command.
-rw-r--r-- | src/modules/hellodict.c | 18 |
1 files changed, 18 insertions, 0 deletions
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 <key> + * + * 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); |