Async-Redis

 view release on metacpan or  search on metacpan

lib/Async/Redis/Cookbook.pod  view on Meta::CPAN

=pod

=head1 NAME

Async::Redis::Cookbook - Practical Async::Redis recipes

=head1 SYNOPSIS

This cookbook shows small, copyable Async::Redis patterns. The examples are
written for use inside an C<async sub>:

=over 4

=item * C<$redis> is a connected L<Async::Redis> client.

=item * C<$host> and C<$port> are the Redis endpoint.

=item * C<$prefix> is a unique key prefix for the example.

=item * C<Future>, C<Future::AsyncAwait>, C<Future::IO>, C<Async::Redis>, and
C<Async::Redis::Pool> are loaded where needed.

=back

The C<die> checks are intentional. They keep the cookbook examples executable
by C<t/00-pod/cookbook-examples.t> so the documentation does not drift away
from the implementation.

=head1 RECIPES

=head2 Connect And Ping

Create a client, connect explicitly, use it, then disconnect when done.

=for cookbook-test connect_and_ping

    my $client = Async::Redis->new(
        host            => $host,
        port            => $port,
        connect_timeout => 2,
    );

    await $client->connect;
    my $pong = await $client->ping;
    die "expected PONG" unless $pong eq 'PONG';

    $client->disconnect;

=for cookbook-test-end connect_and_ping

=head2 Basic GET/SET

Most command methods return a Future. Inside an C<async sub>, C<await> the
result.

=for cookbook-test basic_get_set

    await $redis->set("$prefix:hello", 'world');

    my $value = await $redis->get("$prefix:hello");
    die "unexpected value: $value" unless $value eq 'world';

=for cookbook-test-end basic_get_set

=head2 Run Independent Commands Concurrently

Start independent commands before awaiting them, then use
C<< Future->needs_all >> to wait for all results. Responses are matched to the
right Futures in command order by Async::Redis.

=for cookbook-test concurrent_commands

    my @futures = (
        $redis->set("$prefix:k1", 'v1'),
        $redis->set("$prefix:k2", 'v2'),
        $redis->incr("$prefix:counter"),



( run in 0.621 second using v1.01-cache-2.11-cpan-524268b4103 )