NAME
AnyEvent::Redis - an event-driven Redis client
SYNOPSIS
use AnyEvent::Redis;
my $redis = AnyEvent::Redis->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);
DESCRIPTION
This module is an AnyEvent user; you must use and run a supported event loop.
AnyEvent::Redis 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.
Establishing a connection
To connect to the Redis server, use the connect() method:
my $redis = AnyEvent::Redis->connect(host => <host>, ...);
The host
argument is required.
Optional (but recommended) arguments include:
- port => $port
-
Connect to the server on the specified port number. (If not specified, the default port will be used.)
- auth => $password
-
Authenticate to the server with the given password.
- on_connect => $cb->($conn, $host, $port)
-
Specifies a callback to be executed upon a successful connection. The connection object and the actual peer host and port number will be passed as arguments to the callback.
- on_connect_error => $cb->($conn, $errmsg)
-
Specifies a callback to be executed if the connection failed (or authentication failed). The connection object and the error message will be passed as arguments to the callback. The connection is not reusable.
- on_error => $cb->($conn, $errmsg)
-
Specifies a callback to be executed if an I/O error occurs (e.g. connection reset by peer). The connection object and the error message will be passed as arguments to the callback. The connection is not reusable.
Queries and responses
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.)
To issue a query, use the query() method:
$redis->query(@args, $cb->($errmsg, @data)));
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 callback (code reference) that will be fired when a response to the query is received. It will be called with the error message (which will be undef
if there was no error) as the first argument; the remaining arguments will be the values (if any) that comprise the response.
Alternatively, you may invoke Redis commands as methods, e.g.:
$redis->set(key1 => $val1, sub { ... });
$redis->get(key1 => sub { ... });
SEE ALSO
Redis Command Reference http://code.google.com/p/redis/wiki/CommandReference
AUTHOR
Michael S. Fischer <michael+cpan@dynamine.net>
COPYRIGHT AND LICENSE
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.