NAME
AnyEvent::Redis::RipeRedis - Flexible non-blocking Redis client with reconnect feature
SYNOPSIS
use AnyEvent;
use AnyEvent::Redis::RipeRedis qw( :err_codes );
my $cv = AnyEvent->condvar();
my $redis = AnyEvent::Redis::RipeRedis->new(
host => 'localhost',
port => '6379',
password => 'your_password',
reconnect => 1,
encoding => 'utf8',
on_connect => sub {
print "Connected to Redis server\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";
},
);
# Set value
$redis->set( '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;
if (
$err_code == E_CANT_CONN
or $err_code == E_LOADING_DATASET
or $err_code == E_IO
or $err_code == E_CONN_CLOSED_BY_REMOTE_HOST
) {
# Repeat 'set' command
}
else {
$cv->croak( "$err_msg. Error code: $err_code" );
}
}
} );
$cv->recv();
$redis->disconnect();
DESCRIPTION
AnyEvent::Redis::RipeRedis is a non-blocking flexible Redis client with reconnect 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
new()
my $redis = AnyEvent::Redis::RipeRedis->new(
host => 'localhost',
port => '6379',
password => 'your_password',
database => 1,
lazy => 1,
connection_timeout => 5,
reconnect => 1,
encoding => 'utf8',
on_connect => sub {
print "Connected to Redis server\n";
},
on_disconnect => sub {
print "Disconnected from Redis server\n";
},
on_connect_error => sub {
my $err_msg = shift;
warn "$err_msg\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, then
AUTHcommand will be executed immediately after connection and after every reconnection. - database
-
Database index. If it set, then client will be switched to specified database immediately after connection and after every reconnection. Default database index is
0. - connection_timeout
-
Connection timeout. If after this timeout client could not connect to the server, callback
on_erroris called. By default used kernel's connection timeout. - lazy
-
If this parameter is set, then connection will be established, when you will send a first command to the server. By default connection establishes after calling method
new. - reconnect
-
If this parameter is TRUE and connection to the Redis server was lost, then client will try to reconnect to server while executing next command. Client try to reconnect only once and if fails, calls
on_errorcallback. If you need several attempts of reconnection, just retry command fromon_errorcallback as many times, as you need. This feature made client more responsive.By default is TRUE.
- encoding
-
Used to decode and encode strings during input/output operations. Not set by default.
- on_connect => $cb->()
-
Callback
on_connectis called, when connection is established. Not set by default. - on_disconnect => $cb->()
-
Callback
on_disconnectis called, when connection is closed. Not set by default. - on_connect_error => $cb->( $err_msg )
-
Callback
on_connect_erroris called, when the connection could not be established. If this collback isn't specified, thenon_errorcallback is called. - on_error => $cb->( $err_msg, $err_code )
-
Callback
on_erroris called, when any error occurred. If callback is no set just prints error message toSTDERR.
COMMAND EXECUTION
<command>( [ @args[, \%params ] ] )
# Set value
$redis->set( 'foo', 'Some string' );
# Increment
$redis->incr( 'bar', {
on_done => sub {
my $data = shift;
print "$data\n";
},
} );
# 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;
$cv->croak( "$err_msg. Error code: $err_code" );
},
} );
Full list of Redis commands can be found here: http://redis.io/commands
- on_done => $cb->( $data )
-
Callback
on_doneis called, when command handling is done. - on_error => $cb->( $err_msg, $err_code )
-
Callback
on_erroris called, when any error occurred.
SUBSCRIPTIONS
subscribe( @channels[, \%params ] )
Subscribe to channels 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";
},
} );
- on_done => $cb->( $ch_name, $sub_num )
-
Callback
on_doneis called, when subscription is done. - on_message => $cb->( $ch_name, $msg )
-
Callback
on_messageis called, when published message is received. - on_error => $cb->( $err_msg, $err_code )
-
Callback
on_erroris called, when any error occurred.
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";
},
} );
- on_done => $cb->( $ch_pattern, $sub_num )
-
Callback
on_doneis called, when subscription is done. - on_message => $cb->( $ch_name, $msg, $ch_pattern )
-
Callback
on_messageis called, when published message is received. - on_error => $cb->( $err_msg, $err_code )
-
Callback
on_erroris called, when any error occurred.
unsubscribe( @channels[, \%params ] )
Unsubscribe from channels 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";
},
} );
- on_done => $cb->( $ch_name, $sub_num )
-
Callback
on_doneis called, when unsubscription is done. - on_error => $cb->( $err_msg, $err_code )
-
Callback
on_erroris called, when any error occurred.
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";
},
} );
- on_done => $cb->( $ch_pattern, $sub_num )
-
Callback
on_doneis called, when unsubscription is done. - on_error => $cb->( $err_msg, $err_code )
-
Callback
on_erroris called, when any error occurred.
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 have to specify unix/, and in the parameter port you have to specify the path to the socket.
my $redis = AnyEvent::Redis::RipeRedis->new(
host => 'unix/',
port => '/tmp/redis.sock',
);
ERROR CODES
Error codes can be used for programmatic handling of errors.
1 - E_CANT_CONN
2 - E_LOADING_DATASET
3 - E_IO
4 - E_CONN_CLOSED_BY_REMOTE_HOST
5 - E_CONN_CLOSED_BY_CLIENT
6 - E_NO_CONN
7 - E_INVALID_PASS
8 - E_AUTH_REQUIRED
9 - E_COMMAND_EXEC
10 - E_CLIENT
- E_CANT_CONN
-
Can't connect to server.
- E_LOADING_DATASET
-
Redis is loading the dataset in memory.
- E_IO
-
Input/Output operation error. Connection closed.
- E_CONN_CLOSED_BY_REMOTE_HOST
-
Connection closed by remote host.
- E_CONN_CLOSED_BY_CLIENT
-
Connection closed by client.
Error can occur if at time of disconnection in client queue were uncompleted commands.
- E_NO_CONN
-
No connection to the server.
Error can occur at time of command execution if connection was closed by any reason and parameter
reconnectwas set to FALSE. - E_INVALID_PASS
-
Invalid password.
- E_AUTH_REQUIRED
-
Operation not permitted. Authentication required.
- E_COMMAND_EXEC
-
Command execution error.
- E_CLIENT
-
Client error.
To use constants of error codes you have to import them.
use AnyEvent::Redis::RipeRedis qw( :err_codes );
DISCONNECTION
When the connection to the server is no longer needed you can close it in three ways: call method disconnect(), send QUIT command or you can just "forget" any references to an AnyEvent::Redis::RipeRedis object, but in this case client object destroying silently without calling any callbacks including on_disconnect callback to avoid unexpected behavior.
disconnect()
Method for synchronous disconnection.
$redis->disconnect();
SEE ALSO
AnyEvent, AnyEvent::Redis, Redis
AUTHOR
Eugene Ponizovsky, <ponizovsky@gmail.com>
Special thanks
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.