Skip to content

Instantly share code, notes, and snippets.

@hmpmarketing
Created January 5, 2018 19:19
Show Gist options
  • Save hmpmarketing/92f4552a1b1881cea7bd47806ebd021c to your computer and use it in GitHub Desktop.
Save hmpmarketing/92f4552a1b1881cea7bd47806ebd021c to your computer and use it in GitHub Desktop.
Cache.remember = function (key, lifetime, callback) {
return Promise.try(() => {
return memcached.getAsync(key);
}).then((value) => {
if (value !== undefined) {
console.log('exists');
return value;
} else {
return Promise.try(() => {
if (typeof callback === "function") {
return callback()
}else{
throw new Error('callback undefined or not a function')
}
}).then((value) => {
console.log('does not exists');
return memcached.setAsync(key, value, lifetime);
}).then(() => {
return value;
})
}
})
};
//Calling
cache.remember('key', 600, function() {
return Promise.resolve('value2');
}).then((val) => {
console.log('set: ', val);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment