Class: Labkit::Redis::Script
- Inherits:
-
Object
- Object
- Labkit::Redis::Script
- Defined in:
- lib/labkit/redis/script.rb
Overview
Wraps a Lua script for EVALSHA-with-NOSCRIPT-fallback execution. The SHA is computed once at construction. Redis caches the script body the first time EVAL is invoked; subsequent EVALSHA calls hit that cache. SCRIPT FLUSH or a Redis restart drops the cache; the NOSCRIPT recovery re-ships the body and re-populates it.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#sha ⇒ Object
readonly
Returns the value of attribute sha.
Instance Method Summary collapse
-
#eval(conn, keys:, argv:) ⇒ Object
The script’s return value.
-
#initialize(body) ⇒ Script
constructor
A new instance of Script.
Constructor Details
#initialize(body) ⇒ Script
Returns a new instance of Script.
23 24 25 26 27 28 |
# File 'lib/labkit/redis/script.rb', line 23 def initialize(body) @body = body.freeze # SHA1 is mandated by the Redis EVALSHA wire protocol, not a discretionary hash choice. @sha = OpenSSL::Digest::SHA1.hexdigest(body).freeze # rubocop:disable Fips/SHA1 freeze end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
21 22 23 |
# File 'lib/labkit/redis/script.rb', line 21 def body @body end |
#sha ⇒ Object (readonly)
Returns the value of attribute sha.
21 22 23 |
# File 'lib/labkit/redis/script.rb', line 21 def sha @sha end |
Instance Method Details
#eval(conn, keys:, argv:) ⇒ Object
Returns the script’s return value.
34 35 36 37 38 39 40 |
# File 'lib/labkit/redis/script.rb', line 34 def eval(conn, keys:, argv:) conn.evalsha(@sha, keys: keys, argv: argv) rescue ::Redis::CommandError => e raise unless e..start_with?("NOSCRIPT") conn.eval(@body, keys: keys, argv: argv) end |