Created
January 5, 2018 19:19
-
-
Save hmpmarketing/92f4552a1b1881cea7bd47806ebd021c 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 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