AnyEvent-RipeRedis
view release on metacpan or search on metacpan
lib/AnyEvent/RipeRedis.pm view on Meta::CPAN
$redis->psubscribe( qw( foo_* bar_* ),
sub {
my $msg = shift;
my $pattern = shift;
my $channel = shift;
# message handling...
}
);
=head2 publish( $channel, $message [, $cb->( $reply, $err ) ] )
Posts a message to the given channel.
=head2 unsubscribe( [ @channels ] [, $cb->( $reply, $err ) ] )
Unsubscribes the client from the given channels, or from all of them if none
is given. In first argument to the callback is passed the number of channels we
are currently subscribed or zero if we were unsubscribed from all channels.
$redis->unsubscribe( qw( foo bar ),
sub {
my $channels_num = shift;
my $err = shift;
if ( defined $err ) {
# error handling...
return;
}
# reply handling...
}
);
=head2 punsubscribe( [ @patterns ] [, $cb->( $reply, $err ) ] )
Unsubscribes the client from the given patterns, or from all of them if none
is given. See C<unsubscribe()> method for details.
$redis->punsubscribe( qw( foo_* bar_* ),
sub {
my $channels_num = shift;
my $err = shift;
if ( defined $err ) {
# error handling...
return;
}
# reply handling...
}
);
=head1 CONNECTION VIA UNIX-SOCKET
Redis 2.2 and higher support connection via UNIX domain socket. To connect via
a UNIX-socket in the parameter C<host> you have to specify C<unix/>, and in
the parameter C<port> you have to specify the path to the socket.
my $redis = AnyEvent::RipeRedis->new(
host => 'unix/',
port => '/tmp/redis.sock',
);
=head1 LUA SCRIPTS EXECUTION
Redis 2.6 and higher support execution of Lua scripts on the server side.
To execute a Lua script you can send one of the commands C<EVAL> or C<EVALSHA>,
or use the special method C<eval_cached()>.
=head2 eval_cached( $script, $keys_num [, @keys ] [, @args ] [, $cb->( $reply, $err ) ] ] );
When you call the C<eval_cached()> method, the client first generate a SHA1
hash for a Lua script and cache it in memory. Then the client optimistically
send the C<EVALSHA> command under the hood. If the C<E_NO_SCRIPT> error will be
returned, the client send the C<EVAL> command.
If you call the C<eval_cached()> method with the same Lua script, client don not
generate a SHA1 hash for this script repeatedly, it gets a hash from the cache
instead.
$redis->eval_cached( 'return { KEYS[1], KEYS[2], ARGV[1], ARGV[2] }',
2, 'key1', 'key2', 'first', 'second',
sub {
my $reply = shift;
my $err = shift;
if ( defined $err ) {
# error handling...
return;
}
foreach my $value ( @{$reply} ) {
print "$value\n";
}
}
);
Be care, passing a different Lua scripts to C<eval_cached()> method every time
cause memory leaks.
If Lua script returns multi-bulk reply with at least one error reply, to the
callback will be passed error object, and the reply will be contain nested
error objects.
$redis->eval_cached( "return { 'foo', redis.error_reply( 'Error.' ) }", 0,
sub {
my $reply = shift;
my $err = shift;
if ( defined $err ) {
my $err_msg = $err->message;
my $err_code = $err->code;
if ( defined $reply ) {
foreach my $nested_reply ( @{$reply} ) {
if ( ref($nested_reply) eq 'AnyEvent::RipeRedis::Error' ) {
my $nested_err_msg = $nested_reply->message();
my $nested_err_code = $nested_reply->code();
( run in 0.948 second using v1.01-cache-2.11-cpan-39bf76dae61 )