Class: Stoplight::Infrastructure::Redis::DataStore::Scripting
- Inherits:
-
Object
- Object
- Stoplight::Infrastructure::Redis::DataStore::Scripting
- Defined in:
- lib/stoplight/infrastructure/redis/data_store/scripting.rb,
sig/_private/stoplight/infrastructure/redis/data_store/scripting.rbs
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.
Direct Known Subclasses
Constant Summary collapse
- SCRIPTS_ROOT =
File.join(current_dir, "lua_scripts")
Instance Attribute Summary collapse
-
#redis ⇒ redis
readonly
Returns the value of attribute redis.
-
#scripts_root ⇒ String
readonly
Returns the value of attribute scripts_root.
-
#shas ⇒ Hash[interned, String]
readonly
Returns the value of attribute shas.
Class Method Summary collapse
Instance Method Summary collapse
- #call(script_name, keys: [], args: []) ⇒ Object
-
#initialize(redis:, scripts_root: self.class.default_scripts_root) ⇒ Scripting
constructor
A new instance of Scripting.
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 |
Instance Attribute Details
#redis ⇒ redis (readonly)
Returns the value of attribute redis.
49 50 51 |
# File 'lib/stoplight/infrastructure/redis/data_store/scripting.rb', line 49 def redis @redis end |
#scripts_root ⇒ String (readonly)
Returns the value of attribute scripts_root.
48 49 50 |
# File 'lib/stoplight/infrastructure/redis/data_store/scripting.rb', line 48 def scripts_root @scripts_root end |
#shas ⇒ Hash[interned, String] (readonly)
Returns the value of attribute shas.
53 54 55 |
# File 'lib/stoplight/infrastructure/redis/data_store/scripting.rb', line 53 def shas @shas end |
Class Method Details
.default_scripts_root ⇒ String
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..include?("NOSCRIPT") reload_script(script_name) retry else raise end end |