Skip to content

Commit

Permalink
Able to specify expiry when using cache.set
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed May 12, 2011
1 parent c6dfba5 commit e572332
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions lib/Cache.js
Expand Up @@ -50,8 +50,11 @@ Cache.prototype.getKey = function (key) {
* @api public
*/

Cache.prototype.set = function (key, value, callback) {
if (typeof value === 'function') {
Cache.prototype.set = function (key, value, expiry, callback) {
if (typeof expiry === 'function') {
callback = expiry;
this.client.set(this.getKey(key), value, callback);
} else if (typeof value === 'function') {
callback = value;
var i, set = [];
for (i in key) {
Expand All @@ -61,7 +64,7 @@ Cache.prototype.set = function (key, value, callback) {
set.push(callback);
this.client.mset.apply(this.client, set);
} else {
this.client.set(this.getKey(key), value, callback);
this.client.setex(this.getKey(key), expiry, value, callback);
}
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion test/cache.test.js
Expand Up @@ -83,7 +83,7 @@ module.exports = {
'test cache increment': function () {
var cache = redback.createCache('test_cache_increment');

cache.set('foo', 1, function (err) {
cache.set('foo', 1, 1, function (err) {
cache.increment('foo', function (err) {
cache.get('foo', function (err, value) {
assert.equal(2, value);
Expand Down

0 comments on commit e572332

Please sign in to comment.