Lua script helper with SHA1 caching.
Computes the SHA1 at creation time and uses EVALSHA by default, automatically falling back to EVAL on NOSCRIPT error.
Usage
script = Redis.Script.new("return redis.call('GET', KEYS[1])")
{:ok, result} = Redis.Script.eval(conn, script, keys: ["mykey"])
# With arguments
script = Redis.Script.new("return redis.call('SET', KEYS[1], ARGV[1])")
{:ok, "OK"} = Redis.Script.eval(conn, script, keys: ["mykey"], args: ["myval"])
# Pre-load into Redis script cache
:ok = Redis.Script.load(conn, script)The SHA1 is computed once at struct creation. Subsequent eval calls
try EVALSHA first (fast path), falling back to EVAL only on NOSCRIPT.
Summary
Functions
Evaluates the script against a connection.
Evaluates the script, raising on error.
Read-only variant of eval (EVALSHA_RO / EVAL_RO). Safe for use on replicas.
Checks if the script is cached on the server.
Pre-loads the script into the server's script cache via SCRIPT LOAD.
Creates a new Script from Lua source code. Computes the SHA1 hash immediately.
Types
Functions
@spec eval(GenServer.server(), t(), keyword()) :: {:ok, term()} | {:error, term()}
Evaluates the script against a connection.
Tries EVALSHA first. On NOSCRIPT error, falls back to EVAL (which also loads the script into the server cache for future EVALSHA calls).
Options
:keys- list of Redis keys (default: []):args- list of additional arguments (default: [])
@spec eval!(GenServer.server(), t(), keyword()) :: term()
Evaluates the script, raising on error.
@spec eval_ro(GenServer.server(), t(), keyword()) :: {:ok, term()} | {:error, term()}
Read-only variant of eval (EVALSHA_RO / EVAL_RO). Safe for use on replicas.
@spec exists?(GenServer.server(), t()) :: boolean()
Checks if the script is cached on the server.
@spec load(GenServer.server(), t()) :: :ok | {:error, term()}
Pre-loads the script into the server's script cache via SCRIPT LOAD.
Creates a new Script from Lua source code. Computes the SHA1 hash immediately.