Add RM_GetAbsExpire/RM_SetAbsExpire for module. (#8564)
Add a check to ensure that the expire parameters in RM_SetExpire and RM_SetAbsExpire must be positive.
This commit is contained in:
parent
b45b0d81bb
commit
ccc39f64f8
32
src/module.c
32
src/module.c
@ -2334,7 +2334,7 @@ mstime_t RM_GetExpire(RedisModuleKey *key) {
|
||||
* The function returns REDISMODULE_OK on success or REDISMODULE_ERR if
|
||||
* the key was not open for writing or is an empty key. */
|
||||
int RM_SetExpire(RedisModuleKey *key, mstime_t expire) {
|
||||
if (!(key->mode & REDISMODULE_WRITE) || key->value == NULL)
|
||||
if (!(key->mode & REDISMODULE_WRITE) || key->value == NULL || (expire < 0 && expire != REDISMODULE_NO_EXPIRE))
|
||||
return REDISMODULE_ERR;
|
||||
if (expire != REDISMODULE_NO_EXPIRE) {
|
||||
expire += mstime();
|
||||
@ -2345,6 +2345,36 @@ int RM_SetExpire(RedisModuleKey *key, mstime_t expire) {
|
||||
return REDISMODULE_OK;
|
||||
}
|
||||
|
||||
/* Return the key expire value, as absolute Unix timestamp.
|
||||
* If no TTL is associated with the key or if the key is empty,
|
||||
* REDISMODULE_NO_EXPIRE is returned. */
|
||||
mstime_t RM_GetAbsExpire(RedisModuleKey *key) {
|
||||
mstime_t expire = getExpire(key->db,key->key);
|
||||
if (expire == -1 || key->value == NULL)
|
||||
return REDISMODULE_NO_EXPIRE;
|
||||
return expire;
|
||||
}
|
||||
|
||||
/* Set a new expire for the key. If the special expire
|
||||
* REDISMODULE_NO_EXPIRE is set, the expire is cancelled if there was
|
||||
* one (the same as the PERSIST command).
|
||||
*
|
||||
* Note that the expire must be provided as a positive integer representing
|
||||
* the absolute Unix timestamp the key should have.
|
||||
*
|
||||
* The function returns REDISMODULE_OK on success or REDISMODULE_ERR if
|
||||
* the key was not open for writing or is an empty key. */
|
||||
int RM_SetAbsExpire(RedisModuleKey *key, mstime_t expire) {
|
||||
if (!(key->mode & REDISMODULE_WRITE) || key->value == NULL || (expire < 0 && expire != REDISMODULE_NO_EXPIRE))
|
||||
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;
|
||||
}
|
||||
|
||||
/* Performs similar operation to FLUSHALL, and optionally start a new AOF file (if enabled)
|
||||
* If restart_aof is true, you must make sure the command that triggered this call is not
|
||||
* propagated to the AOF file.
|
||||
|
@ -635,6 +635,8 @@ REDISMODULE_API char * (*RedisModule_StringDMA)(RedisModuleKey *key, size_t *len
|
||||
REDISMODULE_API int (*RedisModule_StringTruncate)(RedisModuleKey *key, size_t newlen) REDISMODULE_ATTR;
|
||||
REDISMODULE_API mstime_t (*RedisModule_GetExpire)(RedisModuleKey *key) REDISMODULE_ATTR;
|
||||
REDISMODULE_API int (*RedisModule_SetExpire)(RedisModuleKey *key, mstime_t expire) REDISMODULE_ATTR;
|
||||
REDISMODULE_API mstime_t (*RedisModule_GetAbsExpire)(RedisModuleKey *key) REDISMODULE_ATTR;
|
||||
REDISMODULE_API int (*RedisModule_SetAbsExpire)(RedisModuleKey *key, mstime_t expire) REDISMODULE_ATTR;
|
||||
REDISMODULE_API void (*RedisModule_ResetDataset)(int restart_aof, int async) REDISMODULE_ATTR;
|
||||
REDISMODULE_API unsigned long long (*RedisModule_DbSize)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
|
||||
REDISMODULE_API RedisModuleString * (*RedisModule_RandomKey)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
|
||||
@ -907,6 +909,8 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int
|
||||
REDISMODULE_GET_API(StringTruncate);
|
||||
REDISMODULE_GET_API(GetExpire);
|
||||
REDISMODULE_GET_API(SetExpire);
|
||||
REDISMODULE_GET_API(GetAbsExpire);
|
||||
REDISMODULE_GET_API(SetAbsExpire);
|
||||
REDISMODULE_GET_API(ResetDataset);
|
||||
REDISMODULE_GET_API(DbSize);
|
||||
REDISMODULE_GET_API(RandomKey);
|
||||
|
Loading…
x
Reference in New Issue
Block a user