und3f / mojox-redis

asynchronous Redis client based on Mojo
https://metacpan.org/pod/MojoX::Redis
Artistic License 2.0
10 stars 2 forks source link

=head1 NAME

MojoX::Redis - asynchronous Redis client for L.

=head1 SYNOPSIS

use MojoX::Redis;

my $redis = MojoX::Redis->new(server => '127.0.0.1:6379');

# Execute some commands
$redis->ping(
    sub {
        my ($redis, $res) = @_;

        if ($res) {
            print "Got result: ", $res->[0], "\n";
        }
        else {
            print "Error: ", $redis->error, "\n";
        }
    }
);

# Work with keys
$redis->set(key => 'value');

$redis->get(
    key => sub {
        my ($redis, $res) = @_;

        print "Value of 'key' is $res->[0]\n";
    }
);

# Cleanup connection
$redis->quit(sub { shift->stop });

# Start IOLoop (in case it is not started yet)
$redis->start;

Create new Mojo::IOLoop instance if you need to get blocked in a Mojolicious application.

use Mojolicious::Lite;
use MojoX::Redis;

get '/' => sub {
    my $self = shift;

    my $redis = MojoX::Redis->new(ioloop => Mojo::IOLoop->new);

    my $value;

    $redis->set(foo => 'bar')->get(
        foo => sub {
            my ($redis, $result) = @_;

            $redis->quit->stop;

            return app->log->error($redis->error) unless $result;

            $value = $result->[0];
        }
    )->start;

    $self->render(text => qq(Foo value is "$value"));
};

app->start;

=head1 DESCRIPTION

L is an asynchronous client to Redis for Mojo.

=head1 ATTRIBUTES

L implements the following attributes.

=head2 C

my $server = $redis->server;
$redis     = $redis->server('127.0.0.1:6379');

C server connection string, defaults to '127.0.0.1:6379'.

=head2 C

my $ioloop = $redis->ioloop;
$redis     = $redis->ioloop(Mojo::IOLoop->new);

Loop object to use for io operations, by default a L singleton object will be used.

=head2 C

my $timeout = $redis->timeout;
$redis      = $redis->timeout(100);

Maximum amount of time in seconds a connection can be inactive before being dropped, defaults to C<300>.

=head2 C

my $encoding = $redis->encoding;
$redis       = $redis->encoding('UTF-8');

Encoding used for stored data, defaults to C.

=head2 C

use Protocol::Redis::XS;
$redis->protocol_redis("Protocol::Redis::XS");

L implementation' constructor for parsing. By default L will be used. Parser library must support L<APIv1|Protocol::Redis/APIv1>.

Using L instead of default choice can speedup parsing.

=head1 METHODS

L supports Redis' methods.

$redis->set(key => 'value);
$redis->get(key => sub { ... });

For more details take a look at C method.

Also L implements the following ones.

=head2 C

$redis = $redis->connect;

Connect to C server.

=head2 C

$redis = $redis->execute("ping" => sub{
    my ($redis, $result) = @_;

    # Process result
});
$redis->execute(lrange => ["test", 0, -1] => sub {...});
$redis->execute(set => [test => "test_ok"]);

Execute specified command on C server. If error occured during request $result will be set to undef, error string can be obtained with $redis->error.

=head2 C

$redis->execute("ping" => sub {
    my ($redis, $result) = @_;
    die $redis->error unless defined $result;
});

Returns error occured during command execution. Note that this method returns error code just from current command and can be used just in callback.

=head2 C

$redis->on_error(sub{
    my $redis = shift;
    warn 'Redis error ', $redis->error, "\n";
});

Executes if error occured. Called before commands callbacks.

=head2 C

$redis->start;

Starts IOLoop. Shortcut for $redis->ioloop->start;

=head1 SEE ALSO

L, L

=head1 AUTHOR

Sergey Zasenko, Cundef@cpan.org.

=head1 CREDITS

Marcus Ramberg (marcusramberg)

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2010-2015, Sergey Zasenko

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.