Class: Redis::Scripting::Script

Inherits:
Object
  • Object
show all
Defined in:
lib/redis/scripting/script.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_filename, opts = {}) ⇒ Script

Returns a new instance of Script.



9
10
11
12
13
14
15
16
# File 'lib/redis/scripting/script.rb', line 9

def initialize(source_filename, opts = {})
  @source_filename = source_filename
  @source = File.read(source_filename)
  if opts[:script_header]
    @source = "#{opts[:script_header]}\n\n#{@source}"
  end
  @name = File.basename(source_filename, ".lua")
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/redis/scripting/script.rb', line 7

def name
  @name
end

#sourceObject (readonly)

Returns the value of attribute source.



7
8
9
# File 'lib/redis/scripting/script.rb', line 7

def source
  @source
end

#source_filenameObject (readonly)

Returns the value of attribute source_filename.



7
8
9
# File 'lib/redis/scripting/script.rb', line 7

def source_filename
  @source_filename
end

Instance Method Details

#run(redis, keys, argv) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/redis/scripting/script.rb', line 18

def run(redis, keys, argv)
  begin
    redis.evalsha(self.sha, keys: keys, argv: argv)
  rescue Redis::CommandError => err
    raise unless err.message.start_with?("NOSCRIPT")
    redis.eval(self.source, keys: keys, argv: argv)
  end
end

#shaObject



27
28
29
# File 'lib/redis/scripting/script.rb', line 27

def sha
  @sha ||= OpenSSL::Digest::SHA1.hexdigest(source)
end