summaryrefslogtreecommitdiff
path: root/src/module.c
diff options
context:
space:
mode:
authorMeir Shpilraien (Spielrein) <meir@redis.com>2023-02-09 14:59:05 +0200
committerGitHub <noreply@github.com>2023-02-09 14:59:05 +0200
commit5c3938d5cc08b42acc99f314d92f9e0d5671f96e (patch)
tree0494f545c08dfe96af7379dceb35402f42198de1 /src/module.c
parent66bed3f220d3ae73449b6b9f89ae616e42eff2e4 (diff)
downloadredis-5c3938d5cc08b42acc99f314d92f9e0d5671f96e.tar.gz
Match REDISMODULE_OPEN_KEY_* flags to LOOKUP_* flags (#11772)
The PR adds support for the following flags on RedisModule_OpenKey: * REDISMODULE_OPEN_KEY_NONOTIFY - Don't trigger keyspace event on key misses. * REDISMODULE_OPEN_KEY_NOSTATS - Don't update keyspace hits/misses counters. * REDISMODULE_OPEN_KEY_NOEXPIRE - Avoid deleting lazy expired keys. * REDISMODULE_OPEN_KEY_NOEFFECTS - Avoid any effects from fetching the key In addition, added `RM_GetOpenKeyModesAll`, which returns the mask of all supported OpenKey modes. This allows the module to check, in runtime, which OpenKey modes are supported by the current Redis instance.
Diffstat (limited to 'src/module.c')
-rw-r--r--src/module.c38
1 files changed, 34 insertions, 4 deletions
diff --git a/src/module.c b/src/module.c
index 2eab04b2d..94f420b54 100644
--- a/src/module.c
+++ b/src/module.c
@@ -3783,17 +3783,29 @@ static void moduleInitKeyTypeSpecific(RedisModuleKey *key) {
* The return value is the handle representing the key, that must be
* closed with RM_CloseKey().
*
- * If the key does not exist and WRITE mode is requested, the handle
+ * If the key does not exist and REDISMODULE_WRITE mode is requested, the handle
* is still returned, since it is possible to perform operations on
* a yet not existing key (that will be created, for example, after
- * a list push operation). If the mode is just READ instead, and the
+ * a list push operation). If the mode is just REDISMODULE_READ instead, and the
* key does not exist, NULL is returned. However it is still safe to
* call RedisModule_CloseKey() and RedisModule_KeyType() on a NULL
- * value. */
+ * value.
+ *
+ * Extra flags that can be pass to the API under the mode argument:
+ * * REDISMODULE_OPEN_KEY_NOTOUCH - Avoid touching the LRU/LFU of the key when opened.
+ * * REDISMODULE_OPEN_KEY_NONOTIFY - Don't trigger keyspace event on key misses.
+ * * REDISMODULE_OPEN_KEY_NOSTATS - Don't update keyspace hits/misses counters.
+ * * REDISMODULE_OPEN_KEY_NOEXPIRE - Avoid deleting lazy expired keys.
+ * * REDISMODULE_OPEN_KEY_NOEFFECTS - Avoid any effects from fetching the key. */
RedisModuleKey *RM_OpenKey(RedisModuleCtx *ctx, robj *keyname, int mode) {
RedisModuleKey *kp;
robj *value;
- int flags = mode & REDISMODULE_OPEN_KEY_NOTOUCH? LOOKUP_NOTOUCH: 0;
+ int flags = 0;
+ flags |= (mode & REDISMODULE_OPEN_KEY_NOTOUCH? LOOKUP_NOTOUCH: 0);
+ flags |= (mode & REDISMODULE_OPEN_KEY_NONOTIFY? LOOKUP_NONOTIFY: 0);
+ flags |= (mode & REDISMODULE_OPEN_KEY_NOSTATS? LOOKUP_NOSTATS: 0);
+ flags |= (mode & REDISMODULE_OPEN_KEY_NOEXPIRE? LOOKUP_NOEXPIRE: 0);
+ flags |= (mode & REDISMODULE_OPEN_KEY_NOEFFECTS? LOOKUP_NOEFFECTS: 0);
if (mode & REDISMODULE_WRITE) {
value = lookupKeyWriteWithFlags(ctx->client->db,keyname, flags);
@@ -3811,6 +3823,23 @@ RedisModuleKey *RM_OpenKey(RedisModuleCtx *ctx, robj *keyname, int mode) {
return kp;
}
+/**
+ * Returns the full OpenKey modes mask, using the return value
+ * the module can check if a certain set of OpenKey modes are supported
+ * by the redis server version in use.
+ * Example:
+ *
+ * int supportedMode = RM_GetOpenKeyModesAll();
+ * if (supportedMode & REDISMODULE_OPEN_KEY_NOTOUCH) {
+ * // REDISMODULE_OPEN_KEY_NOTOUCH is supported
+ * } else{
+ * // REDISMODULE_OPEN_KEY_NOTOUCH is not supported
+ * }
+ */
+int RM_GetOpenKeyModesAll() {
+ return _REDISMODULE_OPEN_KEY_ALL;
+}
+
/* Destroy a RedisModuleKey struct (freeing is the responsibility of the caller). */
static void moduleCloseKey(RedisModuleKey *key) {
int signal = SHOULD_SIGNAL_MODIFIED_KEYS(key->ctx);
@@ -12809,6 +12838,7 @@ void moduleRegisterCoreAPI(void) {
REGISTER_API(SelectDb);
REGISTER_API(KeyExists);
REGISTER_API(OpenKey);
+ REGISTER_API(GetOpenKeyModesAll);
REGISTER_API(CloseKey);
REGISTER_API(KeyType);
REGISTER_API(ValueLength);