Skip to content

Instantly share code, notes, and snippets.

@lele85
Created November 21, 2013 20:28
Show Gist options
  • Select an option

  • Save lele85/7589030 to your computer and use it in GitHub Desktop.

Select an option

Save lele85/7589030 to your computer and use it in GitHub Desktop.
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