From 45c99d7092926c0774608520b9a399dbd40f9714 Mon Sep 17 00:00:00 2001 From: guybe7 Date: Wed, 27 Jul 2022 13:40:05 +0200 Subject: Adds RM_Microseconds and RM_CachedMicroseconds (#11016) RM_Microseconds Return the wall-clock Unix time, in microseconds RM_CachedMicroseconds Returns a cached copy of the Unix time, in microseconds. It is updated in the server cron job and before executing a command. It is useful for complex call stacks, such as a command causing a key space notification, causing a module to execute a RedisModule_Call, causing another notification, etc. It makes sense that all these callbacks would use the same clock. --- tests/modules/keyspace_events.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'tests/modules') diff --git a/tests/modules/keyspace_events.c b/tests/modules/keyspace_events.c index 58670164d..c0f79bf37 100644 --- a/tests/modules/keyspace_events.c +++ b/tests/modules/keyspace_events.c @@ -30,10 +30,14 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#define _DEFAULT_SOURCE /* For usleep */ #include "redismodule.h" #include #include +#include + +ustime_t cached_time = 0; /** stores all the keys on which we got 'loaded' keyspace notification **/ RedisModuleDict *loaded_event_log = NULL; @@ -59,6 +63,12 @@ static int KeySpace_NotificationLoaded(RedisModuleCtx *ctx, int type, const char static int KeySpace_NotificationGeneric(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key) { REDISMODULE_NOT_USED(type); + if (cached_time) { + RedisModule_Assert(cached_time == RedisModule_CachedMicroseconds()); + usleep(1); + RedisModule_Assert(cached_time != RedisModule_Microseconds()); + } + if (strcmp(event, "del") == 0) { RedisModuleString *copykey = RedisModule_CreateStringPrintf(ctx, "%s_copy", RedisModule_StringPtrLen(key, NULL)); RedisModuleCallReply* rep = RedisModule_Call(ctx, "DEL", "s!", copykey); @@ -158,6 +168,8 @@ static int cmdDelKeyCopy(RedisModuleCtx *ctx, RedisModuleString **argv, int argc if (argc != 2) return RedisModule_WrongArity(ctx); + cached_time = RedisModule_CachedMicroseconds(); + RedisModuleCallReply* rep = RedisModule_Call(ctx, "DEL", "s!", argv[1]); if (!rep) { RedisModule_ReplyWithError(ctx, "NULL reply returned"); @@ -165,6 +177,7 @@ static int cmdDelKeyCopy(RedisModuleCtx *ctx, RedisModuleString **argv, int argc RedisModule_ReplyWithCallReply(ctx, rep); RedisModule_FreeCallReply(rep); } + cached_time = 0; return REDISMODULE_OK; } -- cgit v1.2.1