Class: Readthis::Scripts

Inherits:
Object
  • Object
show all
Defined in:
lib/readthis/scripts.rb

Overview

The ‘Scripts` class is used to conveniently execute lua scripts. The first time a command is run it is stored on the server and subsequently referred to by its SHA. Each instance tracks SHAs separately, they are not global.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScripts

Creates a new Readthis::Scripts instance.



11
12
13
# File 'lib/readthis/scripts.rb', line 11

def initialize
  @loaded = {}
end

Instance Attribute Details

#loadedObject (readonly)

Returns the value of attribute loaded.



8
9
10
# File 'lib/readthis/scripts.rb', line 8

def loaded
  @loaded
end

Instance Method Details

#run(command, store, keys, args = []) ⇒ Any

Run a named lua script with the provided keys and arguments.

Examples:


scripts.run('mexpire', store, %w[a b c], 1) # => 'OK'

Parameters:

  • command (String)

    The script to run, without a ‘.lua` extension

  • store (#Store)

    A Redis client for storing and evaluating the script

  • keys (Array)

    One or more keys to pass to the command

  • args (Array) (defaults to: [])

    One or more args to pass to the command

Returns:

  • (Any)

    The Redis converted value returned on the script



28
29
30
31
32
33
34
# File 'lib/readthis/scripts.rb', line 28

def run(command, store, keys, args = [])
  store.evalsha(
    sha(command, store),
    Array(keys),
    Array(args)
  )
end