blib/lib/EV/Hiredis.pm

filter: Statementstmt 91.8% 34 / 37 Branchbran 41.6% 5 / 12 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 90.0% 9 / 10 Podpod 100% 1 / 1 totaltotal 72.8% 51 / 70 CC 9 SCAR 22.1
CC
9
Cov
75%
new
17.2
__ANON__
1.2
BEGIN
1.0
CRAP 9.1
↑ Summary builder/MyBuilder.pm »
1 package EV::Hiredis;
2 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 11 ▶use EV;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶ use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 our $VERSION = '0.07';
10 11 XSLoader::load __PACKAGE__, $VERSION;
11 }
12  
13 1 ▶sub new {
Subroutine new: called CC=9 SCAR=28.5
Pod documented
14 1 my ($class, %args) = @_;
15  
16 1 ▶ my $loop = $args{loop} || EV::default_loop;
Condition33%1/3
$args{'loop'} || &EV::default_loop()
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{'loop'} || &EV::default_loop()
$args{'loop'} &EV::default_loop()
Truth table33%1/3
A || B
A= $args{'loop'}B= &EV::default_loop()
ABresult
1X1
011
000
17 1 my $self = $class->_new($loop);
18  
19 0 ▶ $self->on_error($args{on_error} || sub { die @_ });
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$args{'on_error'} || sub { die @_; }
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{'on_error'} || sub { die @_; }
$args{'on_error'} sub { die @_; }
Truth table33%1/3
A || B
A= $args{'on_error'}B= sub { die @_; }
ABresult
1X1
011
000
20 1 ▶ $self->on_connect($args{on_connect}) if $args{on_connect};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"on_connect"} 0 1 1
21 1 ▶ $self->connect_timeout($args{connect_timeout}) if $args{connect_timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"connect_timeout"} 0 1 1
22 1 ▶ $self->command_timeout($args{command_timeout}) if $args{command_timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"command_timeout"} 0 1 1
23  
24 1 ▶ if (exists $args{host}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'host'}) { } 0 1 1
elsif (exists $args{'path'}) { } 0 1 1
25 0 ▶ $self->connect($args{host}, defined $args{port} ? $args{port} : 6379);
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'port'} ? : 0 0 0
26 }
27 elsif (exists $args{path}) {
28 0 $self->connect_unix($args{path});
29 }
30  
31 1 $self;
32 }
33  
34 11 our $AUTOLOAD;
35  
36 1 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
37 1 (my $method = $AUTOLOAD) =~ s/.*:://;
38  
39 1 ▶ my $sub = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
40 1 my $self = shift;
41 1 $self->command($method, @_);
42 1 };
43  
44 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 *$method = $sub;
46 1 goto $sub;
47 }
48  
49 11 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<EV> as backend.
86  
87 This module connected to L<EV> with C-Level interface so that it runs faster.
88  
89 =head1 ANYEVENT INTEGRATION
90  
91 L<AnyEvent> has a support for EV as its one of backends, so L<EV::Hiredis> 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<EV::Hiredis> 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<EV::default_loop>.
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<connect> or C<connect_unix> 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 <typester@cpan.org>
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
↑ Summary builder/MyBuilder.pm »