diff --git a/README.md b/README.md index 43fc135..6a1b89e 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,7 @@ Currently implemented are the following redis commands: * mset * msetnx * setex +* psetex * setnx * ping diff --git a/lib/client/redis-client.js b/lib/client/redis-client.js index a6952f0..edcce39 100644 --- a/lib/client/redis-client.js +++ b/lib/client/redis-client.js @@ -369,6 +369,14 @@ RedisClient.prototype.setex = RedisClient.prototype.SETEX = function (key, secon }); }; +RedisClient.prototype.psetex = RedisClient.prototype.PSETEX = function (key, ms, value, callback) { + this._selectedDb.set(key, value, () => { + this._selectedDb.pexpire(key, ms, (err, result) => { + helpers.callCallback(callback, err, "OK"); + }); + }); +}; + RedisClient.prototype.setnx = RedisClient.prototype.SETNX = function (key, value, callback) { this._selectedDb.setnx(key, value, callback); }; diff --git a/test/client/redis-mock.strings.test.js b/test/client/redis-mock.strings.test.js index 0449219..9688da7 100644 --- a/test/client/redis-mock.strings.test.js +++ b/test/client/redis-mock.strings.test.js @@ -383,6 +383,54 @@ describe("setex", function () { }); +describe("psetex", function () { + + this.timeout(5000); // eslint-disable-line no-invalid-this + + it("should set a key", function (done) { + var key = 'test_persist'; + r.psetex(key, 1000, "val", function (err, result) { + result.should.equal("OK"); + r.get(key, function (err, result) { + result.should.equal("val"); + done(); + }); + }); + }); + + it("should set a disappearing key", function (done) { + var key = 'test_disappearing'; + r.psetex(key, 1, "val", cb); + + function cb(err, result) { + result.should.be.ok(); + + setTimeout(function () { + r.exists(key, function (err, result) { + result.should.equal(0); + done(); + }); + }, 100); + } + }); + + it("should set a key without a callback involved", function (done) { + var key = 'test_persist_wo_callback'; + + r.psetex(key, 1000, "val"); + + setTimeout(function () { + r.get(key, function (err, result) { + result.should.equal("val"); + r.del(key); + done(); + }); + }, 100); + + }); + +}); + describe("setnx", function () { it("should set a key", function (done) {