-
Notifications
You must be signed in to change notification settings - Fork 0
/
app4.js
35 lines (30 loc) · 844 Bytes
/
app4.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var redis = require('redis')
,client = redis.createClient();
client.on('error', function (err) {
console.error('error: ' + err);
});
client.on('connect', runSample);
function runSample() {
// Set a value with an expiration
client.set('string key', 'Hello World', redis.print);
// Expire in 3 seconds
client.expire('string key', 3);
// This time is only to demo the TTL
// Runs every second until the timeout occurs on the value
var myTimer = setInterval( function () {
client.get('string key', function (err, reply) {
if(reply) {
console.log('I live: ' + reply.toString());
client.ttl('string key', writeTTL);
}
else {
clearTimeout(myTimer);
console.log('I expired');
client.quit();
}
});
}, 1000);
};
function writeTTL(err, data) {
console.log('I\'ve lived for this long yet: ' + data);
}