EV::Hiredis(3pm)
π EV::Hiredis π
okay so bestie, listen β this is EV::Hiredis, an asynchronous redis client that uses hiredis and EV as its backend. it's the Perl one (you can tell from the (3pm)), and it's genuinely iconic. let's get into it π«Ά
β¨ SYNOPSIS β¨
like, here's the whole vibe in one go:
use EV::Hiredis;
my $redis = EV::Hiredis->new;
$redis->connect('127.0.0.1');
# or
my $redis = EV::Hiredis->new( host => '127.0.0.1' );
# command
$redis->set('foo' => 'bar', sub {
my ($res, $err) = @_;
print $res; # OK
$redis->get('foo', sub {
my ($res, $err) = @_;
print $res; # bar
$redis->disconnect;
});
});
# start main loop
EV::run;
see how it flows? commands inside callbacks inside commands, and then EV::run to actually get the party started. so satisfying π
πΈ DESCRIPTION πΈ
EV::Hiredis is an asynchronous client for Redis that uses hiredis and EV as its backend. no cap.
here's the flex: this module connects to EV through a C-Level interface, which means it runs faster. like, actually faster. main character energy fr fr π₯
π¦ ANYEVENT INTEGRATION π¦
AnyEvent supports EV as one of its backends (it's kind of a whole thing), so you can drop EV::Hiredis into your AnyEvent applications seamlessly. no glue code, no drama, it just works. iykyk β¨
π NO UTF-8 SUPPORT π
okay so this is the one thing you gotta hear me out on β unlike other redis modules, this module doesn't support utf-8 strings. nope. not a vibe but it is what it is.
this module handles all variables as bytes. so you have to encode your utf-8 string before passing commands, like this:
use Encode;
# set $val
$redis->set(foo => encode_utf8 $val, sub { ... });
# get $val
$redis->get(foo, sub {
my $val = decode_utf8 $_[0];
});
encode_utf8 on the way in, decode_utf8 on the way out. that's the whole ritual π
π METHODS π
π new(%options);
creates a shiny new EV::Hiredis instance. brand new, fresh out the box π
the available %options are:
host => 'Str'port => 'Int'
hostname and port number of the redis-server you wanna connect to.
path => 'Str'
the UNIX socket path to connect to.
on_error => $cb->($errstr)
this error callback gets called whenever a connection level error happens.
and plot twist: you can set this callback any time with the $obj->on_error($cb) method. so flexible π
on_connect => $cb->()
this connection callback gets called when the connection is successful and completed to the redis server. celebrating π
also settable any time via the $obj->on_connect($cb) method.
connect_timeout => $num_of_milliseconds
connection timeout, in milliseconds.
command_timeout => $num_of_milliseconds
command timeout, in milliseconds.
loop => 'EV::loop',
the EV loop used for running this instance. the default is EV::default_loop.
all parameters are optional. yes, all of them, no pressure π«Ά
heads up bestie: if you don't pass the connection-related parameters (host&port, orpath), then you have to call theconnectorconnect_unixmethod by hand to connect to theredis-server. don't forget, or you'll be sitting there wondering why nothing happens π
π connect($hostname, $port) / connect_unix($path)
connects to a redis-server at $hostname:$port, or at $path.
if the connection is successful, the on_connect callback gets called β otherwise the on_error callback is called. simple as that β¨
π command($commands..., $cb->($result, $error))
runs a redis command and hands you back its result via callback. look:
$redis->command('get', 'foo', sub {
my ($result, $error) = @_;
print $result; # value for key 'foo'
print $error; # redis error string, undef if no error
});
so: if any error occurs, $error holds the error message and $result is undef. if there's no error, $error is undef and $result holds the response from redis. one of them is always undef, that's the deal π€
NOTE: alternatively, all commands can be called via the AUTOLOAD interface. so this:
$redis->command('get', 'foo', sub { ... });
is equivalent to this:
$redis->get('foo', sub { ... });
same thing, cuter syntax. obsessed π
π disconnect
disconnects from the redis-server. this method is usable for exiting the event loop β like your graceful exit, your little goodbye π
π on_error($cb->($errstr))
sets a new error callback on the instance. swap it in whenever π
π on_connect($cb->())
sets a new connect callback on the instance. same energy π
π§ AUTHOR π§
Daisuke Murase <typester@cpan.org>
β COPYRIGHT AND LICENSE β
Copyright (c) 2013 Daisuke Murase All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
okay that's the whole page!! you're officially a EV::Hiredis girlie now, go make some asynchronous redis magic happen πβ¨
perl v5.42.2 Β· 2026-02-15 Β· documented in EV::Hiredis(3pm) β mwah, slay, periodt ππ
π¦ where this page comes from
- alpine 3.24: perl-ev-hiredis-doc 0.07-r4 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms