EV::Hiredis(3pm)

πŸ“¦ version 0.07, in alpine 3.24

πŸ’– 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, or path), then you have to call the connect or connect_unix method by hand to connect to the redis-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 (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