AnyEvent::Redis::RipeRedis - Flexible non-blocking Redis client with reconnect feature
use AnyEvent; use AnyEvent::Redis::RipeRedis qw( :err_codes ); my $cv = AE::cv(); 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( 'foo', 'Some string', { on_done => sub { print "SET is done\n"; $cv->send(); }, on_error => sub { my $err_msg = shift; my $err_code = shift; $cv->croak( "$err_msg. Error code: $err_code" ); } } ); $cv->recv(); $redis->disconnect();
AnyEvent::Redis::RipeRedis is the flexible non-blocking Redis client with reconnect feature. The client supports subscriptions, transactions and connection via UNIX-socket.
Requires Redis 1.2 or higher, and any supported event loop.
my $redis = AnyEvent::Redis::RipeRedis->new( host => 'localhost', port => '6379', password => 'your_password', database => 7, lazy => 1, connection_timeout => 5, read_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 the password is specified, then the AUTH command will be send immediately to the server after a connection and after every reconnection.
AUTH
Database index. If the index is specified, then client will be switched to the specified database immediately after a connection and after every reconnection.
The default database index is 0.
0
If after this timeout the client could not connect to the server, the callback on_error is called with the error code E_CANT_CONN. The timeout must be specified in seconds and can contain a fractional part.
on_error
E_CANT_CONN
my $redis = AnyEvent::Redis::RipeRedis->new( connection_timeout => 10.5, );
By default the client use kernel's connection timeout.
If after this timeout the client do not received a response from the server to any command, the callback on_error is called with the error code E_READ_TIMEDOUT. The timeout must be specified in seconds and can contain a fractional part.
E_READ_TIMEDOUT
my $redis = AnyEvent::Redis::RipeRedis->new( read_timeout => 0.5, );
Not set by default.
If this parameter is set, then the connection will be established, when you will send the first command to the server. By default the connection establishes after calling of the method new.
new
If the connection to the Redis server was lost and the parameter 'reconnect' is TRUE, then the client try to restore the connection, when executing a next command. The client try to reconnect only once and if it fails, then is called the on_error callback. If you need several attempts of the reconnection, just retry a command from the on_error callback as many times, as you need. This feature made the client more responsive.
By default is TRUE.
Used for encode/decode strings during input/output operations.
The callback on_connect is called, when the connection is successfully established.
on_connect
The callback on_disconnect is called, when the connection is closed by any reason.
on_disconnect
The callback on_connect_error is called, when the connection could not be established. If this collback isn't specified, then the on_error callback is called with the error code E_CANT_CONN.
on_connect_error
The callback on_error is called, if any error occurred. If the callback is not set, the client just print an error message to STDERR.
STDERR
The full list of the Redis commands can be found here: http://redis.io/commands.
# 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" ); }, } );
The callback on_done is called, when the current operation is done.
on_done
The callback on_error is called, if any error occurred. If the callback is not set, then the on_error callback, that was specified in constructor, is called.
The detailed information abount the Redis transactions can be found here: http://redis.io/topics/transactions.
Marks the start of a transaction block. Subsequent commands will be queued for atomic execution using EXEC.
EXEC
Executes all previously queued commands in a transaction and restores the connection state to normal. When using WATCH, EXEC will execute commands only if the watched keys were not modified.
WATCH
Flushes all previously queued commands in a transaction and restores the connection state to normal.
If WATCH was used, DISCARD unwatches all keys.
DISCARD
Marks the given keys to be watched for conditional execution of a transaction.
Forget about all watched keys.
The detailed information about Redis Pub/Sub can be found here: http://redis.io/topics/pubsub
Subscribes the client to the specified channels.
Once the client enters the subscribed state it is not supposed to issue any other commands, except for additional SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE and PUNSUBSCRIBE commands.
SUBSCRIBE
PSUBSCRIBE
UNSUBSCRIBE
PUNSUBSCRIBE
$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_error => sub { my $err_msg = shift; my $err_code = shift; $cv->croak( "$err_msg. Error code: $err_code" ); }, } );
The callback on_done is called, when the current subscription operation is done.
The callback on_message is called, when a published message is received.
on_message
The callback on_error is called, if the subscription operation fails. If the callback is not set, then the on_error callback, that was specified in constructor, is called.
Subscribes the client to the given patterns.
$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; my $err_code = shift; $cv->croak( "$err_msg. Error code: $err_code" ); }, } );
The callback on_message is called, when published message is received.
Posts a message to the given channel.
Unsubscribes the client from the given channels, or from all of them if none is given.
When no channels are specified, the client is unsubscribed from all the previously subscribed channels. In this case, a message for every unsubscribed channel will be sent to the client.
Unsubscribes the client from the given patterns, or from all of them if none is given.
When no patters are specified, the client is unsubscribed from all the previously subscribed patterns. In this case, a message for every unsubscribed pattern will be sent to the client.
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', );
Redis 2.6 and higher support execution of Lua scripts on the server side. To execute a Lua script you can use one of the commands EVAL or EVALSHA, or you can use the special method eval_cached().
EVAL
EVALSHA
eval_cached()
When you call the eval_cached() method, the client first generate a SHA1 hash for a Lua script and cache it in memory. Then the client optimistically send the EVALSHA command under the hood. If the NO_SCRIPT error will be returned, the client send the EVAL command.
NO_SCRIPT
If you call the eval_cached() method with the same Lua script, client don't generate a SHA1 hash for this script repeatedly, it gets a hash from the cache.
$redis->eval_cached( 'return { KEYS[1], KEYS[2], ARGV[1], ARGV[2] }', 2, 'key1', 'key2', 'first', 'second', { on_done => sub { my $data = shift; foreach my $val ( @{$data} ) { print "$val\n"; } } } );
Every time when the calback on_error is called, the current error code passed to it in the second argument. Error codes can be used for programmatic handling of errors.
AnyEvent::Redis::RipeRedis provides constants of error codes, that can be imported and used in expressions.
use AnyEvent::Redis::RipeRedis qw( :err_codes );
Can't connect to the server. If this error occurred, the client abort all operations.
Redis is loading the dataset in memory.
Input/Output operation error. If this error occurred, the client abort all operations and close the connection.
The connection closed by remote host. If this error occurred, the client abort all operations.
If in the client queue are uncompleted operations, when application calling method disconnect() or executing command QUIT, the client abort them with this error.
disconnect()
QUIT
No connection to the server. Error occurs, if at time of a command execution the connection has been closed by any reason and the parameter reconnect was set to FALSE.
reconnect
Invalid password.
Operation not permitted.
Operation error. Usually returned by the the Redis server.
The client received unexpected data from the server. If this error occurred, the client abort all operations and close the connection.
No matching script. Use the EVAL command.
Read timed out. If this error occurred, the client abort all operations and close the connection.
When the connection to the server is no longer needed you can close it in three ways: call the method disconnect(), send the QUIT command or you can just "forget" any references to an AnyEvent::Redis::RipeRedis object, but in this case a client object is destroyed silently without calling any callbacks including the on_disconnect callback to avoid an unexpected behavior.
The method for synchronous disconnection.
$redis->disconnect();
Get, set or reset to default the connection_timeout of the client.
connection_timeout
Get, set or disable the read_timeout of the client.
read_timeout
Enable or disable reconnection mode of the client.
Get, set or disable the encoding.
encoding
Get, set or disable the on_connect callback.
Get, set or disable the on_disconnect callback.
Get, set or disable the on_connect_error callback.
Get, set or disable the on_error callback.
AnyEvent, AnyEvent::Redis, Redis, Redis::hiredis, RedisDB
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.