19

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

Vikas Bansal
  • 10,662
  • 14
  • 58
  • 100

1 Answers1

47

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

Ben Walding
  • 4,006
  • 2
  • 30
  • 28
Tuan Anh Tran
  • 6,807
  • 6
  • 37
  • 54
  • 12
    According to the documentation, `EX` for seconds, `PX` for milliseconds https://redis.io/commands/set – jesugmz Jan 18 '20 at 15:13