From c8103928b43d37b25c46af939759098968d016f0 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Tue, 14 Aug 2012 20:17:41 -0700 Subject: [PATCH] Attempt evalsha before eval Fix #253 Signed-off-by: DTrejo --- index.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/index.js b/index.js index 77bda9779d..888ee055e3 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ var net = require("net"), Queue = require("./lib/queue"), to_array = require("./lib/to_array"), events = require("events"), + crypto = require("crypto"), parsers = [], commands, connection_id = 0, default_port = 6379, @@ -1059,6 +1060,36 @@ RedisClient.prototype.MULTI = function (args) { return new Multi(this, args); }; + +// stash original eval method +var eval = RedisClient.prototype.eval; +// hook eval with an attempt to evalsha for cached scripts +RedisClient.prototype.eval = +RedisClient.prototype.EVAL = function () { + var self = this, + args = to_array(arguments), + callback; + + if (typeof args[args.length - 1] === "function") { + callback = args.pop(); + } + + // replace script source with sha value + var source = args[0]; + args[0] = crypto.createHash("sha1").update(source).digest("hex"); + + self.evalsha(args, function (err, reply) { + if (err && /NOSCRIPT/.test(err.message)) { + args[0] = source; + eval.call(self, args, callback); + + } else if (callback) { + callback(err, reply); + } + }); +}; + + exports.createClient = function (port_arg, host_arg, options) { var port = port_arg || default_port, host = host_arg || default_host,