The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

AnyEvent::Redis::RipeRedis - Non-blocking Redis client with reconnection feature

SYNOPSIS

  use AnyEvent;
  use AnyEvent::Redis::RipeRedis qw( :err_codes );

  my $redis = AnyEvent::Redis::RipeRedis->new(
    host => 'localhost',
    port => '6379',
    password => 'your_password',
    encoding => 'utf8',

    on_connect => sub {
      print "Connected\n";
    },

    on_disconnect => sub {
      print "Disconnected from Redis server\n";
    },

    on_error => sub {
      my $err_msg = shift;
      my $err_code = shift;

      warn "$err_msg. Error code: $err_code\n";
    },
  );

  my $cv = AnyEvent->condvar();

  # Increment
  $redis->incr( 'foo', {
    on_done => sub {
      my $data = shift;
      print "$data\n";
    },
  } );

  # Set value (retry on error)
  set( $redis, 'bar', 'Some string', {
    on_done => sub {
      my $data = shift;
      print "$data\n";
      $cv->send();
    },

    on_error => sub {
      my $err_msg = shift;
      my $err_code = shift;

      warn "$err_msg. Error code: $err_code\n";
      $cv->croak();
    }
  } );

  $cv->recv();

  $redis->disconnect();


  ####
  sub set {
    my $redis = shift;
    my $key = shift;
    my $value = shift;
    my $params = shift;

    $redis->set( $key, $value, {
      on_done => $params->{on_done},
      on_error => sub {
        my $err_msg = shift;
        my $err_code = shift;

        if (
          $err_code == E_CANT_CONN
            or $err_code == E_LOADING_DATASET
            or $err_code == E_IO_OPERATION
            or $err_code == E_CONN_CLOSED_BY_REMOTE_HOST
            ) {
          warn "$err_msg. Error code: $err_code\n";
          my $timer;
          $timer = AnyEvent->timer(
            after => 3,
            cb => sub {
              undef( $timer );
              set( $redis, $key, $value, $params );
            },
          );
        }
        else {
          $params->{on_error}->( $err_msg, $err_code );
        }
      },
    } );
  }

DESCRIPTION

AnyEvent::Redis::RipeRedis is a non-blocking Redis client with with reconnection feature. It supports subscriptions, transactions, has simple API and it faster than AnyEvent::Redis.

Requires Redis 1.2 or higher and any supported event loop.

CONSTRUCTOR

  my $redis = AnyEvent::Redis::RipeRedis->new(
    host => 'localhost',
    port => '6379',
    password => 'your_password',
    connection_timeout => 5,
    reconnect => 1,
    encoding => 'utf8',

    on_connect => sub {
      print "Connected\n";
    },

    on_disconnect => sub {
      print "Disconnected\n";
    },

    on_connect_error => sub {
      my $err_msg = shift;
      my $err_code = shift;

      warn "$err_msg. Error code: $err_code\n";
    },

    on_error => sub {
      my $err_msg = shift;
      my $err_code = shift;

      warn "$err_msg. Error code: $err_code\n";
    },
  );

host

Server hostname (default: 127.0.0.1)

port

Server port (default: 6379)

password

Authentication password. If it specified, AUTH command will be executed automaticaly.

connection_timeout

Connection timeout. If after this timeout client could not connect to the server, callback on_error is called.

reconnect

If this parameter is TRUE (by default), client in case of lost connection will attempt to reconnect to server, when executing next command. Client will attempt to reconnect only once and if it fails, call on_error callback. If you need several attempts of reconnection, just retry command from on_error callback as many times, as you need. This feature made client more responsive.

TRUE by default.

encoding

Used to decode and encode strings during read and write operations.

on_connect

This callback will be called, when connection will be established.

on_disconnect

This callback will be called, when client will be disconnected.

on_connect_error

This callback is called, when the connection could not be established. If this collback isn't specified, then on_error callback is called.

on_error

This callback is called when some error occurred, such as not being able to resolve the hostname, failure to connect, or a read error.

COMMAND EXECUTION

<command>( [ @cmd_args[, \%params ] ] )

  # Increment
  $redis->incr( 'foo', {
    on_done => sub {
      my $data = shift;
      print "$data\n";
    },
  } );

  # Set value
  $redis->set( 'bar', 'Some string' );

  # Get list of values
  $redis->lrange( 'list', 0, -1, {
    on_done => sub {
      my $data = shift;
      foreach my $val ( @{ $data } ) {
        print "$val\n";
      }
    },

    on_error => sub {
      my $err_msg = shift;
      my $err_code = shift;

      warn "$err_msg. Error code: $err_code\n";
    },
  } );

