您的假设是正确的,只是一个接一个地过期。
EXPIRE 不会累积或重置或任何东西,它只是将计时器设置为新值。
示例(无冗长错误处理):
'use strict';
let client = require('redis').createClient()
const KEY = 'my:key';
const TTL = 10;
let value = 'some-value';
client.on('ready', function() {
console.log('Setting key...')
client.set(KEY, value, function() {
console.log('Setting expire on the key...');
client.expire(KEY, TTL, function() {
console.log('Waiting 6 sec before checking expire time...');
// Check in 6 seconds, ttl should be around 6
setTimeout(function() {
client.ttl(KEY, function(err, expiryTime) {
console.log('expiryTime:', expiryTime); // "expiryTime: 6" on my system
// expire again to show it does not stack, it only resets the expire value
console.log('Expiring key again...');
client.expire(KEY, TTL, function() {
// again wait for 3 sec
console.log('Waiting 3 more sec before checking expire time...');
setTimeout(function() {
client.ttl(KEY, function(err, expiryTime) {
console.log('New expiryTime:', expiryTime); // 7
process.exit();
})
}, 3000);
});
});
}, 6000);
});
});
});
(对不起,回调金字塔)。
在我的系统上运行:
[zlatko@desktop-mint ~/tmp]$ node test.js
Setting key...
Setting expire on the key...
Waiting 6 sec before checking expire time...
expiryTime: 4
Expiring key again...
Waiting 3 more sec before checking expire time...
New expiryTime: 7
[zlatko@desktop-mint ~/tmp]$
如您所见,我们将过期时间设置为 10 秒。 6秒后,显然还剩4秒。
如果我们在那一刻,还有 4 秒的时间,将过期时间再次设置为 10,我们只需从 10 开始。
3 秒后,我们还能再坚持 7 秒。