File Coverage

blib/lib/Cache/KyotoTycoon.pm
Criterion Covered Total %
statement 37 200 18.5
branch 5 124 4.0
condition 3 6 50.0
subroutine 10 32 31.2
pod 24 25 96.0
total 79 387 20.4


line stmt bran cond sub pod time code
1             package Cache::KyotoTycoon;
2 6     6   108935 use strict;
  6         13  
  6         238  
3 6     6   24 use warnings;
  6         8  
  6         155  
4 6     6   111 use 5.008001;
  6         19  
  6         255  
5             our $VERSION = '0.16';
6 6     6   2188 use Cache::KyotoTycoon::Cursor;
  6         11  
  6         142  
7 6     6   1989 use TSVRPC::Client;
  6         15  
  6         181  
8 6     6   43 use Carp ();
  6         8  
  6         12755  
9              
10             sub _errmsg {
11 1     1   2 my ($code, $msg) = @_;
12 1         85 return "Cache::KyotoTycoon unexpected response code: $code $msg";
13             }
14              
15             sub new {
16 1     1 0 739 my $class = shift;
17 1 50       7 my %args = @_==1 ? %{$_[0]} : @_;
  0         0  
18              
19 1   50     3 my $host = $args{host} || '127.0.0.1';
20 1   50     4 my $port = $args{port} || 1978;
21 1   50     14 my $db = $args{db} || 0;
22 1         6 my $base = "http://${host}:${port}/rpc/";
23 1 50       11 my $client = TSVRPC::Client->new(
24             timeout => exists( $args{timeout} ) ? $args{timeout} : 1,
25             base => $base,
26             );
27 1         3 my $self = bless {
28             db => $db,
29             client => $client,
30             }, $class;
31 1         3 return $self;
32             }
33              
34             sub db {
35 1     1 1 1 my $self = shift;
36 1 50       4 $self->{db} = shift if @_;
37 1         16 $self->{db};
38             }
39              
40             sub make_cursor {
41 0     0 1 0 my ($self, $cursor_id) = @_;
42 0         0 return Cache::KyotoTycoon::Cursor->new($cursor_id, $self->{db}, $self->{client});
43             }
44              
45             sub echo {
46 0     0 1 0 my ($self, $args) = @_;
47 0         0 my ($code, $body, $msg) = $self->{client}->call('echo', $args);
48 0 0       0 Carp::croak _errmsg($code, $msg) if $code ne 200;
49 0         0 return $body;
50             }
51              
52             sub report {
53 0     0 1 0 my ($self, ) = @_;
54 0         0 my ($code, $body, $msg) = $self->{client}->call('report');
55 0 0       0 Carp::croak _errmsg($code, $msg) if $code ne 200;
56 0         0 return $body;
57             }
58              
59             sub play_script {
60 0     0 1 0 my ($self, $name, $input) = @_;
61 0         0 my %args = (name => $name);
62 0         0 while (my ($k, $v) = each %$input) {
63 0         0 $args{"_$k"} = $v;
64             }
65 0         0 my ($code, $body, $msg) = $self->{client}->call('play_script', \%args);
66 0 0       0 Carp::croak _errmsg($code, $msg) if $code ne 200;
67 0         0 my %res;
68 0         0 while (my ($k, $v) = each %$body) {
69 0         0 $k =~ s!^_!!;
70 0         0 $res{$k} = $v;
71             }
72 0         0 return \%res;
73             }
74              
75             sub status {
76 0     0 1 0 my ($self, ) = @_;
77 0         0 my ($code, $body, $msg) = $self->{client}->call('status', {DB => $self->db});
78 0 0       0 Carp::croak _errmsg($code, $msg) unless $code eq 200;
79 0         0 return $body;
80             }
81              
82             sub clear {
83 0     0 1 0 my ($self, ) = @_;
84 0         0 my %args = (DB => $self->db);
85 0         0 my ($code, $body, $msg) = $self->{client}->call('clear', \%args);
86 0 0       0 Carp::croak _errmsg($code, $msg) unless $code eq 200;
87 0         0 return;
88             }
89              
90             sub synchronize {
91 0     0 1 0 my ($self, $hard, $command) = @_;
92 0         0 my %args = (DB => $self->db);
93 0 0       0 $args{hard} = $hard if $hard;
94 0 0       0 $args{command} = $command if defined $command;
95 0         0 my ($code, $body, $msg) = $self->{client}->call('synchronize', \%args);
96 0 0       0 return 1 if $code eq 200;
97 0 0       0 return 0 if $code eq 450;
98 0         0 Carp::croak _errmsg($code, $msg);
99             }
100              
101             sub set {
102 0     0 1 0 my ($self, $key, $value, $xt) = @_;
103 0         0 my %args = (DB => $self->db, key => $key, value => $value);
104 0 0       0 $args{xt} = $xt if defined $xt;
105 0         0 my ($code, $body, $msg) = $self->{client}->call('set', \%args);
106 0 0       0 Carp::croak _errmsg($code, $msg) unless $code eq 200;
107 0         0 return;
108             }
109              
110             sub add {
111 0     0 1 0 my ($self, $key, $value, $xt) = @_;
112 0         0 my %args = (DB => $self->db, key => $key, value => $value);
113 0 0       0 $args{xt} = $xt if defined $xt;
114 0         0 my ($code, $body, $msg) = $self->{client}->call('add', \%args);
115 0 0       0 return 1 if $code eq '200';
116 0 0       0 return 0 if $code eq '450';
117 0         0 Carp::croak _errmsg($code, $msg);
118             }
119              
120             sub replace {
121 0     0 1 0 my ($self, $key, $value, $xt) = @_;
122 0         0 my %args = (DB => $self->db, key => $key, value => $value);
123 0 0       0 $args{xt} = $xt if defined $xt;
124 0         0 my ($code, $body, $msg) = $self->{client}->call('replace', \%args);
125 0 0       0 return 1 if $code eq '200';
126 0 0       0 return 0 if $code eq '450';
127 0         0 Carp::croak _errmsg($code, $msg);
128             }
129              
130             sub append {
131 0     0 1 0 my ($self, $key, $value, $xt) = @_;
132 0         0 my %args = (DB => $self->db, key => $key, value => $value);
133 0 0       0 $args{xt} = $xt if defined $xt;
134 0         0 my ($code, $body, $msg) = $self->{client}->call('append', \%args);
135 0 0       0 Carp::croak _errmsg($code, $msg) unless $code eq '200';
136 0         0 return;
137             }
138              
139             sub increment {
140 0     0 1 0 my ($self, $key, $num, $xt) = @_;
141 0         0 my %args = (DB => $self->db, key => $key, num => $num);
142 0 0       0 $args{xt} = $xt if defined $xt;
143 0         0 my ($code, $body, $msg) = $self->{client}->call('increment', \%args);
144 0 0       0 Carp::croak _errmsg($code, $msg) unless $code eq '200';
145 0         0 return $body->{num};
146             }
147              
148             sub increment_double {
149 0     0 1 0 my ($self, $key, $num, $xt) = @_;
150 0         0 my %args = (DB => $self->db, key => $key, num => $num);
151 0 0       0 $args{xt} = $xt if defined $xt;
152 0         0 my ($code, $body, $msg) = $self->{client}->call('increment_double', \%args);
153 0 0       0 Carp::croak _errmsg($code, $msg) unless $code eq '200';
154 0         0 return $body->{num};
155             }
156              
157             sub cas {
158 0     0 1 0 my ($self, $key, $oval, $nval, $xt) = @_;
159 0         0 my %args = (DB => $self->db, key => $key);
160 0 0       0 $args{oval} = $oval if defined $oval;
161 0 0       0 $args{nval} = $nval if defined $nval;
162 0 0       0 $args{xt} = $xt if defined $xt;
163 0         0 my ($code, $body, $msg) = $self->{client}->call('cas', \%args);
164 0 0       0 return 1 if $code eq '200';
165 0 0       0 return 0 if $code eq '450';
166 0         0 Carp::croak _errmsg($code, $msg);
167             }
168              
169             sub remove {
170 0     0 1 0 my ($self, $key) = @_;
171 0         0 my %args = (DB => $self->db, key => $key);
172 0         0 my ($code, $body, $msg) = $self->{client}->call('remove', \%args);
173 0 0       0 return 1 if $code eq '200';
174 0 0       0 return 0 if $code eq '450';
175 0         0 Carp::croak _errmsg($code, $msg);
176             }
177              
178             sub get {
179 1     1 1 7 my ($self, $key) = @_;
180 1         5 my %args = (DB => $self->db, key => $key);
181 1         6 my ($code, $body, $msg) = $self->{client}->call('get', \%args);
182 1 50       6 if ($code eq 450) {
    50          
183 0         0 return; # no value for key
184             } elsif ($code eq 200) {
185 0 0       0 return wantarray ? ($body->{value}, $body->{xt}) : $body->{value};
186             } else {
187 1         4 Carp::croak _errmsg($code, $msg);
188             }
189             }
190              
191             sub set_bulk {
192 0     0 1   my ($self, $vals, $xt) = @_;
193 0           my %args = (DB => $self->db);
194 0           while (my ($k, $v) = each %$vals) {
195 0           $args{"_$k"} = $v;
196             }
197 0 0         $args{xt} = $xt if defined $xt;
198 0           my ($code, $body, $msg) = $self->{client}->call('set_bulk', \%args);
199 0 0         Carp::croak _errmsg($code, $msg) unless $code eq '200';
200 0           return $body->{num};
201             }
202              
203             sub remove_bulk {
204 0     0 1   my ($self, $keys) = @_;
205 0           my %args = (DB => $self->db);
206 0           for my $k (@$keys) {
207 0           $args{"_$k"} = '';
208             }
209 0           my ($code, $body, $msg) = $self->{client}->call('remove_bulk', \%args);
210 0 0         Carp::croak _errmsg($code, $msg) unless $code eq '200';
211 0           return $body->{num};
212             }
213              
214             sub get_bulk {
215 0     0 1   my ($self, $keys) = @_;
216 0           my %args = (DB => $self->db);
217 0           for my $k (@$keys) {
218 0           $args{"_$k"} = '';
219             }
220 0           my ($code, $body, $msg) = $self->{client}->call('get_bulk', \%args);
221 0 0         Carp::croak _errmsg($code, $msg) unless $code eq '200';
222 0           my %ret;
223 0           while (my ($k, $v) = each %$body) {
224 0 0         if ($k =~ /^_(.+)$/) {
225 0           $ret{$1} = $v;
226             }
227             }
228 0 0         die "fatal error" unless keys(%ret) == $body->{num};
229 0 0         return wantarray ? %ret : \%ret;
230             }
231              
232             sub vacuum {
233 0     0 1   my ($self, $step) = @_;
234 0           my %args = (DB => $self->db);
235 0 0         if (defined $step) {
236 0           $args{step} = $step;
237             }
238 0           my ($code, $body, $msg) = $self->{client}->call('vacuum', \%args);
239 0 0         Carp::croak _errmsg($code, $msg) unless $code eq '200';
240 0           return;
241             }
242              
243             sub match_prefix {
244 0     0 1   my ($self, $prefix, $max) = @_;
245 0           my %args = (DB => $self->db, prefix => $prefix);
246 0 0         $args{max} = $max if defined $max;
247 0           my ($code, $body, $msg) = $self->{client}->call('match_prefix', \%args);
248 0 0         Carp::croak _errmsg($code, $msg) unless $code eq '200';
249 0           my %ret;
250 0           while (my ($k, $v) = each %$body) {
251 0 0         if ($k =~ /^_(.+)$/) {
252 0           $ret{$1} = $v;
253             }
254             }
255 0 0         die "fatal error" unless keys(%ret) == $body->{num};
256 0 0         return wantarray ? %ret : \%ret;
257             }
258              
259             sub match_regex {
260 0     0 1   my ($self, $regex, $max) = @_;
261 0           my %args = (DB => $self->db, regex => $regex);
262 0 0         $args{max} = $max if defined $max;
263 0           my ($code, $body, $msg) = $self->{client}->call('match_regex', \%args);
264 0 0         Carp::croak _errmsg($code, $msg) unless $code eq '200';
265 0           my %ret;
266 0           while (my ($k, $v) = each %$body) {
267 0 0         if ($k =~ /^_(.+)$/) {
268 0           $ret{$1} = $v;
269             }
270             }
271 0 0         die "fatal error" unless keys(%ret) == $body->{num};
272 0 0         return wantarray ? %ret : \%ret;
273             }
274              
275             sub match_similar {
276 0     0 1   my ($self, $origin, $range, $utf8, $max) = @_;
277 0           my %args = (DB => $self->db, origin => $origin);
278 0 0         $args{range} = $max if defined $range;
279 0 0         $args{utf} = 1 if $utf8;
280 0 0         $args{max} = $max if defined $max;
281 0           my ($code, $body, $msg) = $self->{client}->call('match_similar', \%args);
282 0 0         Carp::croak _errmsg($code, $msg) unless $code eq '200';
283 0           my %ret;
284              
285 0           while (my ($k, $v) = each %$body) {
286 0 0         if ($k =~ /^_(.+)$/) {
287 0           $ret{$1} = $v;
288             }
289             }
290 0 0         die "fatal error" unless keys(%ret) == $body->{num};
291 0 0         return wantarray ? %ret : \%ret;
292             }
293              
294             1;
295             __END__