Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update index.js #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 55 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +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) => {
function genericPromise(instance, method, ...args) {
return new Promise((resolve, reject) => {
instance._cache[method]( ...args, (err, data) => {
if (err) {
reject(err);
}
Expand All @@ -66,13 +47,14 @@ function setPromise(instance, 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}`);
};

/**
Expand All @@ -81,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);
};

/**
Expand All @@ -90,15 +72,15 @@ Cache.prototype.utilGet = function(key) {
* @returns {Promise}
*/
Cache.prototype.getMulti = function(keys) {
return getPromise(this, 'getMulti', keys);
return genericPromise(this, 'getMulti', keys);
};

/**
* gets stats from memcached server
* @returns {Promise}
*/
Cache.prototype.stats = function() {
return getPromise(this, 'stats');
return genericPromise(this, 'stats');
};

/**
Expand All @@ -113,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);
};

/**
Expand All @@ -122,7 +104,52 @@ 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);
};

/**
* 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);
};

/**
* 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 genericPromise(this, 'flush');
};

module.exports = Cache;