AnyEvent::Redis2 - an event-driven Redis client
use AnyEvent::Redis2; my $redis = AnyEvent::Redis2->connect( host => 'redis', on_connect => $cb, on_connect_error => $errcb, on_error => $errcb, ); # Generic query method $redis->query('SET', $key, $value, $cb); # Autoloaded query method $redis->set($key, $value, $cb);
This module is an AnyEvent user; you must use and run a supported event loop.
AnyEvent::Redis2 is an event-driven (asynchronous) client for the Redis key-value (NoSQL) database server. Every operation is supported, except subscription to Redis "classes" (i.e. channels), which is supported by AnyEvent::Redis::Subscriber. However, this module may be used to publish to channels.
To connect to the Redis server, use the connect() method:
my $redis = AnyEvent::Redis2->connect(host => <host>, ...);
The host argument is required.
host
Optional (but recommended) arguments include:
Connect to the server on the specified port number. (If not specified, the default port will be used.)
Authenticate to the server with the given password.
Specifies a callback to be executed upon a successful connection. The actual peer host and port number will be passed as arguments to the callback.
Specifies a callback to be executed if the connection failed (or authentication failed). The error message will be passed to the callback.
The callback may return an interval value (as a fractional number); if specified, the client will automatically attempt to reconnect at that interval until successful.
Specifies a callback to be executed if an I/O error occurs (e.g. connection reset by peer). The error message will be passed to the callback.
After you have successfully connected to the server, you can issue queries to it. (You'll want to do this in the on_connect callback handler fired by connect(), above.)
on_connect
To issue a query, use the query() method:
$cv = $redis->query(@args, [ $cb->($data, $error) ]);
The initial list of arguments to query() comprise the actual command. (See the command reference http://code.google.com/p/redis/wiki/CommandReference for a list of available commands.)
The final argument to query() specifies a optional callback (code reference) that will be fired when a response to the query is received. It will be called with the data (either a scalar, or an ARRAY reference for a multi-bulk response) as the first argument, and a scalar indicating whether the data is an error message as the second argument.
query() will return an AnyEvent condition variable that can also be used to retrieve the results via its recv() method (or via cb() if you don't want to block). If an error occurs, recv() will die, so be sure to wrap it in an eval:
eval
my $cv = $redis->query(@args); eval { my $result = $cv->recv; # ... }; if ($@) { warn "server error: $@"; ... }
Alternatively, you may invoke Redis commands as methods, e.g.:
$cv = $redis->set(key1 => $val1, [ $cb->($result, $error) ]); $cv = $redis->lrange('list', 0, 1000, [ $cb->($result, $error) ]); $cv = $redis->get(key1, [ $cb->($result, $error) ]);
AnyEvent::Redis2 began as a from-scratch implementation of a Redis module for AnyEvent. (When I began writing it, I didn't know such a module already existed.) Instead of abandoning it when I realized my oversight, I decided to contribute it.
The substantive differences from AnyEvent::Redis are:
Redis Command Reference http://code.google.com/p/redis/wiki/CommandReference
Michael S. Fischer <michael+cpan@dynamine.net>
Copyright (C) 2010 Michael S. Fischer.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install AnyEvent::Redis2, copy and paste the appropriate command in to your terminal.
cpanm
cpanm AnyEvent::Redis2
CPAN shell
perl -MCPAN -e shell install AnyEvent::Redis2
For more information on module installation, please visit the detailed CPAN module installation guide.