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