AnyEvent::Redis::RipeRedis - Non-blocking Redis client with reconnection feature
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', 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; warn "$err_msg. Error code: $err_code\n"; if ( $err_code == E_LOADING_DATASET ) { # Do something special } $cv->send(); } } ); $cv->recv(); $redis->disconnect();
AnyEvent::Redis::RipeRedis is a non-blocking Redis client 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.
Sorry for my clumsy English. I work on it.
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 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"; }, );
Server hostname (default: 127.0.0.1)
Server port (default: 6379)
Authentication password. If it specified, AUTH command will be executed automaticaly.
AUTH
Connection timeout. If after this timeout client could not connect to the server, callback on_error is called.
on_error
If this parameter is TRUE (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, calls 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.
Used to decode and encode strings during read and write operations.
Callback on_connect is called, when connection is established.
on_connect
Callback on_disconnect is called, when connection is closed.
on_disconnect
Callback on_connect_error is called, when the connection could not be established. If this collback isn't specified, then on_error callback is called.
on_connect_error
Callback on_error is called, when any error occurred.
# 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
Callback on_done is called, when command handling is done.
on_done
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"; }, } );
Callback on_done is called, when subscription is done.
Callback on_message is called, when published message is received.
on_message
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 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"; }, } );
Callback on_done is called, when unsubscription is done.
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"; }, } );
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.
host
unix/
port
my $redis = AnyEvent::Redis::RipeRedis->new( host => 'unix/', port => '/tmp/redis.sock', );
Error codes were introduced in version of module 1.100. They 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
Can't connect to server.
Redis is loading the dataset in memory.
Input/Output operation error. Connection closed.
Connection closed by remote host.
Connection closed by client.
No connection to the server. Occurs if connection was closed by any reason and parameter reconnect set to FALSE
reconnect
Invalid password.
Operation not permitted. Authentication required.
Command execution error.
Client error.
To use constants of error codes you have to import them.
use AnyEvent::Redis::RipeRedis qw( :err_codes );
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()
QUIT
Method for synchronous disconnection.
$redis->disconnect();
Asynchronous disconnection using the Redis command QUIT.
$redis->quit( { on_done => sub { $cv->send(); } } );
AnyEvent, AnyEvent::Redis, Redis
Eugene Ponizovsky, <ponizovsky@gmail.com>
Alexey Shrub
Vadim Vlasov
Konstantin Uvarin
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.
To install AnyEvent::Redis::RipeRedis, copy and paste the appropriate command in to your terminal.
cpanm
cpanm AnyEvent::Redis::RipeRedis
CPAN shell
perl -MCPAN -e shell install AnyEvent::Redis::RipeRedis
For more information on module installation, please visit the detailed CPAN module installation guide.