setex
1. 基本語法
(1) setex(key , seconds, value)
key , seconds, value) 為key值設定效期(expired time),超過設定的秒數(seconds)後,key值會自動變空值。
2. 範例
(1) 為key值設定5秒效期,時間超過效期資料自動變空值。
client.setex('hello', 5, 'world');
client.ttl('hello', redis.print);
client.get('hello', redis.print); Reply: 5
Reply: world(2) 注意:ttl剩0秒的當下,仍然可能取得value(尚未變空值)
client.setex('hello', 5, 'world');
function delay(ms: number) {
return new Promise( resolve => setTimeout(resolve, ms) );
}
(async () => {
client.get('hello', redis.print);
client.ttl('hello', redis.print);
await delay(5000);
client.get('hello', redis.print);
client.ttl('hello', redis.print);
await delay(1000);
client.get('hello', redis.print);
client.ttl('hello', redis.print);
})();
tag: expire
Last updated
Was this helpful?