AnyEvent-RipeRedis-0.48/000755 000765 000024 00000000000 14052732530 015006 5ustar00iphstaff000000 000000 AnyEvent-RipeRedis-0.48/Changes000644 000765 000024 00000005100 14052731676 016307 0ustar00iphstaff000000 000000 Revision history for AnyEvent::RipeRedis. 0.48 Mon May 24 17:06:00 MSK 2021 - Fixed tests for a different error message from Redis (Thanks for Jan "Yenya" Kasprzak) 0.46 Tue Dec 5 15:56:58 MSK 2017 - The client now disconnects if the E_NO_AUTH error was returned. 0.44 Fri Dec 1 12:09:02 MSK 2017 - Made that the client do not reconnects on the error "Client sent AUTH, but no password is set". 0.42 Sat Apr 1 15:49:15 MSK 2017 - README.pod replaced by README.md. 0.40 Sat Feb 25 16:54:34 MSK 2017 - Fix in Changes file. 0.38 Sat Feb 25 16:38:55 MSK 2017 - Added error code E_NOT_BUSY. - Minor fixes. 0.36 Tue Feb 21 14:55:42 MSK 2017 - Added parsing of reply for "CLUSTER INFO" command. - Added README.pod file instead of README file. 0.34 Sat Jan 28 18:59:15 MSK 2017 - Light refactoring. 0.32 Thu Dec 29 15:16:05 MSK 2016 - Weakened one more circular reference. - Fixed typo in POD. 0.30 Sun Nov 20 19:40:55 MSK 2016 - Fixes in POD. - Fixes in tests. - Cosmetic changes in code. 0.28 Fri Sep 23 12:10:33 MSK 2016 - Bugfix. If the client object was destroyed prematurely, in some cases "_process_reply" method have been called on undefined value. 0.26 Fri Sep 2 18:17:17 MSK 2016 - "min_reconnect_interval" renamed to "reconnect_interval" 0.24 Fri Sep 2 08:20:05 MSK 2016 - Error "not allowed after "multi"" now raise exception. - Unit tests improved. 0.22 Tue Aug 30 19:51:25 MSK 2016 - Unit tests improved. 0.20 Tue Aug 30 19:20:30 MSK 2016 - Minor changes in POD. - Cosmetic changes in tests. 0.18 Tue Aug 30 11:03:25 MSK 2016 - Added link to AnyEvent::RipeRedis::Cluster in POD. 0.16 Tue Aug 30 11:00:00 MSK 2016 - %ERROR_CODES moved to AnyEvent::RipeRedis::Error. - Minor changes in unit tests. - Changes in POD. 0.14 Mon Aug 29 17:01:40 MSK 2016 - Removed unnecessary string in unit test. 0.12 Mon Aug 29 16:48:05 MSK 2016 - Refactoring of unit test. 0.10 Wed Aug 26 11:44:35 MSK 2016 - Removed "on_connect_error" callback. - POD Improved. - Unit tests improved. 0.08 Wed Aug 25 11:18:30 MSK 2016 - Added method "execute()". - Getter "selected_database()" renamed to "database()". - Bugfix. "DISCARD" command do not turned off transaction mode. - Refactoring. - POD Improved. 0.06 Fri Jul 29 10:51:10 MSK 2016 - Improved POD. - Light refactoring. 0.04 Mon Jul 27 11:41:15 MSK 2016 - Refactoring of eval_cached() method. - POD improved. - Bugfix in unit tests. - Removed unused code. Cosmetic. 0.02 Mon Jul 25 15:37:30 MSK 2016 - First release of new incarnation of AnyEvent::Redis::RipeRedis. AnyEvent-RipeRedis-0.48/MANIFEST000644 000765 000024 00000001157 14052732531 016144 0ustar00iphstaff000000 000000 Changes examples/eval.pl examples/generic.pl examples/subs.pl examples/unix_socket.pl lib/AnyEvent/RipeRedis.pm lib/AnyEvent/RipeRedis/Error.pm Makefile.PL MANIFEST This list of files README.md t/00-base.t t/01-error-codes.t t/02-accessors.t t/03-auth.t t/04-commands.t t/05-db-select.t t/06-subs.t t/07-lazy-conn.t t/08-eval.t t/09-conn-errors.t t/10-exceptions.t t/11-leaks.t t/12-pod.t t/13-pod-coverage.t t/test_helper.pl t/tlib/Test/RedisRunner.pm META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) AnyEvent-RipeRedis-0.48/t/000755 000765 000024 00000000000 14052732530 015251 5ustar00iphstaff000000 000000 AnyEvent-RipeRedis-0.48/README.md000644 000765 000024 00000050510 14052730013 016260 0ustar00iphstaff000000 000000 # NAME AnyEvent::RipeRedis - Flexible non-blocking Redis client # SYNOPSIS use AnyEvent; use AnyEvent::RipeRedis; my $redis = AnyEvent::RipeRedis->new( host => 'localhost', port => 6379, password => 'yourpass', ); my $cv = AE::cv; $redis->set( 'foo', 'bar', sub { my $err = $_[1]; if ( defined $err ) { warn $err->message . "\n"; $cv->send; return; } $redis->get( 'foo', sub { my $reply = shift; my $err = shift; if ( defined $err ) { warn $err->message . "\n"; $cv->send; return; } print "$reply\n"; $cv->send; } ); } ); $cv->recv; # DESCRIPTION AnyEvent::RipeRedis is flexible non-blocking Redis client. Supports subscriptions, transactions and can automaticaly restore connection after failure. Requires Redis 1.2 or higher, and any supported event loop. # CONSTRUCTOR ## new( %params ) my $redis = AnyEvent::RipeRedis->new( host => 'localhost', port => 6379, password => 'yourpass', database => 7, connection_timeout => 5, read_timeout => 5, lazy => 1, reconnect_interval => 5, on_connect => sub { # handling... }, on_disconnect => sub { # handling... }, on_error => sub { my $err = 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 switches 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 and switches to it automaticaly. The default database index is `0`. - utf8 => $boolean If enabled, all strings will be converted to UTF-8 before sending to the server, and all results will be decoded from UTF-8. Enabled by default. - connection\_timeout => $fractional\_seconds Specifies connection timeout. If the client could not connect to the server after specified timeout, the `on_error` callback is called with the `E_CANT_CONN` error. 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 the `E_READ_TIMEDOUT` error. 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 server was lost and the parameter `reconnect` is TRUE (default), the client will try to restore the connection when you execute next command. The client will try to reconnect only once and, if attempt fails, the error object is passed to command callback. If you need several attempts of the reconnection, you must retry a command from the callback as many times, as you need. Such behavior allows to control reconnection procedure. Enabled by default. - reconnect\_interval => $fractional\_seconds If the parameter is specified, the client will try to reconnect only after this interval. Commands executed between reconnections will be queued. reconnect_interval => 5, Not set by default. - handle\_params => \\%params Specifies [AnyEvent::Handle](https://metacpan.org/pod/AnyEvent::Handle) parameters. handle_params => { autocork => 1, linger => 60, } Enabling of the `autocork` parameter can improve performance. See documentation on [AnyEvent::Handle](https://metacpan.org/pod/AnyEvent::Handle) for more information. - 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\_error => $cb->( $err ) The `on_error` callback is called when occurred an error, which was affected on entire client (e. g. connection error or authentication error). Also the `on_error` callback is called on command errors if the command callback is not specified. If the `on_error` callback is not specified, the client just print an error messages to `STDERR`. # COMMAND EXECUTION ## <command>( \[ @args \] \[, $cb->( $reply, $err ) \] ) To execute the command you must call specific method with corresponding name. The reply to the command is passed to the callback in first argument. If any error occurred during the command execution, the error object is passed to the callback in second argument. Error object is the instance of the class [AnyEvent::RipeRedis::Error](https://metacpan.org/pod/AnyEvent::RipeRedis::Error). The command callback is optional. If it is not specified and any error occurred, the `on_error` callback of the client is called. The full list of the Redis commands can be found here: [http://redis.io/commands](http://redis.io/commands). $redis->get( 'foo', sub { my $reply = shift; my $err = shift; if ( defined $err ) { my $err_msg = $err->message; my $err_code = $err->code; # error handling... return; } print "$reply\n"; } ); $redis->lrange( 'list', 0, -1, sub { my $reply = shift; my $err = shift; if ( defined $err ) { my $err_msg = $err->message; my $err_code = $err->code; # error handling... return; } foreach my $value ( @{$reply} ) { print "$value\n"; } } ); $redis->incr( 'counter' ); You can execute multi-word commands like this: $redis->client_getname( sub { my $reply = shift; my $err = shift; if ( defined $err ) { my $err_msg = $err->message; my $err_code = $err->code; # error handling... return; } print "$reply\n"; } ); ## execute( $command, \[ @args \] \[, $cb->( $reply, $err ) \] ) An alternative method to execute commands. In some cases it can be more convenient. $redis->execute( 'get', 'foo', sub { my $reply = shift; my $err = shift; if ( defined $err ) { my $err_msg = $err->message; my $err_code = $err->code; return; } print "$reply\n"; } ); # TRANSACTIONS The detailed information about the Redis transactions can be found here: [http://redis.io/topics/transactions](http://redis.io/topics/transactions). ## multi( \[ $cb->( $reply, $err ) \] ) Marks the start of a transaction block. Subsequent commands will be queued for atomic execution using `EXEC`. ## exec( \[ $cb->( $reply, $err ) \] ) 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 during a transaction at least one command fails, to the callback will be passed error object, and the reply will be contain nested error objects for every failed command. $redis->multi(); $redis->set( 'foo', 'string' ); $redis->incr('foo'); # causes an error $redis->exec( sub { my $reply = shift; my $err = shift; if ( defined $err ) { my $err_msg = $err->message(); my $err_code = $err->code(); if ( defined $reply ) { foreach my $nested_reply ( @{$reply} ) { if ( ref($nested_reply) eq 'AnyEvent::RipeRedis::Error' ) { my $nested_err_msg = $nested_reply->message(); my $nested_err_code = $nested_reply->code(); # error handling... } } return; } # error handling... return; } # reply handling... }, ); ## discard( \[ $cb->( $reply, $err ) \] ) 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->( $reply, $err ) \] ) Marks the given keys to be watched for conditional execution of a transaction. ## unwatch( \[ $cb->( $reply, $err ) \] ) Forget about all watched keys. # SUBSCRIPTIONS 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. The detailed information about Redis Pub/Sub can be found here: [http://redis.io/topics/pubsub](http://redis.io/topics/pubsub) ## subscribe( @channels, ( $cb->( $msg, $channel ) | \\%cbs ) ) Subscribes the client to the specified channels. Method can accept two callbacks: `on_reply` and `on_message`. The `on_reply` callback is called when subscription to all specified channels will be activated. In first argument to the callback is passed the number of channels we are currently subscribed. If subscription to specified channels was lost, the `on_reply` callback is called with the error object in the second argument. The `on_message` callback is called on every published message. If the `subscribe` method is called with one callback, this callback will be act as `on_message` callback. $redis->subscribe( qw( foo bar ), { on_reply => sub { my $channels_num = shift; my $err = shift; if ( defined $err ) { # error handling... return; } # reply handling... }, on_message => sub { my $msg = shift; my $channel = shift; # message handling... }, } ); $redis->subscribe( qw( foo bar ), sub { my $msg = shift; my $channel = shift; # message handling... } ); ## psubscribe( @patterns, ( $cb->( $msg, $pattern, $channel ) | \\%cbs ) ) Subscribes the client to the given patterns. See `subscribe()` method for details. $redis->psubscribe( qw( foo_* bar_* ), { on_reply => sub { my $channels_num = shift; my $err = shift; if ( defined $err ) { # error handling... return; } # reply handling... }, on_message => sub { my $msg = shift; my $pattern = shift; my $channel = shift; # message handling... }, } ); $redis->psubscribe( qw( foo_* bar_* ), sub { my $msg = shift; my $pattern = shift; my $channel = shift; # message handling... } ); ## publish( $channel, $message \[, $cb->( $reply, $err ) \] ) Posts a message to the given channel. ## unsubscribe( \[ @channels \] \[, $cb->( $reply, $err ) \] ) Unsubscribes the client from the given channels, or from all of them if none is given. In first argument to the callback is passed the number of channels we are currently subscribed or zero if we were unsubscribed from all channels. $redis->unsubscribe( qw( foo bar ), sub { my $channels_num = shift; my $err = shift; if ( defined $err ) { # error handling... return; } # reply handling... } ); ## punsubscribe( \[ @patterns \] \[, $cb->( $reply, $err ) \] ) Unsubscribes the client from the given patterns, or from all of them if none is given. See `unsubscribe()` method for details. $redis->punsubscribe( qw( foo_* bar_* ), sub { my $channels_num = shift; my $err = shift; if ( defined $err ) { # error handling... return; } # reply handling... } ); # 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::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, $keys\_num \[, @keys \] \[, @args \] \[, $cb->( $reply, $err ) \] \] ); 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; my $err = shift; if ( defined $err ) { # error handling... return; } foreach my $value ( @{$reply} ) { print "$value\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, to the callback will be passed error object, and the reply will be contain nested error objects. $redis->eval_cached( "return { 'foo', redis.error_reply( 'Error.' ) }", 0, sub { my $reply = shift; my $err = shift; if ( defined $err ) { my $err_msg = $err->message; my $err_code = $err->code; if ( defined $reply ) { foreach my $nested_reply ( @{$reply} ) { if ( ref($nested_reply) eq 'AnyEvent::RipeRedis::Error' ) { my $nested_err_msg = $nested_reply->message(); my $nested_err_code = $nested_reply->code(); # error handling... } } } # error handling... return; } # reply handling... } ); # ERROR CODES Every error object, passed to callback, contain error code, which can be used for programmatic handling of errors. AnyEvent::RipeRedis provides constants for error codes. They can be imported and used in expressions. use AnyEvent::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 reply 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` or `SHUTDOWN NOSAVE`. - E\_NOT\_BUSY No scripts in execution right now. - 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](http://redis.io/topics/cluster-spec) - E\_MOVED Redirection required. For more information see: [http://redis.io/topics/cluster-spec](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::RipeRedis object, but in this case the 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. ## quit( \[ $cb->( $reply, $err ) \] ) The method for asynchronous disconnection. # OTHER METHODS ## info( \[ $section \] \[, $cb->( $reply, $err ) \] ) Gets and parses information and statistics about the server. The result is passed to callback as a hash reference. More information about `INFO` command can be found here: [http://redis.io/commands/info](http://redis.io/commands/info) ## host() Gets current host of the client. ## port() Gets current port of the client. ## select( $index, \[, $cb->( $reply, $err ) \] ) Selects the database by numeric index. ## database() Gets selected database index. ## utf8( \[ $boolean \] ) Enables or disables UTF-8 mode. ## connection\_timeout( \[ $fractional\_seconds \] ) Gets or sets the `connection_timeout` of the client. The `undef` value resets the `connection_timeout` to default value. ## read\_timeout( \[ $fractional\_seconds \] ) Gets or sets the `read_timeout` of the client. ## reconnect( \[ $boolean \] ) Enables or disables reconnection mode of the client. ## reconnect\_interval( \[ $fractional\_seconds \] ) Gets or sets `reconnect_interval` of the client. ## on\_connect( \[ $callback \] ) Gets or sets the `on_connect` callback. ## on\_disconnect( \[ $callback \] ) Gets or sets the `on_disconnect` callback. ## on\_error( \[ $callback \] ) Gets or sets the `on_error` callback. # SEE ALSO [AnyEvent::RipeRedis::Cluster](https://metacpan.org/pod/AnyEvent::RipeRedis::Cluster), [AnyEvent](https://metacpan.org/pod/AnyEvent), [Redis::hiredis](https://metacpan.org/pod/Redis::hiredis), [Redis](https://metacpan.org/pod/Redis), [RedisDB](https://metacpan.org/pod/RedisDB) # AUTHOR Eugene Ponizovsky, Sponsored by SMS Online, ## Special thanks - Alexey Shrub - Vadim Vlasov - Konstantin Uvarin - Ivan Kruglov # COPYRIGHT AND LICENSE Copyright (c) 2012-2017, Eugene Ponizovsky, SMS Online. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. AnyEvent-RipeRedis-0.48/examples/000755 000765 000024 00000000000 14052732530 016624 5ustar00iphstaff000000 000000 AnyEvent-RipeRedis-0.48/META.yml000664 000765 000024 00000001732 14052732530 016264 0ustar00iphstaff000000 000000 --- abstract: 'Flexible non-blocking Redis client' author: - 'Eugene Ponizovsky ' build_requires: File::Temp: '0.19' POSIX: '0' Test::Fatal: '0.013' Test::More: '0.98' Test::TCP: '2.12' Time::HiRes: '0' version: '0.77' configure_requires: ExtUtils::MakeMaker: '6.64' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 7.34, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: AnyEvent-RipeRedis no_index: directory: - t - inc requires: AnyEvent: '6.01' Carp: '0' Digest::SHA: '0' Scalar::Util: '0' perl: '5.008000' resources: bugtracker: https://github.com/iph0/AnyEvent-RipeRedis/issues homepage: https://github.com/iph0/AnyEvent-RipeRedis license: http://dev.perl.org/licenses/ repository: https://github.com/iph0/AnyEvent-RipeRedis version: '0.48' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' AnyEvent-RipeRedis-0.48/lib/000755 000765 000024 00000000000 14052732530 015554 5ustar00iphstaff000000 000000 AnyEvent-RipeRedis-0.48/Makefile.PL000644 000765 000024 00000002053 14052730013 016752 0ustar00iphstaff000000 000000 use 5.008000; use ExtUtils::MakeMaker; WriteMakefile( NAME => 'AnyEvent::RipeRedis', VERSION_FROM => 'lib/AnyEvent/RipeRedis.pm', MIN_PERL_VERSION => '5.008000', PREREQ_PM => { 'AnyEvent' => '6.01', 'Scalar::Util' => '0', 'Digest::SHA' => '0', 'Carp' => '0', }, CONFIGURE_REQUIRES => { 'ExtUtils::MakeMaker' => '6.64', }, BUILD_REQUIRES => { 'Test::More' => '0.98', 'Test::TCP' => '2.12', 'Test::Fatal' => '0.013', 'Time::HiRes' => '0', 'File::Temp' => '0.19', 'POSIX' => '0', version => '0.77', }, META_MERGE => { resources => { homepage => 'https://github.com/iph0/AnyEvent-RipeRedis', bugtracker => 'https://github.com/iph0/AnyEvent-RipeRedis/issues', repository => 'https://github.com/iph0/AnyEvent-RipeRedis', license => 'http://dev.perl.org/licenses/', }, }, ABSTRACT_FROM => 'lib/AnyEvent/RipeRedis.pm', AUTHOR => 'Eugene Ponizovsky ', LICENSE => 'perl', ); AnyEvent-RipeRedis-0.48/META.json000664 000765 000024 00000003132 14052732530 016430 0ustar00iphstaff000000 000000 { "abstract" : "Flexible non-blocking Redis client", "author" : [ "Eugene Ponizovsky " ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 7.34, CPAN::Meta::Converter version 2.150010", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "AnyEvent-RipeRedis", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "File::Temp" : "0.19", "POSIX" : "0", "Test::Fatal" : "0.013", "Test::More" : "0.98", "Test::TCP" : "2.12", "Time::HiRes" : "0", "version" : "0.77" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "6.64" } }, "runtime" : { "requires" : { "AnyEvent" : "6.01", "Carp" : "0", "Digest::SHA" : "0", "Scalar::Util" : "0", "perl" : "5.008000" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/iph0/AnyEvent-RipeRedis/issues" }, "homepage" : "https://github.com/iph0/AnyEvent-RipeRedis", "license" : [ "http://dev.perl.org/licenses/" ], "repository" : { "url" : "https://github.com/iph0/AnyEvent-RipeRedis" } }, "version" : "0.48", "x_serialization_backend" : "JSON::PP version 4.02" } AnyEvent-RipeRedis-0.48/lib/AnyEvent/000755 000765 000024 00000000000 14052732530 017305 5ustar00iphstaff000000 000000 AnyEvent-RipeRedis-0.48/lib/AnyEvent/RipeRedis.pm000644 000765 000024 00000117731 14052731424 021544 0ustar00iphstaff000000 000000 package AnyEvent::RipeRedis; use 5.008000; use strict; use warnings; use base qw( Exporter ); our $VERSION = '0.48'; use AnyEvent::RipeRedis::Error; use AnyEvent; use AnyEvent::Handle; use Scalar::Util qw( looks_like_number weaken ); use Digest::SHA qw( sha1_hex ); use Carp qw( croak ); my %ERROR_CODES; BEGIN { %ERROR_CODES = %AnyEvent::RipeRedis::Error::ERROR_CODES; our @EXPORT_OK = keys %ERROR_CODES; our %EXPORT_TAGS = ( err_codes => \@EXPORT_OK ); } use constant { # Default values D_HOST => 'localhost', D_PORT => 6379, D_DB_INDEX => 0, %ERROR_CODES, # Operation status S_NEED_DO => 1, S_IN_PROGRESS => 2, S_DONE => 3, # String terminator EOL => "\r\n", EOL_LENGTH => 2, }; my %SUB_CMDS = ( subscribe => 1, psubscribe => 1, ); my %SUBUNSUB_CMDS = ( %SUB_CMDS, unsubscribe => 1, punsubscribe => 1, ); my %MESSAGE_TYPES = ( message => 1, pmessage => 1, ); my %NEED_PREPROCESS = ( multi => 1, exec => 1, discard => 1, eval_cached => 1, %SUBUNSUB_CMDS, ); my %NEED_POSTPROCESS = ( info => 1, cluster_info => 1, select => 1, quit => 1, ); my %ERR_PREFS_MAP = ( LOADING => E_LOADING_DATASET, NOSCRIPT => E_NO_SCRIPT, BUSY => E_BUSY, MASTERDOWN => E_MASTER_DOWN, MISCONF => E_MISCONF, READONLY => E_READONLY, OOM => E_OOM, EXECABORT => E_EXEC_ABORT, NOAUTH => E_NO_AUTH, WRONGTYPE => E_WRONG_TYPE, NOREPLICAS => E_NO_REPLICAS, BUSYKEY => E_BUSY_KEY, CROSSSLOT => E_CROSS_SLOT, TRYAGAIN => E_TRY_AGAIN, ASK => E_ASK, MOVED => E_MOVED, CLUSTERDOWN => E_CLUSTER_DOWN, NOTBUSY => E_NOT_BUSY, ); my %EVAL_CACHE; sub new { my $class = shift; my %params = @_; my $self = bless {}, $class; $self->{host} = $params{host} || D_HOST; $self->{port} = $params{port} || D_PORT; $self->{password} = $params{password}; $self->{database} = defined $params{database} ? $params{database} : D_DB_INDEX; $self->{utf8} = exists $params{utf8} ? $params{utf8} : 1; $self->{lazy} = $params{lazy}; $self->{reconnect} = exists $params{reconnect} ? $params{reconnect} : 1; $self->{handle_params} = $params{handle_params} || {}; $self->{on_connect} = $params{on_connect}; $self->{on_disconnect} = $params{on_disconnect}; $self->connection_timeout( $params{connection_timeout} ); $self->read_timeout( $params{read_timeout} ); $self->reconnect_interval( $params{reconnect_interval} ); $self->on_error( $params{on_error} ); $self->_reset_internals; $self->{_input_queue} = []; $self->{_temp_queue} = []; $self->{_processing_queue} = []; $self->{_channels} = {}; $self->{_channel_cnt} = 0; $self->{_pchannel_cnt} = 0; unless ( $self->{lazy} ) { $self->_connect; } return $self; } sub execute { my $self = shift; my $cmd_name = shift; my $cmd = $self->_prepare( $cmd_name, [@_] ); $self->_execute($cmd); return; } sub disconnect { my $self = shift; $self->_disconnect; return; } sub on_error { my $self = shift; if (@_) { my $on_error = shift; if ( defined $on_error ) { $self->{on_error} = $on_error; } else { $self->{on_error} = sub { my $err = shift; warn $err->message . "\n"; }; } } return $self->{on_error}; } # Generate accessors { no strict qw( refs ); foreach my $name ( qw( host port database ) ) { *{$name} = sub { my $self = shift; return $self->{$name}; } } foreach my $name ( qw( connection_timeout read_timeout reconnect_interval ) ) { *{$name} = sub { my $self = shift; if (@_) { my $seconds = shift; if ( defined $seconds && ( !looks_like_number($seconds) || $seconds < 0 ) ) { croak qq{"$name" must be a positive number}; } $self->{$name} = $seconds; } return $self->{$name}; }; } foreach my $name ( qw( utf8 reconnect on_connect on_disconnect ) ) { *{$name} = sub { my $self = shift; if (@_) { $self->{$name} = shift; } return $self->{$name}; }; } } sub _connect { my $self = shift; $self->{_handle} = AnyEvent::Handle->new( %{ $self->{handle_params} }, connect => [ $self->{host}, $self->{port} ], on_prepare => $self->_create_on_prepare, on_connect => $self->_create_on_connect, on_connect_error => $self->_create_on_connect_error, on_rtimeout => $self->_create_on_rtimeout, on_eof => $self->_create_on_eof, on_error => $self->_create_on_handle_error, on_read => $self->_create_on_read, ); return; } sub _create_on_prepare { my $self = shift; weaken($self); return sub { if ( defined $self->{connection_timeout} ) { return $self->{connection_timeout}; } return; }; } sub _create_on_connect { my $self = shift; weaken($self); return sub { $self->{_connected} = 1; unless ( defined $self->{password} ) { $self->{_auth_state} = S_DONE; } if ( $self->{database} == 0 ) { $self->{_db_selection_state} = S_DONE; } if ( $self->{_auth_state} == S_NEED_DO ) { $self->_auth; } elsif ( $self->{_db_selection_state} == S_NEED_DO ) { $self->_select_database; } else { $self->{_ready} = 1; $self->_process_input_queue; } if ( defined $self->{on_connect} ) { $self->{on_connect}->(); } }; } sub _create_on_connect_error { my $self = shift; weaken($self); return sub { my $err_msg = pop; my $err = _new_error( "Can't connect to $self->{host}:$self->{port}: $err_msg", E_CANT_CONN ); $self->_disconnect($err); }; } sub _create_on_rtimeout { my $self = shift; weaken($self); return sub { if ( @{ $self->{_processing_queue} } ) { my $err = _new_error( 'Read timed out.', E_READ_TIMEDOUT ); $self->_disconnect($err); } else { $self->{_handle}->rtimeout(undef); } }; } sub _create_on_eof { my $self = shift; weaken($self); return sub { my $err = _new_error( 'Connection closed by remote host.', E_CONN_CLOSED_BY_REMOTE_HOST ); $self->_disconnect($err); }; } sub _create_on_handle_error { my $self = shift; weaken($self); return sub { my $err_msg = pop; my $err = _new_error( $err_msg, E_IO ); $self->_disconnect($err); }; } sub _create_on_read { my $self = shift; weaken($self); my $str_len; my @bufs; my $bufs_num = 0; return sub { my $handle = shift; MAIN: while (1) { return if $handle->destroyed; my $reply; my $err_code; if ( defined $str_len ) { if ( length( $handle->{rbuf} ) < $str_len + EOL_LENGTH ) { return; } $reply = substr( $handle->{rbuf}, 0, $str_len, '' ); substr( $handle->{rbuf}, 0, EOL_LENGTH, '' ); if ( $self->{utf8} ) { utf8::decode($reply); } undef $str_len; } else { my $eol_pos = index( $handle->{rbuf}, EOL ); if ( $eol_pos < 0 ) { return; } $reply = substr( $handle->{rbuf}, 0, $eol_pos, '' ); my $type = substr( $reply, 0, 1, '' ); substr( $handle->{rbuf}, 0, EOL_LENGTH, '' ); if ( $type ne '+' && $type ne ':' ) { if ( $type eq '$' ) { if ( $reply >= 0 ) { $str_len = $reply; next; } undef $reply; } elsif ( $type eq '*' ) { if ( $reply > 0 ) { push( @bufs, { reply => [], err_code => undef, chunks_cnt => $reply, } ); $bufs_num++; next; } elsif ( $reply == 0 ) { $reply = []; } else { undef $reply; } } elsif ( $type eq '-' ) { $err_code = E_OPRN_ERROR; if ( $reply =~ m/^([A-Z]{3,}) / ) { if ( exists $ERR_PREFS_MAP{$1} ) { $err_code = $ERR_PREFS_MAP{$1}; } } } else { my $err = _new_error( 'Unexpected reply received.', E_UNEXPECTED_DATA ); $self->_disconnect($err); return; } } } while ( $bufs_num > 0 ) { my $curr_buf = $bufs[-1]; if ( defined $err_code ) { unless ( ref($reply) ) { $reply = _new_error( $reply, $err_code ); } $curr_buf->{err_code} = E_OPRN_ERROR; } push( @{ $curr_buf->{reply} }, $reply ); if ( --$curr_buf->{chunks_cnt} > 0 ) { next MAIN; } $reply = $curr_buf->{reply}; $err_code = $curr_buf->{err_code}; pop @bufs; $bufs_num--; } $self->_process_reply( $reply, $err_code ); } return; }; } sub _prepare { my $self = shift; my $cmd_name = shift; my $args = shift; my $cbs; if ( ref( $args->[-1] ) eq 'HASH' ) { $cbs = pop @{$args}; } else { $cbs = {}; if ( ref( $args->[-1] ) eq 'CODE' ) { if ( exists $SUB_CMDS{$cmd_name} ) { $cbs->{on_message} = pop @{$args}; } else { $cbs->{on_reply} = pop @{$args}; } } } my @kwds = $cmd_name eq 'eval_cached' ? ('evalsha') : split( m/_/, lc($cmd_name) ); my $cmd = { name => $cmd_name, kwds => \@kwds, args => $args, %{$cbs}, }; unless ( defined $cmd->{on_reply} ) { weaken($self); $cmd->{on_reply} = sub { my $err = $_[1]; if ( defined $err ) { $self->{on_error}->($err); return; } }; } return $cmd; } sub _execute { my $self = shift; my $cmd = shift; if ( $self->{_multi_mode} && ( exists $SUBUNSUB_CMDS{ $cmd->{name} } || exists $NEED_POSTPROCESS{ $cmd->{name} } ) ) { croak qq{Command "$cmd->{name}" not allowed after "multi" command.} . ' First, the transaction must be finalized.'; } if ( exists $NEED_PREPROCESS{ $cmd->{name} } ) { if ( $cmd->{name} eq 'multi' ) { $self->{_multi_mode} = 1; } elsif ( $cmd->{name} eq 'exec' || $cmd->{name} eq 'discard' ) { $self->{_multi_mode} = 0; } elsif ( $cmd->{name} eq 'eval_cached' ) { my $script = $cmd->{args}[0]; unless ( exists $EVAL_CACHE{$script} ) { $EVAL_CACHE{$script} = sha1_hex($script); } $cmd->{args}[0] = $EVAL_CACHE{$script}; $cmd->{script} = $script; } else { # subscribe, unsubscribe, psubscribe, punsubscribe if ( exists $SUB_CMDS{ $cmd->{name} } && !defined $cmd->{on_message} ) { croak '"on_message" callback must be specified'; } if ( @{ $cmd->{args} } ) { $cmd->{reply_cnt} = scalar @{ $cmd->{args} }; } } } unless ( $self->{_ready} ) { if ( defined $self->{_handle} ) { if ( $self->{_connected} ) { if ( $self->{_auth_state} == S_DONE ) { if ( $self->{_db_selection_state} == S_NEED_DO ) { $self->_select_database; } } elsif ( $self->{_auth_state} == S_NEED_DO ) { $self->_auth; } } } elsif ( $self->{lazy} ) { undef $self->{lazy}; $self->_connect; } elsif ( $self->{reconnect} ) { if ( defined $self->{reconnect_interval} && $self->{reconnect_interval} > 0 ) { unless ( defined $self->{_reconnect_timer} ) { weaken($self); $self->{_reconnect_timer} = AE::timer( $self->{reconnect_interval}, 0, sub { undef $self->{_reconnect_timer}; $self->_connect; } ); } } else { $self->_connect; } } else { AE::postpone { my $err = _new_error( qq{Operation "$cmd->{name}" aborted:} . ' No connection to the server.', E_NO_CONN ); $cmd->{on_reply}->( undef, $err ); }; return; } push( @{ $self->{_input_queue} }, $cmd ); return; } $self->_push_write($cmd); return; } sub _push_write { my $self = shift; my $cmd = shift; my $cmd_str = ''; my @tokens = ( @{ $cmd->{kwds} }, @{ $cmd->{args} } ); foreach my $token (@tokens) { unless ( defined $token ) { $token = ''; } elsif ( $self->{utf8} ) { utf8::encode($token); } $cmd_str .= '$' . length($token) . EOL . $token . EOL; } $cmd_str = '*' . scalar(@tokens) . EOL . $cmd_str; my $handle = $self->{_handle}; if ( defined $self->{read_timeout} && !@{ $self->{_processing_queue} } ) { $handle->rtimeout_reset; $handle->rtimeout( $self->{read_timeout} ); } push( @{ $self->{_processing_queue} }, $cmd ); $handle->push_write($cmd_str); return; } sub _auth { my $self = shift; weaken($self); $self->{_auth_state} = S_IN_PROGRESS; $self->_push_write( { name => 'auth', kwds => ['auth'], args => [ $self->{password} ], on_reply => sub { my $err = $_[1]; if ( defined $err && $err->message ne 'ERR Client sent AUTH, but no password is set' ) { $self->{_auth_state} = S_NEED_DO; $self->_abort($err); return; } $self->{_auth_state} = S_DONE; if ( $self->{_db_selection_state} == S_NEED_DO ) { $self->_select_database; } else { $self->{_ready} = 1; $self->_process_input_queue; } }, } ); return; } sub _select_database { my $self = shift; weaken($self); $self->{_db_selection_state} = S_IN_PROGRESS; $self->_push_write( { name => 'select', kwds => ['select'], args => [ $self->{database} ], on_reply => sub { my $err = $_[1]; if ( defined $err ) { $self->{_db_selection_state} = S_NEED_DO; $self->_abort($err); return; } $self->{_db_selection_state} = S_DONE; $self->{_ready} = 1; $self->_process_input_queue; }, } ); return; } sub _process_input_queue { my $self = shift; $self->{_temp_queue} = $self->{_input_queue}; $self->{_input_queue} = []; while ( my $cmd = shift @{ $self->{_temp_queue} } ) { $self->_push_write($cmd); } return; } sub _process_reply { my $self = shift; my $reply = shift; my $err_code = shift; if ( defined $err_code ) { $self->_process_error( $reply, $err_code ); } elsif ( $self->{_channel_cnt} + $self->{_pchannel_cnt} > 0 && ref($reply) && exists $MESSAGE_TYPES{ $reply->[0] } ) { $self->_process_message($reply); } else { $self->_process_success($reply); } return; } sub _process_error { my $self = shift; my $reply = shift; my $err_code = shift; my $cmd = shift @{ $self->{_processing_queue} }; unless ( defined $cmd ) { my $err = _new_error( q{Don't know how process error message. Processing queue is empty.}, E_UNEXPECTED_DATA ); $self->_disconnect($err); return; } if ( $err_code == E_NO_AUTH ) { my $err = _new_error( $reply, $err_code ); $self->_disconnect($err); return; } if ( $cmd->{name} eq 'eval_cached' && $err_code == E_NO_SCRIPT ) { $cmd->{kwds}[0] = 'eval'; $cmd->{args}[0] = $cmd->{script}; $self->_push_write($cmd); return; } if ( ref($reply) ) { my $err = _new_error( qq{Operation "$cmd->{name}" completed with errors.}, $err_code ); $cmd->{on_reply}->( $reply, $err ); } else { my $err = _new_error( $reply, $err_code ); $cmd->{on_reply}->( undef, $err ); } return; } sub _process_message { my $self = shift; my $msg = shift; my $cmd = $self->{_channels}{ $msg->[1] }; unless ( defined $cmd ) { my $err = _new_error( q{Don't know how process published message.} . qq{ Unknown channel or pattern "$msg->[1]".}, E_UNEXPECTED_DATA ); $self->_disconnect($err); return; } $cmd->{on_message}->( $msg->[0] eq 'pmessage' ? @{$msg}[ 3, 1, 2 ] : @{$msg}[ 2, 1 ] ); return; } sub _process_success { my $self = shift; my $reply = shift; my $cmd = $self->{_processing_queue}[0]; unless ( defined $cmd ) { my $err = _new_error( q{Don't know how process reply. Processing queue is empty.}, E_UNEXPECTED_DATA ); $self->_disconnect($err); return; } if ( exists $SUBUNSUB_CMDS{ $cmd->{name} } ) { if ( $cmd->{name} eq 'subscribe' ) { $self->{_channels}{ $reply->[1] } = $cmd; $self->{_channel_cnt}++; } elsif ( $cmd->{name} eq 'psubscribe' ) { $self->{_channels}{ $reply->[1] } = $cmd; $self->{_pchannel_cnt}++; } elsif ( $cmd->{name} eq 'unsubscribe' ) { unless ( defined $cmd->{reply_cnt} ) { $cmd->{reply_cnt} = $self->{_channel_cnt}; } delete $self->{_channels}{ $reply->[1] }; $self->{_channel_cnt}--; } else { # punsubscribe unless ( defined $cmd->{reply_cnt} ) { $cmd->{reply_cnt} = $self->{_pchannel_cnt}; } delete $self->{_channels}{ $reply->[1] }; $self->{_pchannel_cnt}--; } $reply = $reply->[2]; } if ( !defined $cmd->{reply_cnt} || --$cmd->{reply_cnt} == 0 ) { shift @{ $self->{_processing_queue} }; if ( exists $NEED_POSTPROCESS{ $cmd->{name} } ) { if ( $cmd->{name} eq 'info' || $cmd->{name} eq 'cluster_info' ) { $reply = _parse_info($reply); } elsif ( $cmd->{name} eq 'select' ) { $self->{database} = $cmd->{args}[0]; } else { # quit $self->_disconnect; } } $cmd->{on_reply}->($reply); } return; } sub _parse_info { return { map { split( m/:/, $_, 2 ) } grep { m/^[^#]/ } split( EOL, $_[0] ) }; } sub _disconnect { my $self = shift; my $err = shift; my $was_connected = $self->{_connected}; if ( defined $self->{_handle} ) { $self->{_handle}->destroy; } $self->_reset_internals; $self->_abort($err); if ( $was_connected && defined $self->{on_disconnect} ) { $self->{on_disconnect}->(); } return; } sub _reset_internals { my $self = shift; $self->{_handle} = undef; $self->{_connected} = 0; $self->{_auth_state} = S_NEED_DO; $self->{_db_selection_state} = S_NEED_DO; $self->{_ready} = 0; $self->{_multi_mode} = 0; $self->{_reconnect_timer} = undef; return; } sub _abort { my $self = shift; my $err = shift; my @queued_commands = $self->_queued_commands; my %channels = %{ $self->{_channels} }; $self->{_input_queue} = []; $self->{_temp_queue} = []; $self->{_processing_queue} = []; $self->{_channels} = {}; $self->{_channel_cnt} = 0; $self->{_pchannel_cnt} = 0; if ( !defined $err && @queued_commands ) { $err = _new_error( 'Connection closed by client prematurely.', E_CONN_CLOSED_BY_CLIENT ); } if ( defined $err ) { my $err_msg = $err->message; my $err_code = $err->code; $self->{on_error}->($err); if ( %channels && $err_code != E_CONN_CLOSED_BY_CLIENT ) { foreach my $name ( keys %channels ) { my $err = _new_error( qq{Subscription to channel "$name" lost: $err_msg}, $err_code ); my $cmd = $channels{$name}; $cmd->{on_reply}->( undef, $err ); } } foreach my $cmd (@queued_commands) { my $err = _new_error( qq{Operation "$cmd->{name}" aborted: $err_msg}, $err_code ); $cmd->{on_reply}->( undef, $err ); } } return; } sub _queued_commands { my $self = shift; return ( @{ $self->{_processing_queue} }, @{ $self->{_temp_queue} }, @{ $self->{_input_queue} }, ); } sub _new_error { return AnyEvent::RipeRedis::Error->new(@_); } sub AUTOLOAD { our $AUTOLOAD; my $cmd_name = $AUTOLOAD; $cmd_name =~ s/^.+:://; my $sub = sub { my $self = shift; my $cmd = $self->_prepare( $cmd_name, [@_] ); $self->_execute($cmd); return; }; do { no strict 'refs'; *{$cmd_name} = $sub; }; goto &{$sub}; } sub DESTROY { my $self = shift; if ( defined $self->{_handle} ) { $self->{_handle}->destroy; } if ( defined $self->{_processing_queue} ) { my @queued_commands = $self->_queued_commands; foreach my $cmd (@queued_commands) { warn qq{Operation "$cmd->{name}" aborted:} . " Client object destroyed prematurely.\n"; } } return; } 1; __END__ =head1 NAME AnyEvent::RipeRedis - Flexible non-blocking Redis client =head1 SYNOPSIS use AnyEvent; use AnyEvent::RipeRedis; my $redis = AnyEvent::RipeRedis->new( host => 'localhost', port => 6379, password => 'yourpass', ); my $cv = AE::cv; $redis->set( 'foo', 'bar', sub { my $err = $_[1]; if ( defined $err ) { warn $err->message . "\n"; $cv->send; return; } $redis->get( 'foo', sub { my $reply = shift; my $err = shift; if ( defined $err ) { warn $err->message . "\n"; $cv->send; return; } print "$reply\n"; $cv->send; } ); } ); $cv->recv; =head1 DESCRIPTION AnyEvent::RipeRedis is flexible non-blocking Redis client. Supports subscriptions, transactions and can automaticaly restore connection after failure. Requires Redis 1.2 or higher, and any supported event loop. =head1 CONSTRUCTOR =head2 new( %params ) my $redis = AnyEvent::RipeRedis->new( host => 'localhost', port => 6379, password => 'yourpass', database => 7, connection_timeout => 5, read_timeout => 5, lazy => 1, reconnect_interval => 5, on_connect => sub { # handling... }, on_disconnect => sub { # handling... }, on_error => sub { my $err = shift; # error handling... }, ); =over =item host => $host Server hostname (default: 127.0.0.1) =item port => $port Server port (default: 6379) =item password => $password If the password is specified, the C command is sent to the server after connection. =item database => $index Database index. If the index is specified, the client switches to the specified database after connection. You can also switch to another database after connection by using C