File Coverage

blib/lib/EV/Hiredis.pm
Criterion Covered Total %
statement 30 33 90.9
branch 3 8 37.5
condition 2 5 40.0
subroutine 9 10 90.0
pod 1 1 100.0
total 45 57 78.9


line stmt bran cond sub pod time code
1             package EV::Hiredis;
2 11     11   2299894 use strict;
  11         99  
  11         278  
3 11     11   48 use warnings;
  11         19  
  11         253  
4              
5 11     11   650 use EV;
  11         2025  
  11         260  
6              
7             BEGIN {
8 11     11   64 use XSLoader;
  11         13  
  11         374  
9 11     11   54 our $VERSION = '0.05';
10 11         7125 XSLoader::load __PACKAGE__, $VERSION;
11             }
12              
13             sub new {
14 1     1 1 74 my ($class, %args) = @_;
15              
16 1   33     11 my $loop = $args{loop} || EV::default_loop;
17 1         7 my $self = $class->_new($loop);
18              
19 1   50 0   11 $self->on_error($args{on_error} || sub { die @_ });
  0         0  
20 1 50       3 $self->on_connect($args{on_connect}) if $args{on_connect};
21              
22 1 50       3 if (exists $args{host}) {
    50          
23 0 0       0 $self->connect($args{host}, defined $args{port} ? $args{port} : 6379);
24             }
25             elsif (exists $args{path}) {
26 0         0 $self->connect_unix($args{path});
27             }
28              
29 1         3 $self;
30             }
31              
32             our $AUTOLOAD;
33              
34             sub AUTOLOAD {
35 1     1   415 (my $method = $AUTOLOAD) =~ s/.*:://;
36              
37             my $sub = sub {
38 1     1   1 my $self = shift;
39 1         10 $self->command($method, @_);
40 1         4 };
41              
42 11     11   75 no strict 'refs';
  11         20  
  11         749  
43 1         3 *$method = $sub;
44 1         3 goto $sub;
45             }
46              
47             1;
48              
49             =head1 NAME
50              
51             EV::Hiredis - Asynchronous redis client using hiredis and EV
52              
53             =head1 SYNOPSIS
54              
55             use EV::Hiredis;
56            
57             my $redis = EV::Hiredis->new;
58             $redis->connect('127.0.0.1');
59            
60             # or
61             my $redis = EV::Hiredis->new( host => '127.0.0.1' );
62            
63             # command
64             $redis->set('foo' => 'bar', sub {
65             my ($res, $err) = @_;
66            
67             print $res; # OK
68            
69             $redis->get('foo', sub {
70             my ($res, $err) = @_;
71            
72             print $res; # bar
73            
74             $redis->disconnect;
75             });
76             });
77            
78             # start main loop
79             EV::run;
80              
81             =head1 DESCRIPTION
82              
83             EV::Hiredis is a asynchronous client for Redis using hiredis and L as backend.
84              
85             This module connected to L with C-Level interface so that it runs faster.
86              
87             =head1 ANYEVENT INTEGRATION
88              
89             L has a support for EV as its one of backends, so L can be used in your AnyEvent applications seamlessly.
90              
91             =head1 NO UTF-8 SUPPORT
92              
93             Unlike other redis modules, this module doesn't support utf-8 string.
94              
95             This module handle all variables as bytes. You should encode your utf-8 string before passing commands like following:
96              
97             use Encode;
98            
99             # set $val
100             $redis->set(foo => encode_utf8 $val, sub { ... });
101            
102             # get $val
103             $redis->get(foo, sub {
104             my $val = decode_utf8 $_[0];
105             });
106              
107             =head1 METHODS
108              
109             =head2 new(%options);
110              
111             Create new L instance.
112              
113             Available C<%options> are:
114              
115             =over
116              
117             =item * host => 'Str'
118              
119             =item * port => 'Int'
120              
121             Hostname and port number of redis-server to connect.
122              
123             =item * path => 'Str'
124              
125             UNIX socket path to connect.
126              
127             =item * on_error => $cb->($errstr)
128              
129             Error callback will be called when a connection level error occurs.
130              
131             This callback can be set by C<< $obj->on_error($cb) >> method any time.
132              
133             =item * on_connect => $cb->()
134              
135             Connection callback will be called when connection successful and completed to redis server.
136              
137             This callback can be set by C<< $obj->on_connect($cb) >> method any time.
138              
139             =item * loop => 'EV::loop',
140              
141             EV loop for running this instance. Default is C.
142              
143             =back
144              
145             All parameters are optional.
146              
147             If parameters about connection (host&port or path) is not passed, you should call C or C method by hand to connect to redis-server.
148              
149             =head2 connect($hostname, $port)
150              
151             =head2 connect_unix($path)
152              
153             Connect to a redis-server for C<$hostname:$port> or C<$path>.
154              
155             on_connect callback will be called if connection is successful, otherwise on_error callback is called.
156              
157             =head2 command($commands..., $cb->($result, $error))
158              
159             Do a redis command and return its result by callback.
160              
161             $redis->command('get', 'foo', sub {
162             my ($result, $error) = @_;
163              
164             print $result; # value for key 'foo'
165             print $error; # redis error string, undef if no error
166             });
167              
168             If any error is occurred, C<$error> presents the error message and C<$result> is undef.
169             If no error, C<$error> is undef and C<$result> presents response from redis.
170              
171             NOTE: Alternatively all commands can be called via AUTOLOAD interface.
172              
173             $redis->command('get', 'foo', sub { ... });
174              
175             is equivalent to:
176              
177             $redis->get('foo', sub { ... });
178              
179             =head2 disconnect
180              
181             Disconnect from redis-server. This method is usable for exiting event loop.
182              
183             =head2 on_error($cb->($errstr))
184              
185             Set new error callback to the instance.
186              
187             =head2 on_connect($cb->())
188              
189             Set new connect callback to the instance.
190              
191             =head1 AUTHOR
192              
193             Daisuke Murase
194              
195             =head1 COPYRIGHT AND LICENSE
196              
197             Copyright (c) 2013 Daisuke Murase All rights reserved.
198              
199             This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
200              
201             =cut