NAME
RedisDB - Module to access redis database
SYNOPSIS
use RedisDB;
my $redis = RedisDB->new(host => 'localhost', port => 6379);
$redis->set($key, $value);
my $value = $redis->get($key);
DESCRIPTION
This is alfa version, use on your own risk, interface is subject to change
This module provides interface to access redis database. It transparently handles disconnects and forks. It supports pipelining mode.
METHODS
$class->new(%options)
Creates new RedisDB object. The following options are allowed:
- host
-
domain name of the host running redis server. Default: "localhost"
- port
-
port to connect. Default: 6379
- lazy
-
by default new establishes connection to the server. If this parameter is set, then connection will be established when you will send command to the server.
$self->execute($command, @arguments)
send command to the server and return server reply. It throws exception if server returns error. It may be more convenient to use instead of this method wrapper named after the redis command. E.g.:
$redis->execute('set', key => 'value');
# is the same as
$redis->set(key => 'value');
See "SUPPORTED REDIS COMMANDS" section for the full list of defined aliases.
Note, that you can't use execute if you send some commands in pipelining mode and not yet got all the replies.
$self->connect
establish connection to the server. There's usually no need to use this method, as connection is established by new or when you send a command.
$self->send_command($command, @arguments)
send command to the server. Returns true if command was successfully sent, or dies if error occured. Note, that it doesn't return server reply, you should retrieve reply using get_reply method.
$self->reply_ready
This method may be used in pipelining mode to check if there are some replies already received from server. Returns number of replies available for reading.
$self->get_reply
receive reply from the server. Method returns two elements array. First element is the character depending on type of reply: "+" one line reply, "-" error, ":" integer reply, "$" bulk reply, "*" multi-bulk reply. Second element is reply itself, which is scalar for all replies, except that it is array reference for multi-bulk.
SUPPORTED REDIS COMMANDS
Usually, instead of using execute method, you can just use methods with names matching names of the redis commands. The following methods are defined as wrappers around execute: append, auth, bgrewriteaof, bgsave, blpop, brpoplpush, config_get, config_set, config_resetstat, dbsize, debug_object, debug_segfault, decr, decrby, del, echo, exists, expire, expireat, flushall, flushdb, get, getbit, getrange, getset, hdel, hexists, hget, hgetall, hincrby, hkeys, hlen, hmget, hmset, hset, hsetnx, hvals, incr, incrby, info, keys, lastsave, lindex, linsert, llen, lpop, lpush, lpushx, lrange, lrem, lset, ltrim, mget, move, mset, msetnx, persist, ping, publish, quit, randomkey, rename, renamenx, rpop, rpoplpush, rpush, rpushx, sadd, save, scard, sdiff, sdiffstore, select, set, setbit, setex, setnx, setrange, shutdown, sinter, sinterstore, sismember, slaveof, smembers, smove, sort, spop, srandmember, srem, strlen, sunion, sunionstore, sync, ttl, type, zadd, zcard, zcount, zincrby, zinterstore, zrange, zrangebyscore, zrank, zremrangebyrank, zremrangebyscore, zrevrange, zrevrangebyscore, zrevrank, zscore, zunionstore
See description of all commands in redis documentation at http://redis.io/commands.
HANDLING OF SERVER DISCONNECTS
Redis server may close connection if it was idle for some time, also connection may be closed in case redis-server was restarted. RedisDB restores connection to the server but only if no data was lost as result of disconnect. E.g. if client was idle for some time and redis server closed connection, it will be transparently restored on sending next command. If you send a command and server closed connection without sending complete reply, connection will not be restored and module will throw exception.
PIPELINING SUPPORT
You can send commands in pipelining mode. In this case you sending multiple commands to the server without waiting for replies. You can use send_command method to send multiple commands to the server. reply_ready function may be used to check if some replies are already received. And get_reply function may be used to fetch received reply. Note, that you can't use execute method (or wrappers around it, like get or <set>) while in pipeline mode, you must receive replies on all pipelined commands first.
SEE ALSO
Redis, Redis::hiredis, AnyEvent::Redis
WHY ANOTHER ONE
I was in need of a client for redis database. AnyEvent::Redis didn't suite me as it requires event loop, and it didn't fit into existing code. Problem with Redis is that it doesn't (at the time I write this) reconnect to the server if connection was closed after timeout or as result or server restart, and it doesn't support pipelining. After analizing what I need to change in Redis in order to get all I want (see TODO), I decided that it will be simplier to write new module from scratch. This also solves the problem of backward compatibility. Pedro Melo, maintainer of Redis have plans to implement some of these features too.
TODO
Test all commands
User defined error callback
Handle cases than client not interested in replies
Transactions support (MULTI, EXEC, DISCARD, WATCH, UNWATCH)
Subscriptions support (PSUBSCRIBE, PUNSUBSCRIBE, SUBSCRIBE, UNSUBSCRIBE)
MONITOR support
Non-blocking check if reply available
BUGS
Please report any bugs or feature requests to bug-redisdb at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=RedisDB. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
AUTHOR
Pavel Shaydo, <zwon at cpan.org>
LICENSE AND COPYRIGHT
Copyright 2011 Pavel Shaydo.
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.