# `Redis.Script`
[🔗](https://github.com/joshrotenberg/redis_ex/blob/v0.7.1/lib/redis/script.ex#L1)

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.

# `t`

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

# `eval`

```elixir
@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!`

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

Evaluates the script, raising on error.

# `eval_ro`

```elixir
@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?`

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

Checks if the script is cached on the server.

# `load`

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

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

# `new`

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

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
