Redis.Script (Redis v0.7.1)

Copy Markdown View Source

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

t()

@type t() :: %Redis.Script{sha: String.t(), source: String.t()}

Functions

eval(conn, script, opts \\ [])

@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: [])

eval!(conn, script, opts \\ [])

@spec eval!(GenServer.server(), t(), keyword()) :: term()

Evaluates the script, raising on error.

eval_ro(conn, script, opts \\ [])

@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.

exists?(conn, script)

@spec exists?(GenServer.server(), t()) :: boolean()

Checks if the script is cached on the server.

load(conn, script)

@spec load(GenServer.server(), t()) :: :ok | {:error, term()}

Pre-loads the script into the server's script cache via SCRIPT LOAD.

new(source)

@spec new(String.t()) :: t()

Creates a new Script from Lua source code. Computes the SHA1 hash immediately.