class Brpoplpush::RedisScript::Scripts

Interface to dealing with .lua files

@author Mikael Henriksson <mikael@mhenrixon.com>

Constants

SCRIPT_PATHS

@return [Concurrent::Map] a map with configured script paths

Attributes

root_path[R]

@!attribute [r] root_path

@return [Pathname] the path to the directory with lua scripts
scripts[R]

@!attribute [r] scripts

@return [Concurrent::Map] a collection of loaded scripts

Public Class Methods

create(root_path) click to toggle source

Create a new scripts collection based on path

@param [Pathname] root_path the path to scripts

@return [Scripts] a collection of scripts

# File lib/brpoplpush/redis_script/scripts.rb, line 35
def self.create(root_path)
  scripts = new(root_path)
  store(scripts)
end
fetch(root_path) click to toggle source

Fetch a scripts configuration for path

@param [Pathname] root_path the path to scripts

@return [Scripts] a collection of scripts

# File lib/brpoplpush/redis_script/scripts.rb, line 20
def self.fetch(root_path)
  if (scripts = SCRIPT_PATHS.get(root_path))
    return scripts
  end

  create(root_path)
end
new(path) click to toggle source
# File lib/brpoplpush/redis_script/scripts.rb, line 62
def initialize(path)
  raise ArgumentError, "path needs to be a Pathname" unless path.is_a?(Pathname)

  @scripts   = Concurrent::Map.new
  @root_path = path
end
store(scripts) click to toggle source

Store the scripts collection in memory

@param [Scripts] scripts the path to scripts

@return [Scripts] the scripts instance that was stored

# File lib/brpoplpush/redis_script/scripts.rb, line 47
def self.store(scripts)
  SCRIPT_PATHS.put(scripts.root_path, scripts)
  scripts
end

Public Instance Methods

count() click to toggle source
# File lib/brpoplpush/redis_script/scripts.rb, line 118
def count
  scripts.keys.size
end
delete(script) click to toggle source
# File lib/brpoplpush/redis_script/scripts.rb, line 84
def delete(script)
  if script.is_a?(Script)
    scripts.delete(script.name)
  else
    scripts.delete(script.to_sym)
  end
end
execute(name, conn, keys: [], argv: []) click to toggle source

Execute a lua script with given name

@note this method is recursive if we need to load a lua script

that wasn't previously loaded.

@param [Symbol] name the name of the script to execute @param [Redis] conn the redis connection to use for execution @param [Array<String>] keys script keys @param [Array<Object>] argv script arguments

@return value from script

# File lib/brpoplpush/redis_script/scripts.rb, line 113
def execute(name, conn, keys: [], argv: [])
  script = fetch(name, conn)
  conn.evalsha(script.sha, keys: keys, argv: argv)
end
fetch(name, conn) click to toggle source
# File lib/brpoplpush/redis_script/scripts.rb, line 69
def fetch(name, conn)
  if (script = scripts.get(name.to_sym))
    return script
  end

  load(name, conn)
end
kill(conn) click to toggle source
# File lib/brpoplpush/redis_script/scripts.rb, line 92
def kill(conn)
  if conn.respond_to?(:namespace)
    conn.redis.script(:kill)
  else
    conn.script(:kill)
  end
end
load(name, conn) click to toggle source
# File lib/brpoplpush/redis_script/scripts.rb, line 77
def load(name, conn)
  script = Script.load(name, root_path, conn)
  scripts.put(name.to_sym, script)

  script
end