Created
September 24, 2013 22:29
-
-
Save gjohnson/6692226 to your computer and use it in GitHub Desktop.
load script helper
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 = 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