有没有可能在不移除Redis键的现有ttl的情况下进行SET
操作?目前我所知道的唯一方法是查找ttl并执行SETEX
,但这似乎不够准确。
有没有可能在不移除Redis键的现有ttl的情况下进行SET
操作?目前我所知道的唯一方法是查找ttl并执行SETEX
,但这似乎不够准确。
在redis>=6.0中,SET选项将添加KEEPTTL选项。
https://github.com/antirez/redis/pull/6679
The SET command supports a set of options that modify its behavior:
EX seconds -- Set the specified expire time, in seconds.
PX milliseconds -- Set the specified expire time, in milliseconds.
NX -- Only set the key if it does not already exist.
XX -- Only set the key if it already exist.
(!) KEEPTTL -- Retain the time to live associated with the key.
SET
命令会移除 TTL 时间,因为键被覆盖了。EVAL
命令来评估 Lua 脚本,以便自动完成此操作。SETEX
。
示例:local ttl = redis.call('ttl', ARGV[1]) if ttl > 0 then return redis.call('SETEX', ARGV[1], ttl, ARGV[2]) end
... 几秒钟后> set key 123
OK
> expire key 120
(integer) 1
> ttl key
(integer) 97
> eval "local ttl = redis.call('ttl', ARGV[1]) if ttl > 0 then return redis.call('SETEX', ARGV[1], ttl, ARGV[2]) end" 0 key 987
OK
> ttl key
96
> get key
"987"
也许 INCR
, INCRBY
, DECR
等命令可以帮助您,在不修改 TTL 的情况下实现操作。
> setex test 3600 13
OK
> incr test
(integer) 14
> ttl test
(integer) 3554
从Redis中获取剩余的TTL有一种方法。
在Redis中维护TTL:
示例:
// This function helps to get the Remaining TTL from the redis.
const getRemainingTTL=(key)=> {
return new Promise((resolve,reject)=>{
redisClient.TTL(key,(err,value)=>{
if(err)reject(err);
resolve(value);
});
});
};
let remainingTTL=await getRemainingTTL(otpSetKey);
console.log('remainingTTL',remainingTTL);
redisClient.set(key,newValue,'EX',exTime ); // set agin
/**
* update an item. preserve ttl or set a new one
* @param {object} handle the redis handle
* @param {string} key the key
* @param {*} content the content - if an object it'll get stringified
* @param {number||null} expire if a number > 0 its an expire time, 0
means keep existing ttl, undefined means no expiry
* @return {Promise}
*/
ns.updateItem = function (handle , key , content,expire) {
// first we have to get the expiry time if needed
return (expire === 0 ? handle.ttl(key) : Promise.resolve (expire))
.then (function (e) {
// deal with errors retrieving the ttl (-1 no expiry, -2 no existing record)
var ttl = e > 0 ? e : undefined;
// stingify the data if needed
var data = typeof content === "object" ? JSON.stringify(content) : content;
// set and apply ttl if needed
return ttl ? handle.set (key, data , "EX", ttl) : handle.set (key,data);
});
};
出于某种原因,这种函数对我来说无法正常工作:
redisSender.set('tenMinWaitTime', avg, 'KEEPTTL')
但我需要写成这样:
redisSender.set('tenMinWaitTime', avg, { KEEPTTL: true })
文档可以在这里找到: https://www.npmjs.com/package//redis