lib/AnyEvent/Hiredis.pm

filter: Statementstmt 84.6% 11 / 13 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 88.8% 16 / 18 CC 1 SCAR 0.6
CC
1
Cov
85%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« script/bench.pl ↑ Summary
1 package AnyEvent::Hiredis;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $AnyEvent::Hiredis::VERSION = '0.06';
4 }
5 # ABSTRACT: AnyEvent hiredis API
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use Hiredis::Async;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use AnyEvent;
11  
12 sub new {
13 my ($class, %args) = @_;
14  
15 my $self = bless {}, $class;
16  
17 $self->{host} = $args{host} || '127.0.0.1';
18 $self->{port} = $args{port} || 6379;
19  
20 $self->{redis} = $self->_connect;
21  
22 return $self;
23 }
24  
25 sub _connect {
26 my ($self) = @_;
27  
28 my $redis = Hiredis::Async->new(
29 host => $self->{host},
30 port => $self->{port},
31  
32 addRead => sub { $self->_add_read_cb(@_) },
33 delRead => sub { $self->_del_read_cb(@_) },
34 addWrite => sub { $self->_add_write_cb(@_) },
35 delWrite => sub { $self->_del_write_cb(@_) },
36 );
37  
38 return $redis;
39 }
40  
41 sub _add_read_cb {
42 my ($self, $fd) = @_;
43  
44 return if defined $self->{reader};
45  
46 $self->{reader} = AnyEvent->io( fh => $fd, poll => 'r', cb => sub {
47 $self->{redis}->HandleRead;
48 });
49  
50 return;
51 }
52  
53 sub _del_read_cb {
54 my ($self, $fd) = @_;
55  
56 $self->{reader} = undef;
57  
58 return;
59 }
60  
61 sub _add_write_cb {
62 my ($self, $fd) = @_;
63  
64 return if defined $self->{writer};
65  
66 $self->{writer} = AnyEvent->io( fh => $fd, poll => 'w', cb => sub {
67 $self->{redis}->HandleWrite;
68 });
69  
70 return;
71 }
72  
73 sub _del_write_cb {
74 my ($self, $fd) = @_;
75  
76 $self->{writer} = undef;
77  
78 return;
79 }
80  
81 sub command {
82 my ($self, $cmd, $cb) = @_;
83  
84 $self->{redis}->Command($cmd, $cb);
85  
86 return;
87 }
88  
89 sub DESTROY {
90 my ($self) = @_;
91  
92 $self->{writer} = undef;
93 $self->{reader} = undef;
94 $self->{redis} = undef;
95 }
96  
97 1;
98  
99 __END__
« script/bench.pl ↑ Summary