Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Only load lua scripts if they are needed.

Given how many workers we are and how rarely the scripts change,
most of the time the lua scripts do not need to be loaded when
a client is created.  It can be delayed until it is actually
needed (often never during the lifetime of the process).
  • Loading branch information...
commit 57d264f28ea27f9e72d04d4ed9d1abb3358ea593 1 parent b89dbbd
@myronmarston myronmarston authored
Showing with 48 additions and 4 deletions.
  1. +9 −4 lib/qless/lua_script.rb
  2. +39 −0 spec/integration/lua_script_spec.rb
View
13 lib/qless/lua_script.rb
@@ -1,18 +1,19 @@
+require 'digest/sha1'
+
module Qless
class LuaScript
LUA_SCRIPT_DIR = File.expand_path("../qless-core/", __FILE__)
- def initialize(name, redis, sha = nil)
- @sha = sha
+ def initialize(name, redis)
@name = name
@redis = redis
- reload() unless sha
+ @sha = Digest::SHA1.hexdigest(script_contents)
end
attr_reader :name, :redis, :sha
def reload()
- @sha = @redis.script(:load, File.read(File.join(LUA_SCRIPT_DIR, "#{@name}.lua")))
+ @sha = @redis.script(:load, script_contents)
end
def call(keys, argv)
@@ -33,5 +34,9 @@ def _call(keys, argv)
@redis.evalsha(@sha, keys: keys, argv: argv)
end
end
+
+ def script_contents
+ @script_contents ||= File.read(File.join(LUA_SCRIPT_DIR, "#{@name}.lua"))
+ end
end
end
View
39 spec/integration/lua_script_spec.rb
@@ -0,0 +1,39 @@
+require 'spec_helper'
+require "qless"
+require 'yaml'
+
+module Qless
+ describe LuaScript, :integration do
+ let(:redis) { client.redis }
+
+ it 'does not make any redis requests upon initialization' do
+ redis = double("Redis")
+
+ expect {
+ LuaScript.new("config", redis)
+ }.not_to raise_error # e.g. MockExpectationError
+ end
+
+ it 'can issue the command without loading the script if it is already loaded' do
+ script = LuaScript.new("config", redis)
+ redis.script(:load, script.send(:script_contents)) # to ensure its loaded
+ redis.should_not_receive(:script)
+
+ expect {
+ script.call([], ['set', 'key', 3])
+ }.to change { redis.keys.size }.by(1)
+ end
+
+ it 'loads the script as needed if the command fails' do
+ script = LuaScript.new("config", redis)
+ redis.script(:flush)
+
+ redis.should_receive(:script).and_call_original
+
+ expect {
+ script.call([], ['set', 'key', 3])
+ }.to change { redis.keys.size }.by(1)
+ end
+ end
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.