The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


EV::Hiredis - Asynchronous redis client using hiredis and EV


    use EV::Hiredis;
    my $redis = EV::Hiredis->new;
    # or
    my $redis = EV::Hiredis->new( host => '' );
    # command
    $redis->set('foo' => 'bar', sub {
        my ($res, $err) = @_;
        print $res; # OK
        $redis->get('foo', sub {
            my ($res, $err) = @_;
            print $res; # bar
    # start main loop


EV::Hiredis is a asynchronous client for Redis using hiredis and EV as backend.

This module connected to EV with C-Level interface so that it runs faster.


AnyEvent has a support for EV as its one of backends, so EV::Hiredis can be used in your AnyEvent applications seamlessly.


Unlike other redis modules, this module doesn't support utf-8 string.

This module handle all variables as bytes. You should encode your utf-8 string before passing commands like following:

    use Encode;
    # set $val
    $redis->set(foo => encode_utf8 $val, sub { ... });
    # get $val
    $redis->get(foo, sub {
        my $val = decode_utf8 $_[0];



Create new EV::Hiredis instance.

Available %options are:

  • host => 'Str'

  • port => 'Int'

    Hostname and port number of redis-server to connect.

  • path => 'Str'

    UNIX socket path to connect.

  • on_error => $cb->($errstr)

    Error callback will be called when a connection level error occurs.

    This callback can be set by $obj->on_error($cb) method any time.

  • on_connect => $cb->()

    Connection callback will be called when connection successful and completed to redis server.

    This callback can be set by $obj->on_connect($cb) method any time.

  • connect_timeout => $num_of_milliseconds

    Connection timeout.

  • command_timeout => $num_of_milliseconds

    Command timeout.

  • loop => 'EV::loop',

    EV loop for running this instance. Default is EV::default_loop.

All parameters are optional.

If parameters about connection (host&port or path) is not passed, you should call connect or connect_unix method by hand to connect to redis-server.

connect($hostname, $port)


Connect to a redis-server for $hostname:$port or $path.

on_connect callback will be called if connection is successful, otherwise on_error callback is called.

command($commands..., $cb->($result, $error))

Do a redis command and return its result by callback.

    $redis->command('get', 'foo', sub {
        my ($result, $error) = @_;

        print $result; # value for key 'foo'
        print $error;  # redis error string, undef if no error

If any error is occurred, $error presents the error message and $result is undef. If no error, $error is undef and $result presents response from redis.

NOTE: Alternatively all commands can be called via AUTOLOAD interface.

    $redis->command('get', 'foo', sub { ... });

is equivalent to:

    $redis->get('foo', sub { ... });


Disconnect from redis-server. This method is usable for exiting event loop.


Set new error callback to the instance.


Set new connect callback to the instance.


Daisuke Murase <>


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.