Class: Stoplight::Infrastructure::Redis::DataStore::Scripting

Inherits:
Object
  • Object
show all
Defined in:
lib/stoplight/infrastructure/redis/data_store/scripting.rb

Overview

Note:

Scripts are loaded lazily on first use and cached in memory

Note:

Script files must be named ‘<script_name>.lua` and located in scripts_root

Manages Lua scripts for Redis operations.

This class provides execution of Lua scripts by caching their SHA digests and automatically reloading scripts if they’re evicted from Redis memory.

Examples:

script_manager = ScriptManager.new(redis: redis_client)
script_manager.call(:increment_counter, keys: ["counter:1"], args: [5])

Direct Known Subclasses

Storage::Scripting

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis:, scripts_root: self.class.default_scripts_root) ⇒ Scripting

Returns a new instance of Scripting.



27
28
29
30
31
# File 'lib/stoplight/infrastructure/redis/data_store/scripting.rb', line 27

def initialize(redis:, scripts_root: self.class.default_scripts_root)
  @scripts_root = scripts_root
  @redis = redis
  @shas = {}
end

Class Method Details

.default_scripts_rootObject



24
# File 'lib/stoplight/infrastructure/redis/data_store/scripting.rb', line 24

def default_scripts_root = SCRIPTS_ROOT

Instance Method Details

#call(script_name, keys: [], args: []) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/stoplight/infrastructure/redis/data_store/scripting.rb', line 33

def call(script_name, keys: [], args: [])
  redis.then do |client|
    client.evalsha(script_sha(script_name), keys: keys.map(&:to_s), argv: args.map(&:to_s))
  end
rescue ::Redis::CommandError => error
  if error.message.include?("NOSCRIPT")
    reload_script(script_name)
    retry
  else
    raise
  end
end