Class: Brpoplpush::RedisScript::Scripts

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

Overview

Interface to dealing with .lua files

Author:

Constant Summary collapse

SCRIPT_PATHS =

Returns a map with configured script paths.

Returns:

  • (Concurrent::Map)

    a map with configured script paths

Concurrent::Map.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Scripts

Returns a new instance of Scripts.

Raises:

  • (ArgumentError)


62
63
64
65
66
67
# 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

Instance Attribute Details

#root_pathObject (readonly)

Returns the value of attribute root_path.



60
61
62
# File 'lib/brpoplpush/redis_script/scripts.rb', line 60

def root_path
  @root_path
end

#scriptsObject (readonly)

Returns the value of attribute scripts.



55
56
57
# File 'lib/brpoplpush/redis_script/scripts.rb', line 55

def scripts
  @scripts
end

Class Method Details

.create(root_path) ⇒ Scripts

Create a new scripts collection based on path

Parameters:

  • root_path (Pathname)

    the path to scripts

Returns:

  • (Scripts)

    a collection of scripts



35
36
37
38
# File 'lib/brpoplpush/redis_script/scripts.rb', line 35

def self.create(root_path)
  scripts = new(root_path)
  store(scripts)
end

.fetch(root_path) ⇒ Scripts

Fetch a scripts configuration for path

Parameters:

  • root_path (Pathname)

    the path to scripts

Returns:

  • (Scripts)

    a collection of scripts



20
21
22
23
24
25
26
# 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

.store(scripts) ⇒ Scripts

Store the scripts collection in memory

Parameters:

  • scripts (Scripts)

    the path to scripts

Returns:

  • (Scripts)

    the scripts instance that was stored



47
48
49
50
# File 'lib/brpoplpush/redis_script/scripts.rb', line 47

def self.store(scripts)
  SCRIPT_PATHS.put(scripts.root_path, scripts)
  scripts
end

Instance Method Details

#countObject



118
119
120
# File 'lib/brpoplpush/redis_script/scripts.rb', line 118

def count
  scripts.keys.size
end

#delete(script) ⇒ Object



84
85
86
87
88
89
90
# 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: []) ⇒ Object

Note:

this method is recursive if we need to load a lua script that wasn’t previously loaded.

Execute a lua script with given name

Parameters:

  • name (Symbol)

    the name of the script to execute

  • conn (Redis)

    the redis connection to use for execution

  • keys (Array<String>) (defaults to: [])

    script keys

  • argv (Array<Object>) (defaults to: [])

    script arguments

Returns:

  • value from script



113
114
115
116
# File 'lib/brpoplpush/redis_script/scripts.rb', line 113

def execute(name, conn, keys: [], argv: [])
  script = fetch(name, conn)
  conn.evalsha(script.sha, keys, argv)
end

#fetch(name, conn) ⇒ Object



69
70
71
72
73
74
75
# 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) ⇒ Object



92
93
94
95
96
97
98
# 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) ⇒ Object



77
78
79
80
81
82
# 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