AnyEvent-RipeRedis

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN


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.

README.md  view on Meta::CPAN

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";

README.md  view on Meta::CPAN


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...
      },

README.md  view on Meta::CPAN

    );

- 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`.

examples/eval.pl  view on Meta::CPAN

use warnings;

use AnyEvent;
use AnyEvent::RipeRedis;

my $cv = AE::cv;

my $redis = AnyEvent::RipeRedis->new(
  host     => 'localhost',
  port     => 6379,
  password => 'redis_pass',

  on_connect => sub {
    print "Connected to Redis server\n";
  },

  on_disconnect => sub {
    print "Disconnected from Redis server\n";
  },
);

examples/generic.pl  view on Meta::CPAN

use warnings;

use AnyEvent;
use AnyEvent::RipeRedis;

my $cv = AE::cv;

my $redis = AnyEvent::RipeRedis->new(
  host     => 'localhost',
  port     => 6379,
  password => 'redis_pass',

  on_connect => sub {
    print "Connected to Redis server\n";
  },

  on_disconnect => sub {
    print "Disconnected from Redis server\n";
  },
);

examples/subs.pl  view on Meta::CPAN

use warnings;

use AnyEvent;
use AnyEvent::RipeRedis;

my $cv = AE::cv;

my $redis = AnyEvent::RipeRedis->new(
  host     => 'localhost',
  port     => 6379,
  password => 'redis_pass',

  on_connect => sub {
    print "Connected to Redis server\n";
  },

  on_disconnect => sub {
    print "Disconnected from Redis server\n";
  },
);

examples/unix_socket.pl  view on Meta::CPAN


use AnyEvent;
use AnyEvent::RipeRedis;

my $cv = AE::cv;

my $redis;
$redis = AnyEvent::RipeRedis->new(
  host               => 'unix/',
  port               => '/var/run/redis/redis.sock',
  password           => 'redis_pass',
  connection_timeout => 5,
  read_timeout       => 5,
  reconnect_interval => 5,

  on_connect => sub {
    print "Connected to Redis server\n";
  },

  on_disconnect => sub {
    print "Disconnected from Redis server\n";

lib/AnyEvent/RipeRedis.pm  view on Meta::CPAN



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} );

lib/AnyEvent/RipeRedis.pm  view on Meta::CPAN

}

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 ) {

lib/AnyEvent/RipeRedis.pm  view on Meta::CPAN


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 ) {

lib/AnyEvent/RipeRedis.pm  view on Meta::CPAN

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";

lib/AnyEvent/RipeRedis.pm  view on Meta::CPAN


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...
    },

lib/AnyEvent/RipeRedis.pm  view on Meta::CPAN

=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<AUTH> 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<SELECT> command. The client remembers last selected
database after reconnection and switches to it automaticaly.

The default database index is C<0>.

t/02-accessors.t  view on Meta::CPAN

use 5.008000;
use strict;
use warnings;

use Test::More tests => 38;
use AnyEvent::RipeRedis qw( :err_codes );
use AnyEvent::RipeRedis::Error;

my $redis = AnyEvent::RipeRedis->new(
  password           => 'test',
  connection_timeout => 10,
  read_timeout       => 5,
  reconnect          => 1,
  reconnect_interval => 5,

  on_connect => sub {
    return 1;
  },

  on_disconnect => sub {

t/03-auth.t  view on Meta::CPAN


my $server_info = run_redis_instance(
  requirepass => 'testpass',
);
if ( !defined $server_info ) {
  plan skip_all => 'redis-server is required for this test';
}
plan tests => 8;

t_successful_auth($server_info);
t_invalid_password($server_info);


sub t_successful_auth {
  my $server_info = shift;

  my $redis = AnyEvent::RipeRedis->new(
    host     => $server_info->{host},
    port     => $server_info->{port},
    password => $server_info->{password},
  );

  can_ok( $redis, 'disconnect' );

  my $t_reply;

  ev_loop(
    sub {
      my $cv = shift;

t/03-auth.t  view on Meta::CPAN

        }
      );
    }
  );

  $redis->disconnect;

  is( $t_reply, 'PONG', 'successful AUTH' );
}

sub t_invalid_password {
  my $server_info = shift;

  my $redis;

  my $t_cli_err;
  my $t_cmd_err;

  ev_loop(
    sub {
      my $cv = shift;

      $redis = AnyEvent::RipeRedis->new(
        host     => $server_info->{host},
        port     => $server_info->{port},
        password => 'invalid',

        on_error => sub {
          $t_cli_err = shift;
          $cv->send;
        },
      );

      $redis->ping(
        sub {
          my $reply  = shift;
          $t_cmd_err = shift;
        }
      );
    }
  );

  $redis->disconnect;

  my $t_name_prefix = 'invalid password';
  isa_ok( $t_cmd_err, 'AnyEvent::RipeRedis::Error' );
  like( $t_cmd_err->message, qr/^Operation "ping" aborted:/,
      "$t_name_prefix; command error message" );
  is( $t_cmd_err->code, E_OPRN_ERROR, "$t_name_prefix; command error code" );
  isa_ok( $t_cli_err, 'AnyEvent::RipeRedis::Error' );
  like( $t_cli_err->message, qr/^(?:ERR invalid password|WRONGPASS )/,
      "$t_name_prefix; client error message" );
  is( $t_cli_err->code, E_OPRN_ERROR, "$t_name_prefix; client error code" );

  return;
}

t/05-db-select.t  view on Meta::CPAN


  return;
}

sub t_auto_select_after_auth {
  my $server_info = shift;

  my $redis_db1 = AnyEvent::RipeRedis->new(
    host     => $server_info->{host},
    port     => $server_info->{port},
    password => $server_info->{password},
    database => 1,
  );
  my $redis_db2 = AnyEvent::RipeRedis->new(
    host     => $server_info->{host},
    port     => $server_info->{port},
    password => $server_info->{password},
    database => 2,
  );

  ev_loop(
    sub {
      my $cv = shift;

      my $reply_cnt = 0;

      my $on_reply = sub {

t/test_helper.pl  view on Meta::CPAN

  if ( !defined $redis_server ) {
    return;
  }

  my %conn_info = $redis_server->connect_info;

  return {
    server   => $redis_server,
    host     => $conn_info{host},
    port     => $conn_info{port},
    password => $params{requirepass},
  };
}

sub ev_loop {
  my $sub = shift;

  my $cv = AE::cv;

  $sub->($cv);



( run in 1.388 second using v1.01-cache-2.11-cpan-49f99fa48dc )