NAME
Async::Redis::Script - Reusable Lua script wrapper with EVALSHA optimization
SYNOPSIS
# Create via redis->script()
my $script = $redis->script(<<'LUA');
local current = redis.call('GET', KEYS[1]) or 0
return current + ARGV[1]
LUA
# Preferred: run() with explicit keys and args arrays
my $result = await $script->run(['mykey'], [10]);
# Run on a different connection
my $result = await $script->run_on($other_redis, ['mykey'], [10]);
# Legacy: call_with_keys (positional)
my $result = await $script->call_with_keys(1, 'mykey', 10);
# Legacy: call (assumes no KEYS, all ARGV)
my $result = await $script->call('arg1', 'arg2');
DESCRIPTION
Script objects wrap Lua scripts for efficient reuse. They provide:
SHA1 hash computation and caching
Automatic EVALSHA with EVAL fallback on NOSCRIPT
Key prefixing support (via KeyExtractor)
Metadata for named command registration
CONSTRUCTOR
new
my $script = Async::Redis::Script->new(
redis => $redis, # Optional: default connection
script => $lua_code, # Required: Lua source
name => 'my_command', # Optional: for registry
num_keys => 1, # Optional: fixed key count or 'dynamic'
description => 'Does X', # Optional: documentation
);
Typically created via $redis->script($lua) or $redis->define_command().
METHODS
run
my $result = await $script->run(\@keys, \@args);
Preferred entry point. Executes the script with explicit keys and args arrays. Uses EVALSHA with automatic EVAL fallback.
run_on
my $result = await $script->run_on($redis, \@keys, \@args);
Execute on a specific Redis connection. Useful for:
Running in pipelines
Using with connection pools
Scripts created without a default connection
call
my $result = await $script->call(@args);
Legacy method. Assumes all arguments are ARGV (no KEYS). Equivalent to $script->run([], \@args).
call_with_keys
my $result = await $script->call_with_keys($numkeys, @keys_and_args);
Legacy method. First $numkeys arguments are KEYS, rest are ARGV.
ACCESSORS
sha
my $sha1 = $script->sha;
Returns the SHA1 hex digest of the script (lowercase).
script
my $lua = $script->script;
Returns the Lua source code.
name
my $name = $script->name;
Returns the command name (if registered via define_command).
num_keys
my $n = $script->num_keys;
Returns the expected number of keys, or 'dynamic' if variable.
description
my $desc = $script->description;
Returns the description string (if provided).
EVALSHA OPTIMIZATION
Scripts automatically use EVALSHA for efficiency. If the script isn't cached on the Redis server (NOSCRIPT error), it falls back to EVAL which also loads the script for future calls.
This is transparent - you don't need to manually load scripts.
SEE ALSO
Async::Redis - Main client with script() and define_command() methods