From fc8808289ce57049fbf34a0de81cc8d1a472bdbb Mon Sep 17 00:00:00 2001 From: ToddA Date: Tue, 5 Sep 2017 13:15:31 -0700 Subject: [PATCH 1/3] Update index.js Add methods --- index.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/index.js b/index.js index 37b824e..207c3ba 100644 --- a/index.js +++ b/index.js @@ -66,6 +66,19 @@ function setPromise(instance, key, value, expires) { }); } +function genericPromise(instance, method, key, value, expires) { + return new Promise((resolve, reject) => { + instance._cache[method](key, value, expires, (err, data) => { + if (err) { + reject(err); + } + else { + resolve(data); + } + }); + }); +} + /** * get a cache item * @param {string} key - cache key @@ -125,4 +138,22 @@ Cache.prototype.del = function(key) { return getPromise(this, 'del', this.config.keyPrefix + key); }; +/** + * add an item in the cache + * @param {string} key - cache key + * @returns {Promise} + */ +Cache.prototype.add = function(key, data, expires){ + return genericPromise(this, 'add', this.config.keyPrefix + key, data, expires); +}; + +/** + * replace an item in the cache + * @param {string} key - cache key + * @returns {Promise} + */ +Cache.prototype.replace = function(key, data, expires){ + return genericPromise(this, 'replace', this.config.keyPrefix + key, data, expires); +}; + module.exports = Cache; From 426de0ee40de67b3c6caecaebf3570184e3a19ac Mon Sep 17 00:00:00 2001 From: ToddA Date: Tue, 5 Sep 2017 13:29:05 -0700 Subject: [PATCH 2/3] Update index.js --- index.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/index.js b/index.js index 207c3ba..d15de8d 100644 --- a/index.js +++ b/index.js @@ -156,4 +156,13 @@ Cache.prototype.replace = function(key, data, expires){ return genericPromise(this, 'replace', this.config.keyPrefix + key, data, expires); }; +/** + * flush the cache + * @param {string} key - cache key + * @returns {Promise} + */ +Cache.prototype.flush = function(key, data, expires){ + return getPromise(this, 'flush'); +}; + module.exports = Cache; From 1cf0b491e1f0c543d8a44a4960ba715db937785a Mon Sep 17 00:00:00 2001 From: ToddA Date: Thu, 7 Sep 2017 17:52:48 -0700 Subject: [PATCH 3/3] Update index.js --- index.js | 69 +++++++++++++++++++++++--------------------------------- 1 file changed, 28 insertions(+), 41 deletions(-) diff --git a/index.js b/index.js index d15de8d..b5be946 100644 --- a/index.js +++ b/index.js @@ -33,42 +33,10 @@ function Cache(config, options) { this._cache = new Memcached(this.config.cacheHost, options); } -function getPromise(instance, method, key) { - return new Promise((resolve, reject) => { - const cb = (err, data) => { - if (err) { - reject(err); - } - else { - resolve(data); - } - }; - - if (key) { - instance._cache[method](key, cb); - } - else { - instance._cache[method](cb); - } - }); -} -function setPromise(instance, key, value, expires) { - return new Promise((resolve, reject) => { - instance._cache.set(key, value, expires, (err, data) => { - if (err) { - reject(err); - } - else { - resolve(data); - } - }); - }); -} - -function genericPromise(instance, method, key, value, expires) { +function genericPromise(instance, method, ...args) { return new Promise((resolve, reject) => { - instance._cache[method](key, value, expires, (err, data) => { + instance._cache[method]( ...args, (err, data) => { if (err) { reject(err); } @@ -79,13 +47,14 @@ function genericPromise(instance, method, key, value, expires) { }); } + /** * get a cache item * @param {string} key - cache key * @returns {Promise} */ Cache.prototype.get = function(key) { - return getPromise(this, 'get', `${this.config.keyPrefix}${key}`); + return genericPromise(this, 'get', `${this.config.keyPrefix}${key}`); }; /** @@ -94,7 +63,7 @@ Cache.prototype.get = function(key) { * @returns {Promise} */ Cache.prototype.utilGet = function(key) { - return getPromise(this, 'get', key); + return genericPromise(this, 'get', key); }; /** @@ -103,7 +72,7 @@ Cache.prototype.utilGet = function(key) { * @returns {Promise} */ Cache.prototype.getMulti = function(keys) { - return getPromise(this, 'getMulti', keys); + return genericPromise(this, 'getMulti', keys); }; /** @@ -111,7 +80,7 @@ Cache.prototype.getMulti = function(keys) { * @returns {Promise} */ Cache.prototype.stats = function() { - return getPromise(this, 'stats'); + return genericPromise(this, 'stats'); }; /** @@ -126,7 +95,7 @@ Cache.prototype.set = function(key, data, expires){ throw new Error('Cache.set: Expiration must be no greater than ' + this.config.maxExpiration + ' seconds.'); } - return setPromise(this, this.config.keyPrefix + key, data, expires); + return genericPromise(this, 'set', this.config.keyPrefix + key, data, expires); }; /** @@ -135,7 +104,7 @@ Cache.prototype.set = function(key, data, expires){ * @returns {Promise} */ Cache.prototype.del = function(key) { - return getPromise(this, 'del', this.config.keyPrefix + key); + return genericPromise(this, 'del', this.config.keyPrefix + key); }; /** @@ -156,13 +125,31 @@ Cache.prototype.replace = function(key, data, expires){ return genericPromise(this, 'replace', this.config.keyPrefix + key, data, expires); }; +/** + * replace an item in the cache + * @param {string} key - cache key + * @returns {Promise} + */ +Cache.prototype.incr = function(key, value, expires){ + return genericPromise(this, 'incr', this.config.keyPrefix + key, value ); +}; + +/** + * replace an item in the cache + * @param {string} key - cache key + * @returns {Promise} + */ +Cache.prototype.decr = function(key, value, expires){ + return genericPromise(this, 'decr', this.config.keyPrefix + key, value ); +}; + /** * flush the cache * @param {string} key - cache key * @returns {Promise} */ Cache.prototype.flush = function(key, data, expires){ - return getPromise(this, 'flush'); + return genericPromise(this, 'flush'); }; module.exports = Cache;