AnyEvent::Redis(3pm)

πŸ“¦
version 0.24 Β· 3 versions
, in debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ NAME ✨

AnyEvent::Redis β€” bestie, it's a non-blocking Redis client πŸ’… (this is the AnyEvent::Redis(3pm) page btw, in case you were wondering what you just opened)


🌸 SYNOPSIS 🌸

okay so here's the vibe, literally copy-paste core:

use AnyEvent::Redis;

my $redis = AnyEvent::Redis->new(
    host => '127.0.0.1',
    port => 6379,
    encoding => 'utf8',
    on_error => sub { warn @_ },
    on_cleanup => sub { warn "Connection closed: @_" },
);

# callback based
$redis->set( 'foo'=> 'bar', sub { warn "SET!" } );
$redis->get( 'foo', sub { my $value = shift } );

my ($key, $value) = ('list_key', 123);
$redis->lpush( $key, $value );
$redis->lpop( $key, sub { my $value = shift });

# condvar based
my $cv = $redis->lpop( $key );
$cv->cb(sub { my $value = $_[0]->recv });

like. look at it. it's giving effortless ✨


πŸ¦‹ DESCRIPTION πŸ¦‹

AnyEvent::Redis is a non-blocking (event-driven) Redis client. no cap. it does not sit there waiting, it has main character energy and moves on immediately.

⚠️ real talk: this module is an AnyEvent user, so you must install and use a supported event loop. no event loop, no slay. periodt.

πŸ’– ESTABLISHING A CONNECTION πŸ’–

to make a new connection, hit up the new() method with these attributes:

  • host => <HOSTNAME> β€” Required. this is the hostname (or the literal address) of the server. you gotta have it, bestie, it's not optional πŸ™ˆ
  • port => <PORT> β€” Optional. the server port. ✨
  • encoding => <ENCODING> β€” Optional. it encodes and decodes data when you're storing and retrieving, respectively, according to ENCODING. "utf8" is what she recommends πŸ’…, or go peek at Encode::Supported for the deets on what ENCODING values you can use.
    • Omit this entirely if you intend to handle raw binary data with this connection. binary besties, this one's for you 🫢
  • on_error => $cb->($errmsg) β€” Optional. a callback that fires if a connection or database-level error happens. the error message gets passed to the callback as the sole argument. just the one. 😭
  • on_cleanup => $cb->($errmsg) β€” Optional. a callback that fires if a connection error occurs. the error message gets passed as the sole argument too. and like, after this callback, errors will be reported for all outstanding requests. so keep that in mind fr fr.

πŸŽ€ METHODS πŸŽ€

all methods supported by your version of Redis should be supported. all of them. every single one. obsessed. πŸ‘‘

⭐ Normal commands ⭐

there are two alternative approaches for handling results from commands, pick your fighter:

  • AnyEvent::CondVar based:
my $cv = $redis->command(
  # arguments to command
);

# Then...
my $res;
eval {
    # Could die()
    $res = $cv->recv;
};
warn $@ if $@;

# or...
$cv->cb(sub {
  my ($cv) = @_;
  my ($result, $err) = $cv->recv
});
  • Callback:
$redis->command(
  # arguments,
  sub {
    my ($result, $err) = @_;
  });

(the callback is literally just a wrapper around the $cv approach, iykyk)

🌈 Transactions (MULTI/EXEC) 🌈

Redis transactions begin with a multi command and end with an exec command. commands in between are not executed immediately when they're sent β€” they just chill there. on receipt of the exec, the server executes all the saved commands atomically, and returns all their results as one bulk reply. iconic behaviour honestly.

after a transaction is finished, results for each individual command are reported in the usual way. so by the time any of these callbacks is called, the entire transaction is finished β€” for better or worse. no takebacks πŸ’€

results of the exec (which contain all the other results) will be returned as an array reference containing all of the individual results. this may in some cases make callbacks on the individual commands unnecessary, or vice versa. and in this bulk reply, errors reported for each individual command are represented by objects of class AnyEvent::Redis::Error, which will respond to a ->message method call with that error message. cute, right?

🚨 two hard nos: it is not permitted to nest transactions, and this module does not permit subscription-related commands in a transaction. don't try it.

🧚 Subscriptions 🧚

the subscription methods (subscribe and psubscribe) must be used with a callback:

my $cv = $redis->subscribe("test", sub {
  my ($message, $channel[, $actual_channel]) = @_;
  # ($actual_channel is provided for pattern subscriptions.)
});

the $cv condition will be met on unsubscribing from the channel. bye bestie πŸ‘‹

⚠️ due to limitations of the Redis protocol, the only valid commands on a connection with an active subscription are subscribe and unsubscribe commands. that's it. that's the whole list. don't push your luck πŸ™ˆ

πŸ’œ Common methods πŸ’œ

  • get
  • set
  • hset
  • hget
  • lpush
  • lpop

the Redis command reference (http://redis.io/commands) lists all commands Redis supports. go read it, it's a whole thing ✨


πŸ“ REQUIREMENTS πŸ“

this requires Redis >= 1.2. yes, a real number, no cap.


Tatsuhiko Miyagawa <miyagawa@bulknews.net> 2009-


πŸ’… LICENSE πŸ’…

this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. slay.


🌟 AUTHORS 🌟

  • Tatsuhiko Miyagawa
  • David Leadbeater
  • Chia-liang Kao
  • franck cuny
  • Lee Aylward
  • Joshua Barratt
  • Jeremy Zawodny
  • Leon Brocard
  • Michael S. Fischer
  • Chip Salzenberg

the whole crew, we love to see it 🫢


πŸ¦„ SEE ALSO πŸ¦„

  • Redis
  • AnyEvent

that's the whole page bestie πŸ’– this was perl v5.38.2, dated 2024-03-04 β€” so if you're reading this from the future, hi, hope the Redis is still running 😭✌️ mwah πŸ’‹

πŸ“¦ where this page comes from