Currently the modules API exposes a RedisModule_SetExpire function, but this strictly takes a delta for the TTL.
Internally, this is implemented as: https://github.com/antirez/redis/blob/unstable/src/module.c#L1457-L1476 which ends up doing expire += mstime().
It'd be nice to have the same API as EXPIREAT and expose RedisModule_SetExpireAt which takes an absolute mstime_t
Something just as simple as:
int RM_SetExpireAt(RedisModuleKey *key, mstime_t expire) {
if (!(key->mode & REDISMODULE_WRITE) || key->value == NULL)
return REDISMODULE_ERR;
if (expire != REDISMODULE_NO_EXPIRE) {
setExpire(key->ctx->client,key->db,key->key,expire);
} else {
removeExpire(key->db,key->key);
}
return REDISMODULE_OK;
}
The exact same thing, except without adding mstime() to the value.
The reason for this is the only way around it, if you currently have a unix timestamp, is to reimplement mstime() yourself, since this isn't exposed through module API, then subtract this value from your absolute value... just so that RedisModule_SetExpire will add it back.
I'd be happy to take the time and flesh this out into a PR if this would be accepted.
Comment From: mattrobenolt
Turns out, there's RedisModule_Milliseconds() which allows you to get current time in milliseconds.
So this can be implemented just fine by doing RedisModule_SetExpire(key, time - RedisModule_Milliseconds())
Would still seem intuitive to allow having a function to explicitly do SetExpireAt to avoid the +- dance.
Comment From: yossigo
Hey @mattrobenolt, responding late but still thanks for this input. I agree with you that there's no clear advantage for yet another module API function! Closing, please feel free to re-open if necessary.