-
-
Save jkantr/b386d8a3677e5cbaac2d96657904cb77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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