Deprecated.
NAME
AnyEvent::Redis::RipeRedis - DEPRECATED. Use AnyEvent::RipeRedis instead
SYNOPSIS
use AnyEvent;
use AnyEvent::Redis::RipeRedis;
my $cv = AE::cv();
my $redis = AnyEvent::Redis::RipeRedis->new(
host => 'localhost',
port => 6379,
password => 'yourpass',
);
$redis->incr( 'foo',
sub {
my $reply = shift;
if (@_) {
my $err_msg = shift;
my $err_code = shift;
warn "[$err_code] $err_msg\n";
return;
}
print "$reply\n";
}
);
$redis->set( 'bar', 'string',
{ on_error => sub {
my $err_msg = shift;
my $err_code = shift;
warn "[$err_code] $err_msg\n";
}
}
);
$redis->get( 'bar',
{ on_done => sub {
my $reply = shift;
print "$reply\n";
},
on_error => sub {
my $err_msg = shift;
my $err_code = shift;
warn "[$err_code] $err_msg\n";
},
}
);
$redis->quit(
sub {
my $reply = shift;
if (@_) {
my $err_msg = shift;
my $err_code = shift;
warn "[$err_code] $err_msg\n";
}
$cv->send();
}
);
$cv->recv();
DESCRIPTION
MODULE IS DEPRECATED. Use AnyEvent::RipeRedis instead. The interface of AnyEvent::RipeRedis has several differences from interface of AnyEvent::Redis::RipeRedis. For more information see documentation.
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.
CONSTRUCTOR
new( %params )
my $redis = AnyEvent::Redis::RipeRedis->new(
host => 'localhost',
port => 6379,
password => 'yourpass',
database => 7,
lazy => 1,
connection_timeout => 5,
read_timeout => 5,
reconnect => 1,
min_reconnect_interval => 5,
encoding => 'utf8',
on_connect => sub {
# handling...
},
on_disconnect => sub {
# handling...
},
on_connect_error => sub {
my $err_msg = shift;
# error handling...
},
on_error => sub {
my $err_msg = shift;
my $err_code = shift;
# error handling...
},
);
- host => $host
-
Server hostname (default: 127.0.0.1)
- port => $port
-
Server port (default: 6379)
- password => $password
-
If the password is specified, the
AUTH
command is sent to the server after connection. - database => $index
-
Database index. If the index is specified, the client is switched to the specified database after connection. You can also switch to another database after connection by using
SELECT
command. The client remembers last selected database after reconnection.The default database index is
0
. - encoding => $encoding_name
-
Used for encode/decode strings at time of input/output operations.
Not set by default.
- connection_timeout => $fractional_seconds
-
Specifies connection timeout. If the client could not connect to the server after specified timeout, the
on_error
oron_connect_error
callback is called. In case whenon_error
callback is called,E_CANT_CONN
error code is passed to callback in the second argument. The timeout specifies in seconds and can contain a fractional part.connection_timeout => 10.5,
By default the client use kernel's connection timeout.
- read_timeout => $fractional_seconds
-
Specifies read timeout. If the client could not receive a reply from the server after specified timeout, the client close connection and call the
on_error
callback with theE_READ_TIMEDOUT
error code. The timeout is specifies in seconds and can contain a fractional part.read_timeout => 3.5,
Not set by default.
- lazy => $boolean
-
If enabled, the connection establishes at time when you will send the first command to the server. By default the connection establishes after calling of the
new
method.Disabled by default.
- reconnect => $boolean
-
If the connection to the Redis server was lost and the parameter
reconnect
is TRUE, the client try to restore the connection on a next command executuion unlessmin_reconnect_interval
is specified. The client try to reconnect only once and if it fails, is called theon_error
callback. If you need several attempts of the reconnection, just retry a command from theon_error
callback as many times, as you need. This feature made the client more responsive.Enabled by default.
- min_reconnect_interval => $fractional_seconds
-
If the parameter is specified, the client will try to reconnect not often than after this interval.
min_reconnect_interval => 5,
Not set by default.
- handle_params => \%params
-
Specifies AnyEvent::Handle parameters.
handle_params => { autocork => 1, linger => 60, }
- linger => $fractional_seconds
-
Parameter is DEPRECATED. Use
handle_params
parameter instead.This option is in effect when, for example, code terminates connection by calling
disconnect
but there are ongoing operations. In this case destructor of underlying AnyEvent::Handle object will keep the write buffer in memory for long time (see default value) causing temporal 'memory leak'. See AnyEvent::Handle for more information.By default is applied default setting of AnyEvent::Handle (i.e. 3600 seconds).
- autocork => $boolean
-
Parameter is DEPRECATED. Use
handle_params
parameter instead.When enabled, writes to socket will always be queued till the next event loop iteration. This is efficient when you execute many operations per iteration, but less efficient when you execute a single operation only per iteration (or when the write buffer often is full). It also increases operation latency. See AnyEvent::Handle for more information.
Disabled by default.
- on_connect => $cb->()
-
The
on_connect
callback is called when the connection is successfully established.Not set by default.
- on_disconnect => $cb->()
-
The
on_disconnect
callback is called when the connection is closed by any reason.Not set by default.
- on_connect_error => $cb->( $err_msg )
-
The
on_connect_error
callback is called, when the connection could not be established. If this collback isn't specified, the commonon_error
callback is called with theE_CANT_CONN
error code.Not set by default.
- on_error => $cb->( $err_msg, $err_code )
-
The common
on_error
callback is called when ocurred some error, which was affected on whole client (e. g. connection error). Also this callback is called on other errors if neitheron_error
callback noron_reply
callback is specified in the command method. If commonon_error
callback is not specified, the client just print an error messages toSTDERR
.
COMMAND EXECUTION
<command>( [ @args ] [, $cb | \%cbs ] )
The full list of the Redis commands can be found here: http://redis.io/commands.
$redis->incr( 'foo',
sub {
my $reply = shift;
if (@_) {
my $err_msg = shift;
my $err_code = shift;
# error handling...
return;
}
print "$reply\n";
},
);
$redis->incr( 'foo',
{ on_reply => sub {
my $reply = shift;
if (@_) {
my $err_msg = shift;
my $err_code = shift;
# error handling...
return;
}
print "$reply\n";
},
}
);
$redis->set( 'bar', 'string' );
$redis->get( 'bar',
{ on_done => sub {
my $reply = shift;
print "$reply\n";
},
on_error => sub {
my $err_msg = shift;
my $err_code = shift;
# error handling...
}
}
);
$redis->lrange( 'list', 0, -1,
{ on_done => sub {
my $reply = shift;
foreach my $val ( @{$reply} ) {
print "$val\n";
}
},
on_error => sub {
my $err_msg = shift;
my $err_code = shift;
# error handling...
},
}
);
- on_done => $cb->( [ $reply ] )
-
The
on_done
callback is called when the current operation was completed successfully. - on_error => $cb->( $err_msg, $err_code )
-
The
on_error
callback is called when some error occurred. - on_reply => $cb->( [ $reply ] [, $err_msg, $err_code ] )
-
Since version 1.300 of the client you can specify single,
on_reply
callback, instead of two,on_done
andon_error
callbacks. Theon_reply
callback is called in both cases: when operation was completed successfully or when some error occurred. In first case to callback is passed only reply data. In second case to callback is passed three arguments: Theundef
value or reply data with error objects (see below), error message and error code.
TRANSACTIONS
The detailed information abount the Redis transactions can be found here: http://redis.io/topics/transactions.
multi( [ $cb | \%cbs ] )
Marks the start of a transaction block. Subsequent commands will be queued for atomic execution using EXEC
.
exec( [ $cb | \%cbs ] )
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.
If after execution of EXEC
command at least one operation fails, the on_error
or on_reply
callback is called and in addition to error message and error code to callback is passed reply data, which contain replies of successful operations and error objects for each failed operation. To on_error
callback reply data is passed in third argument and to on_reply
callback in first argument. Error object is an instance of class AnyEvent::Redis::RipeRedis::Error
and has two methods: message()
to get error message and code()
to get error code.
$redis->multi();
$redis->set( 'foo', 'string' );
$redis->incr( 'foo' ); # causes an error
$redis->exec(
sub {
my $reply = shift;
if (@_) {
my $err_msg = shift;
my $err_code = shift;
if ( defined $reply ) {
foreach my $nested_reply ( @{$reply} ) {
if ( ref($nested_reply) eq 'AnyEvent::Redis::RipeRedis::Error' ) {
my $nested_err_msg = $nested_reply->message();
my $nested_err_code = $nested_reply->code();
# error handling...
}
}
return;
}
# error handling...
return;
}
# reply handling...
},
);
$redis->multi();
$redis->set( 'foo', 'string' );
$redis->incr( 'foo' ); # causes an error
$redis->exec(
{ on_done => sub {
my $reply = shift;
# handling...
},
on_error => sub {
my $err_msg = shift;
my $err_code = shift;
my $reply = shift;
if ( defined $reply ) {
foreach my $nested_reply ( @{$reply} ) {
if ( ref($nested_reply) eq 'AnyEvent::Redis::RipeRedis::Error' ) {
my $nested_err_msg = $nested_reply->message();
my $nested_err_code = $nested_reply->code();
# error handling...
}
}
}
# error handling...
},
}
);
discard( [ $cb | \%cbs ] )
Flushes all previously queued commands in a transaction and restores the connection state to normal.
If WATCH
was used, DISCARD
unwatches all keys.
watch( @keys [, $cb | \%cbs ] )
Marks the given keys to be watched for conditional execution of a transaction.
unwatch( [ $cb | \%cbs ] )
Forget about all watched keys.
SUBSCRIPTIONS
The detailed information about Redis Pub/Sub can be found here: http://redis.io/topics/pubsub
Once the client enters the subscribed state it is not supposed to issue any other commands, except for additional SUBSCRIBE
, PSUBSCRIBE
, UNSUBSCRIBE
, PUNSUBSCRIBE
and QUIT
commands.
If the connection was lost when the client has active subscriptions, the on_error
or on_reply
callback in appropriate (p)subscribe
method is called.
subscribe( @channels, ( $cb | \%cbs ) )
Subscribes the client to the specified channels.
$redis->subscribe( qw( foo bar ),
{ on_reply => sub {
my $reply = shift;
if (@_) {
my $err_msg = shift;
my $err_code = shift;
# error handling...
return;
}
my $channel = $reply->[0];
my $channels_num = $reply->[1];
# handling...
},
on_message => sub {
my $channel = shift;
my $msg = shift;
# message handling...
},
}
);
$redis->subscribe( qw( foo bar ),
sub {
my $channel = shift;
my $msg = shift;
# message handling...
}
);
$redis->subscribe( qw( foo bar ),
{ on_done => sub {
my $channel = shift;
my $channels_num = shift;
# handling...
},
on_message => sub {
my $channel = shift;
my $msg = shift;
# message handling...
},
on_error => sub {
my $err_msg = shift;
my $err_code = shift;
# error handling...
},
}
);
- on_done => $cb->( $channel, $channels_num )
-
The
on_done
callback is called on every specified channel when the subscription operation was completed successfully. - on_message => $cb->( $channel, $msg )
-
The
on_message
callback is called when a published message was received. - on_error => $cb->( $err_msg, $err_code )
-
The
on_error
callback is called if the subscription operation fails. - on_reply => $cb->( [ $reply ] [, $err_msg, $err_code ] )
-
The
on_reply
callback is called in both cases: when the subscription operation was completed successfully or when subscription operation fails. In first caseon_reply
callback is called on every specified channel. Information about channel name and subscription number is passed to callback in first argument as an array reference.
psubscribe( @patterns, ( $cb | \%cbs ) )
Subscribes the client to the given patterns.
$redis->psubscribe( qw( foo_* bar_* ),
{ on_reply => sub {
my $reply = shift;
if (@_) {
my $err_msg = shift;
my $err_code = shift;
# error handling...
return;
}
my $pattern = $reply->[0];
my $channels_num = $reply->[1];
# handling...
},
on_message => sub {
my $channel = shift;
my $msg = shift;
my $pattern = shift;
# message handling...
},
}
);
$redis->psubscribe( qw( foo_* bar_* ),
sub {
my $channel = shift;
my $msg = shift;
my $pattern = shift;
# message handling...
}
);
$redis->psubscribe( qw( foo_* bar_* ),
{ on_done => sub {
my $pattern = shift;
my $channels_num = shift;
# handling...
},
on_message => sub {
my $channel = shift;
my $msg = shift;
my $pattern = shift;
# message handling...
},
on_error => sub {
my $err_msg = shift;
my $err_code = shift;
# error handling...
},
}
);
- on_done => $cb->( $pattern, $channels_num )
-
The
on_done
callback is called on every specified pattern when the subscription operation was completed successfully. - on_message => $cb->( $channel, $msg, $pattern )
-
The
on_message
callback is called when published message was received. - on_error => $cb->( $err_msg, $err_code )
-
The
on_error
callback is called if the subscription operation fails. - on_reply => $cb->( [ $reply ] [, $err_msg, $err_code ] )
-
The
on_reply
callback is called in both cases: when the subscription operation was completed successfully or when subscription operation fails. In first caseon_reply
callback is called on every specified pattern. Information about channel pattern and subscription number is passed to callback in first argument as an array reference.
publish( $channel, $message [, $cb | \%cbs ] )
Posts a message to the given channel.
unsubscribe( [ @channels ] [, $cb | \%cbs ] )
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.
$redis->unsubscribe( qw( ch_foo ch_bar ),
sub {
my $reply = shift;
if (@_) {
my $err_msg = shift;
my $err_code = shift;
# error handling...
return;
}
my $channel = $reply->[0];
my $channels_num = $reply->[1];
# handling...
}
);
$redis->unsubscribe( qw( ch_foo ch_bar ),
{ on_done => sub {
my $channel = shift;
my $channels_num = shift;
# handling...
},
}
);
- on_done => $cb->( $channel, $channels_num )
-
The
on_done
callback is called on every specified channel when the unsubscription operation was completed successfully. - on_error => $cb->( $err_msg, $err_code )
-
The
on_error
callback is called if the unsubscription operation fails. - on_reply => $cb->( [ $reply ] [, $err_msg, $err_code ] )
-
The
on_reply
callback is called in both cases: when the unsubscription operation was completed successfully or when unsubscription operation fails. In first caseon_reply
callback is called on every specified channel. Information about channel name and number of remaining subscriptions is passed to callback in first argument as an array reference.
punsubscribe( [ @patterns ] [, $cb | \%cbs ] )
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->punsubscribe( qw( foo_* bar_* ),
sub {
my $reply = shift;
if (@_) {
my $err_msg = shift;
my $err_code = shift;
# error handling...
return;
}
my $pattern = $reply->[0];
my $channels_num = $reply->[1];
# handling...
}
);
$redis->punsubscribe( qw( foo_* bar_* ),
{ on_done => sub {
my $pattern = shift;
my $channels_num = shift;
# handling...
},
}
);
- on_done => $cb->( $channel, $channels_num )
-
The
on_done
callback is called on every specified pattern when the unsubscription operation was completed successfully. - on_error => $cb->( $err_msg, $err_code )
-
The
on_error
callback is called if the unsubscription operation fails. - on_reply => $cb->( [ $reply ] [, $err_msg, $err_code ] )
-
The
on_reply
callback is called in both cases: when the unsubscription operation was completed successfully or when unsubscription operation fails. In first caseon_reply
callback is called on every specified pattern. Information about channel pattern and number of remaining subscriptions is passed to callback in first argument as an array reference.
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',
);
LUA SCRIPTS EXECUTION
Redis 2.6 and higher support execution of Lua scripts on the server side. To execute a Lua script you can send one of the commands EVAL
or EVALSHA
, or use the special method eval_cached()
.
eval_cached( $script, $numkeys [, @keys ] [, @args ] [, $cb | \%cbs ] );
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 E_NO_SCRIPT
error will be returned, the client send the EVAL
command.
If you call the eval_cached()
method with the same Lua script, client don not generate a SHA1 hash for this script repeatedly, it gets a hash from the cache instead.
$redis->eval_cached( 'return { KEYS[1], KEYS[2], ARGV[1], ARGV[2] }',
2, 'key1', 'key2', 'first', 'second',
sub {
my $reply = shift;
if (@_) {
my $err_msg = shift;
my $err_code = shift;
# error handling...
return;
}
foreach my $val ( @{$reply} ) {
print "$val\n";
}
}
);
Be care, passing a different Lua scripts to eval_cached()
method every time cause memory leaks.
If Lua script returns multi-bulk reply with at least one error reply, the on_error
or on_reply
callback is called and in addition to error message and error code to callback is passed reply data, which contain successful replies and error objects for each error reply, as well as described for EXEC
command.
$redis->eval_cached( "return { 'foo', redis.error_reply( 'Error.' ) }", 0,
sub {
my $reply = shift;
if ( @_ ) {
my $err_msg = shift;
my $err_code = shift;
if ( defined $reply ) {
foreach my $nested_reply ( @{$reply} ) {
if ( ref($nested_reply) eq 'AnyEvent::Redis::RipeRedis::Error' ) {
my $nested_err_msg = $nested_reply->message();
my $nested_err_code = $nested_reply->code();
# error handling...
}
}
}
# error handling...
return;
}
# reply handling...
}
);
$redis->eval_cached( "return { 'foo', redis.error_reply( 'Error.' ) }", 0,
{ on_done => sub {
my $reply = shift;
# handling...
},
on_error => sub {
my $err_msg = shift;
my $err_code = shift;
my $reply = shift;
if ( defined $reply ) {
foreach my $nested_reply ( @{$reply} ) {
if ( ref($nested_reply) eq 'AnyEvent::Redis::RipeRedis::Error' ) {
my $nested_err_msg = $nested_reply->message();
my $nested_err_code = $nested_reply->code();
# error handling...
}
}
}
# error handling...
}
}
);
SERVER INFORMATION AND STATISTICS
info( [ $section ] [, $cb | \%cbs ] )
Gets and parses information and statistics about the server. The result is passed to on_done
or on_reply
callback as a hash reference.
More information abount INFO
command can be found here: http://redis.io/commands/info
ERROR CODES
Every time when error occurred the error code is passed to on_error
or to on_reply
callback. Error codes can be used for programmatic handling of errors.
AnyEvent::Redis::RipeRedis provides constants of error codes, which can be imported and used in expressions.
use AnyEvent::Redis::RipeRedis qw( :err_codes );
- E_CANT_CONN
-
Can't connect to the server. All operations were aborted.
- E_LOADING_DATASET
-
Redis is loading the dataset in memory.
- E_IO
-
Input/Output operation error. The connection to the Redis server was closed and all operations were aborted.
- E_CONN_CLOSED_BY_REMOTE_HOST
-
The connection closed by remote host. All operations were aborted.
- E_CONN_CLOSED_BY_CLIENT
-
Connection closed by client prematurely. Uncompleted operations were aborted.
- E_NO_CONN
-
No connection to the Redis server. Connection was lost by any reason on previous operation.
- E_OPRN_ERROR
-
Operation error. For example, wrong number of arguments for a command.
- E_UNEXPECTED_DATA
-
The client received unexpected data from the server. The connection to the Redis server was closed and all operations were aborted.
- E_READ_TIMEDOUT
-
Read timed out. The connection to the Redis server was closed and all operations were aborted.
Error codes available since Redis 2.6.
- E_NO_SCRIPT
-
No matching script. Use the
EVAL
command. - E_BUSY
-
Redis is busy running a script. You can only call
SCRIPT KILL
orSHUTDOWN NOSAVE
. - E_MASTER_DOWN
-
Link with MASTER is down and slave-serve-stale-data is set to 'no'.
- E_MISCONF
-
Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.
- E_READONLY
-
You can't write against a read only slave.
- E_OOM
-
Command not allowed when used memory > 'maxmemory'.
- E_EXEC_ABORT
-
Transaction discarded because of previous errors.
Error codes available since Redis 2.8.
- E_NO_AUTH
-
Authentication required.
- E_WRONG_TYPE
-
Operation against a key holding the wrong kind of value.
- E_NO_REPLICAS
-
Not enough good slaves to write.
- E_BUSY_KEY
-
Target key name already exists.
Error codes available since Redis 3.0.
- E_CROSS_SLOT
-
Keys in request don't hash to the same slot.
- E_TRY_AGAIN
-
Multiple keys request during rehashing of slot.
- E_ASK
-
Redirection required. For more information see: http://redis.io/topics/cluster-spec
- E_MOVED
-
Redirection required. For more information see: http://redis.io/topics/cluster-spec
- E_CLUSTER_DOWN
-
The cluster is down or hash slot not served.
DISCONNECTION
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 without calling any callbacks, including the on_disconnect
callback, to avoid an unexpected behavior.
disconnect()
The method for synchronous disconnection. All uncompleted operations will be aborted.
$redis->disconnect();
quit()
The method for asynchronous disconnection.
$redis->quit(
sub {
# handling...
}
);
OTHER METHODS
connection_timeout( [ $fractional_seconds ] )
Get or set the connection_timeout
of the client. The undef
value resets the connection_timeout
to default value.
read_timeout( [ $fractional_seconds ] )
Get or set the read_timeout
of the client.
reconnect( [ $boolean ] )
Enables or disables reconnection mode of the client.
min_reconnect_interval( [ $fractional_seconds ] )
Get or set min_reconnect_interval
of the client.
encoding( [ $enc_name ] )
Get or set the current encoding
of the client.
on_connect( [ $callback ] )
Get or set the on_connect
callback.
on_disconnect( [ $callback ] )
Get or set the on_disconnect
callback.
on_connect_error( [ $callback ] )
Get or set the on_connect_error
callback.
on_error( [ $callback ] )
Get or set the on_error
callback.
selected_database()
Get currently selected database index.
KNOWN BUGS
Methods unsubscribe()
and punsubscribe()
without arguments don't works correctly. This issue fixed in AnyEvent::RipeRedis.
SEE ALSO
AnyEvent::RipeRedis, AnyEvent, Redis::hiredis, Redis, RedisDB
AUTHOR
Eugene Ponizovsky, <ponizovsky@gmail.com>
Special thanks
Alexey Shrub
Vadim Vlasov
Konstantin Uvarin
Ivan Kruglov
COPYRIGHT AND LICENSE
Copyright (c) 2012-2016, 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.