Skip to content

Instantly share code, notes, and snippets.

@gjohnson
Created September 24, 2013 22:29
Show Gist options
  • Select an option

  • Save gjohnson/6692226 to your computer and use it in GitHub Desktop.

Select an option

Save gjohnson/6692226 to your computer and use it in GitHub Desktop.
load script helper
module.exports = function (redis, code) {
var cache = null;
return function (keys, args, done) {
if (!cache) load(code, curry(evalsha, keys, args, done))
else evalsha(cache, keys, args, done);
};
function curry(fn) {
var args = [].slice.call(arguments, 1);
return function () {
return fn.apply(null, args);
};
}
function load(code, next) {
redis.send_command('script', ['LOAD', code], function (err, sha) {
if (err) return done(err);
cache = sha;
next();
});
}
function evalsha(sha, keys, args, next) {
redis.evalsha([sha, keys.length, keys, args], next);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment