Skip to content

Instantly share code, notes, and snippets.

@jkantr
Forked from hmpmarketing/remember.js
Last active January 5, 2018 19:42
Show Gist options
  • Save jkantr/b386d8a3677e5cbaac2d96657904cb77 to your computer and use it in GitHub Desktop.
Save jkantr/b386d8a3677e5cbaac2d96657904cb77 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 memcached.getAsync(key);
})
}
})
};
//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