Async-Redis
view release on metacpan or search on metacpan
lib/Async/Redis/Cookbook.pod view on Meta::CPAN
Register scripts with C<define_command>, then run them explicitly with
C<run_script>. Script names are per client instance; they are not installed as
Perl methods.
=for cookbook-test lua_define_command
$redis->define_command(cookbook_incrby => {
keys => 1,
lua => <<'LUA',
local current = tonumber(redis.call('GET', KEYS[1]) or 0)
local next_value = current + tonumber(ARGV[1])
redis.call('SET', KEYS[1], next_value)
return next_value
LUA
});
my $result = await $redis->run_script(
'cookbook_incrby',
"$prefix:lua:counter",
7,
);
die "script returned wrong value" unless $result == 7;
=for cookbook-test-end lua_define_command
=head2 Use A Pool With C<with>
C<with> is the preferred pool API because it releases the connection even when
your callback dies.
=for cookbook-test pool_with
my $pool = Async::Redis::Pool->new(
host => $host,
port => $port,
min => 1,
max => 2,
);
my $value = await $pool->with(async sub {
my ($conn) = @_;
await $conn->set("$prefix:pool:key", 'pooled');
return await $conn->get("$prefix:pool:key");
});
die "pool returned wrong value" unless $value eq 'pooled';
my $stats = $pool->stats;
die "pool leaked active connection" unless $stats->{active} == 0;
$pool->shutdown;
=for cookbook-test-end pool_with
=head1 PITFALLS
=over 4
=item * Do not share one connection between a C<BLPOP> worker and unrelated
commands. The connection is blocked until Redis replies.
=item * Do not mix C<next> and C<on_message> on the same subscription.
Callback mode is sticky.
=item * Do not return dirty connections to a pool. Use C<< $pool->with(...) >>
or let the pool destroy/clean dirty connections on release.
=item * Do not rely on C<prefix> as a security boundary. Use Redis ACLs or
separate databases for tenant isolation.
=back
=head1 SEE ALSO
L<Async::Redis>, L<Async::Redis::Pool>, L<Async::Redis::Subscription>,
L<Async::Redis::Script>
=cut
( run in 0.551 second using v1.01-cache-2.11-cpan-df04353d9ac )