File Coverage

blib/lib/EV/Hiredis.pm
Criterion Covered Total %
statement 32 35 91.4
branch 5 12 41.6
condition 2 5 40.0
subroutine 9 10 90.0
pod 1 1 100.0
total 49 63 77.7


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