Full list of Redis commands can be found here: http://redis.io/commands

SUBSCRIPTIONS

subscribe( @channels[, \%params ] )

Subscribe to channel by name

  $redis->subscribe( qw( ch_foo ch_bar ), {
    on_done =>  sub {
      my $ch_name = shift;
      my $subs_num = shift;

      print "Subscribed: $ch_name. Active: $subs_num\n";
    },

    on_message => sub {
      my $ch_name = shift;
      my $msg = shift;

      print "$ch_name: $msg\n";
    },
  } );

psubscribe( @patterns[, \%params ] )

Subscribe to group of channels by pattern

  $redis->psubscribe( qw( info_* err_* ), {
    on_done =>  sub {
      my $ch_pattern = shift;
      my $subs_num = shift;

      print "Subscribed: $ch_pattern. Active: $subs_num\n";
    },

    on_message => sub {
      my $ch_name = shift;
      my $msg = shift;
      my $ch_pattern = shift;

      print "$ch_name ($ch_pattern): $msg\n";
    },

    on_error => sub {
      my $err_msg = shift;
      warn "$err_msg\n";
    },
  } );

unsubscribe( @channels[, \%params ] )

Unsubscribe from channel by name

  $redis->unsubscribe( qw( ch_foo ch_bar ), {
    on_done => sub {
      my $ch_name = shift;
      my $subs_num = shift;

      print "Unsubscribed: $ch_name. Active: $subs_num\n";
    },

    on_error => sub {
      my $err_msg = shift;
      warn "$err_msg\n";
    },
  } );

punsubscribe( @patterns[, \%params ] )

Unsubscribe from group of channels by pattern

  $redis->punsubscribe( qw( info_* err_* ), {
    on_done => sub {
      my $ch_pattern = shift;
      my $subs_num = shift;

      print "Unsubscribed: $ch_pattern. Active: $subs_num\n";
    },

    on_error => sub {
      my $err_msg = shift;
      warn "$err_msg\n";
    },
  } );

CONNECTION VIA UNIX-SOCKET

Redis 2.2 and higher support connection via UNIX domain socket. To connect via a UNIX-socket in the parameter host you must specify unix/, and in the parameter port you must specify the path to the socket.

  my $redis = AnyEvent::Redis::RipeRedis->new(
    host => 'unix/',
    port => '/tmp/redis.sock',
  );

ERROR CODES

Error codes were introduced in version of module 1.100

  1  - E_CANT_CONN
  2  - E_LOADING_DATASET
  3  - E_IO_OPERATION
  4  - E_CONN_CLOSED_BY_REMOTE_HOST
  5  - E_CONN_CLOSED_ON_DEMAND
  6  - E_NO_CONN
  7  - E_INVALID_PASS
  8  - E_AUTH_REQUIRED
  9  - E_COMMAND_EXEC
  10 - E_UNEXPECTED
E_CANT_CONN

Can't connect to server.

E_LOADING_DATASET

Redis is loading the dataset in memory.

E_IO_OPERATION

I/O operation error.

E_CONN_CLOSED_BY_REMOTE_HOST

Connection closed by remote host.

E_CONN_CLOSED_ON_DEMAND

Connection closed on demand.

E_NO_CONN

No connection to the server.

E_INVALID_PASS

Invalid password

E_AUTH_REQUIRED

Operation not permitted. Authentication required.

E_COMMAND_EXEC

Command execution error.

E_UNEXPECTED

Unexpected error.

To use error codes constants you must import them.

  use AnyEvent::Redis::RipeRedis qw( :err_codes );

DISCONNECTION FROM SERVER

When the connection to the server is no longer needed you can close it in three ways: send QUIT command, call method disconnect(), or you can just "forget" any references to an AnyEvent::Redis::RipeRedis object, but in this case client don't calls on_disconnect callback.

  $redis->quit(
    on_done => sub {
      # Do something
    }
  } );

  $redis->disconnect();

  undef( $redis );

SEE ALSO

AnyEvent, AnyEvent::Redis, Redis

AUTHOR

Eugene Ponizovsky, <ponizovsky@gmail.com>

Special thanks to:

  • Alexey Shrub

  • Vadim Vlasov

  • Konstantin Uvarin

COPYRIGHT AND LICENSE

Copyright (c) 2012, Eugene Ponizovsky, <ponizovsky@gmail.com>. All rights reserved.

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