NAME Mojo::Redis - Asynchronous Redis client for Mojolicious. DESCRIPTION Mojo::Redis is an asynchronous Redis client for the Mojolicious framework. Currently we support these Redis commands in addition to the "METHODS" described in this module. append auth bgrewriteaof bgsave blpop brpop brpoplpush config_get config_set config_resetstat dbsize debug_object debug_segfault decr decrby del discard echo exec 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 monitor move mset msetnx multi 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 unwatch watch zadd zcard zcount zincrby zinterstore zrange zrangebyscore zrank zrem zremrangebyrank zremrangebyscore zrevrange zrevrangebyscore zrevrank zscore zunionstore If a command is missing, then please file a bug report and use "execute" in the meanwhile. SYNOPSIS Standalone use Mojo::Redis; my $redis = Mojo::Redis->new(server => '127.0.0.1:6379'); # Execute some commands $redis->ping( sub { my ($redis, $res) = @_; if (defined $res) { print "Got result: ", $res, "\n"; } } ); # Work with keys # Ommitting the callback still makes it non-blocking and "error" events # will be called if something terrible goes wrong. $redis->set(key => 'value'); $redis->get( key => sub { my ($redis, $res) = @_; print "Value of 'key' is $res\n"; } ); # Cleanup connection $redis->quit(sub { shift->ioloop->stop }); # Start IOLoop (in case it is not started yet) $redis->ioloop->start; Mojolicious::Lite example use Mojolicious::Lite; use Mojo::Redis; get '/user' => sub { my $self = shift->render_later; my $uid = $self->session('uid'); my $redis = Mojo::Redis->new; Mojo::IOLoop->delay( sub { my ($delay) = @_; $redis->hgetall("user:$uid", $delay->begin); }, sub { my ($delay, $user) = @_; $self->render(json=>$user); }, ); }; Websocket example websocket '/messages' => sub { my $self = shift; my $tx = $self->tx; my $redis = Mojo::Redis->new; # messages from redis $redis->on(message => 'pub:sub:channel', sub { my ($redis, $err, $message, $channel) = @_; # $channel == "pub:sub:channel" $tx->send($message || $err); }); # message from websocket $self->on(message => sub { my ($self, $message) = @_; $redis->publish('pub:sub:channel' => $message); }); $self->stash(redis => $redis); # need to clean up after websocket close $self->on(finish => sub { delete $self->stash->{redis}; undef $tx; }); }; app->start; EVENTS blpop $cb = $redis->on(blpop => @list_names => sub { my($redis, $err, $data, $list_name) = @_; warn "[REDIS BLPOP] Got ($data) from $list_name\n"; }); This is a special event which allow you to do recurring "blpop" without blocking the current Mojo::Redis object. Recurring means that once the callback has handled the data, it will issue a new BLPOP. One of the commands below is required to stop the BLPOP loop: $redis->on(message => @channels); $redis->on(message => @channels => $cb); brpop See "blpop". error $redis->on(error => sub { my($redis, $error) = @_; warn "[REDIS ERROR] $error\n"; }); Emitted if error occurred. Called before commands callbacks. close $redis->on(close => sub { my($redis) = @_; warn "[REDIS DISCONNECT]\n"; }); Emitted when the connection to the server gets closed. message $redis->on(message => @channels => sub { my($redis, $err, $message, $channel) = @_; warn "[REDIS PUBSUB] Got ($message) from $channel\n"; }); This is a special event which allow you to "subscribe" to messages directly in the current Mojo::Redis object instead of using a new Mojo::Redis::Subscription object. To unsubscribe you need to do one of these: $redis->on(message => @channels); $redis->on(message => @channels => $cb); ATTRIBUTES Mojo::Redis implements the following attributes. connected $bool = $self->connected; Returns true if we are indeed connected to the redis server. encoding $encoding = $redis->encoding; $redis = $redis->encoding('UTF-8'); Encoding used for stored data, defaults to "UTF-8". ioloop $ioloop = $redis->ioloop; $redis = $redis->ioloop(Mojo::IOLoop->new); Loop object to use for io operations, by default a Mojo::IOLoop singleton object will be used. protocol Holds a object of "protocol_redis". This attribute should be considered internal. protocol_redis use Protocol::Redis::XS; $redis->protocol_redis("Protocol::Redis::XS"); Protocol::Redis implementation' constructor for parsing. By default Protocol::Redis will be used. Parser library must support APIv1. Using Protocol::Redis::XS instead of default choice can speedup parsing. server $server = $redis->server; $redis = $redis->server('127.0.0.1:6379'); $redis = $redis->server('redis://anything:PASSWORD@127.0.0.1:6379/DB_INDEX'); "Redis" server connection string, defaults to "127.0.0.1:6379". The latter can be used if you want Mojo::Redis to automatically run "auth" with "PASSWORD" and/or "select" with "DB_INDEX" on connect. Both AUTH and DB_INDEX are optional. timeout $seconds = $redis->timeout; $redis = $redis->timeout(100); Maximum amount of time in seconds a connection can be inactive before being dropped, defaults to 0 - meaning no timeout. METHODS connect $redis = $redis->connect; Connect to "Redis" server. disconnect $redis = $self->disconnect; Used to disconnect from the server. This method is automatically called when this object goes out of scope. execute $redis = $redis->execute("ping" => sub { my ($redis, $result) = @_; # Process $result }); $redis->execute(lrange => "test", 0, -1 => sub {...}); $redis->execute(set => test => "test_ok"); $redis->execute( [lrange => "test", 0, -1], [get => "test"], [hmset => foo => { one => 1, two => 2 }], sub { my($redis, $lrange, $get, $hmset) = @_; # ... }, ); Execute specified command on "Redis" server. If error occurred during request $result will be set to undef, error string can be obtained with the "error" event. on Same as "on" in Mojo::EventEmitter, except it allows special events such as "blpop", "brpop" and "message". once Same as "once" in Mojo::EventEmitter, except it allows special events such as "blpop", "brpop" and "message". psubscribe Subscribes to channels matching the given patterns. # Subscribes to foo, foobar, foo.whaz, etc. my $psub = $redis->psubscribe('foo*'); $psub->on(message => sub { my ($self, $msg, $channel, $pattern) = @_; # 'hi!', 'foo.roo', 'foo*' }); $redis->publish('foo.roo' => 'hi!'); "psubscribe" has the same interface options and capabilities as "subscribe". subscribe It's possible to subscribe in two ways: $redis = $redis->subscribe('foo','bar' => sub { my ($redis, $data) = @_; }); The above code will overtake the current connection (if any) and put this object into a pure subscribe mode. my $sub = $redis->subscribe('foo','bar'); $sub->on(data => sub { my ($sub, $data) = @_; }); Opens up a new connection that subscribes to the given pubsub channels. Returns an instance of Mojo::Redis::Subscription. The existing $redis object can still be used to "get" data as expected. unsubscribe The opposite as "on". See also "unsubscribe" in Mojo::EventEmitter. SEE ALSO Protocol::Redis, Mojolicious, Mojo::IOLoop SUPPORT You can contact the developers "marcus" and "batman" on IRC: (#mojo on irc.perl.org) AUTHOR Sergey Zasenko, "undef@cpan.org". Forked from MojoX::Redis and updated to new IOLoop API by Marcus Ramberg "mramberg@cpan.org" and Jan Henning Thorsen "jhthorsen@cpan.org". COPYRIGHT AND LICENSE Copyright (C) 2010-2011, Sergey Zasenko (C) 2012, Marcus Ramberg This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.