Created
November 21, 2013 20:28
-
-
Save lele85/7589030 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| module.exports = exports = { | |
| create : function(notCachedClient){ | |
| var cachedClient = {}; | |
| var cache = {}; | |
| Object.keys(notCachedClient).forEach(function(method){ | |
| cachedClient[method] = function(){ | |
| var args = Array.prototype.slice.call(arguments), | |
| cb = args.pop(); | |
| if (cache[method]){ | |
| process.nextTick(function(){cb(false, cache[method]);}); | |
| } else { | |
| var cacheResultThenCb = function(err, data){ | |
| if (!err){ cache[method] = data; } | |
| cb(err,data); | |
| }; | |
| var newArgs = args.concat(cacheResultThenCb); | |
| notCachedClient[method].apply(notCachedClient, newArgs); | |
| } | |
| }; | |
| }); | |
| return cachedClient; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment