Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs IOREDIS: how to set expire time for a key?

I am connecting to a Redis sentinel using the code given below

var Redis = require('ioredis');
var redis = new Redis({
    sentinels: [{ host: '99.9.999.99', port: 88888 }],
    name: 'mymaster'
});

I am setting the value of a key by using following code:

function (key, data) {

        var dataInStringFormat = JSON.stringify(data); /// conbverting obj to string

        /// making promise for returning
        var promise = new Promise(function (resolve, reject) {
            /// set data in redis
            redis.set(key, dataInStringFormat)
                .then(function (data) {
                    resolve(data);
                }, function (err) {
                    reject(err);
                });
        });

        return promise;

    }

Can you please help me by providing a solution to set an expire time for the key value e.g. 12 hours

like image 977
Vikas Bansal Avatar asked Dec 20 '16 07:12

Vikas Bansal


1 Answers

It's documented

redis.set('key', 100, 'ex', 10)

Where EX and 10 stands for 10 seconds. If you want to use milliseconds, replace EX with PX

like image 69
Tuan Anh Tran Avatar answered Oct 17 '22 11:10

Tuan Anh Tran