expire
expire(key
, seconds
, callback
)
key
, seconds
, callback
) 為key
對應資料設定seconds
秒的效期(Expire Time),時間超過效期資料自動變空值。
Redis 7.0版本起 EXPIRE 起支援以下參數:
NX
-- Set expiry only when the key has no expiryXX
-- Set expiry only when the key has an existing expiryGT
-- Set expiry only when the new expiry is greater than current oneLT
-- Set expiry only when the new expiry is less than current one
A non-volatile key is treated as an infinite TTL for the purpose of GT
and LT
. The GT
, LT
and NX
options are mutually exclusive.
function delay(ms: number) {
return new Promise( resolve => setTimeout(resolve, ms) );
}
(async () => {
console.log('=== First set and get ===');
client.set("foo", "bar");
client.expire("foo", 5);
client.get('foo', redis.print);
client.ttl('foo', redis.print);
await delay(5000);
console.log('=== Second get ===');
client.get('foo', redis.print);
client.ttl('foo', redis.print);
await delay(1000);
console.log('=== Last get ===');
client.get('foo', redis.print);
client.ttl('foo', redis.print);
})();
=== First set and get ===
Reply: bar
Reply: 5
=== Second get ===
Reply: bar
Reply: 0
=== Last get ===
Reply: null
Reply: -2
Last updated
Was this helpful?