51 |
$self; |
$self; |
52 |
} |
} |
53 |
|
|
54 |
|
sub _sock_result { |
55 |
|
my $result = <$sock>; |
56 |
|
warn "# result: ",dump( $result ); |
57 |
|
$result =~ s{\r\n$}{} || warn "can't find cr/lf"; |
58 |
|
return $result; |
59 |
|
} |
60 |
|
|
61 |
|
sub _sock_read_bulk { |
62 |
|
my $len = <$sock>; |
63 |
|
warn "## bulk len: ",dump($len); |
64 |
|
return undef if $len eq "nil\r\n"; |
65 |
|
my $v; |
66 |
|
read($sock, $v, $len) || die $!; |
67 |
|
warn "## bulk v: ",dump($v); |
68 |
|
my $crlf; |
69 |
|
read($sock, $crlf, 2); # skip cr/lf |
70 |
|
return $v; |
71 |
|
} |
72 |
|
|
73 |
|
sub _sock_result_bulk { |
74 |
|
my $self = shift; |
75 |
|
warn "## _sock_result_bulk ",dump( @_ ); |
76 |
|
print $sock join(' ',@_) . "\r\n"; |
77 |
|
_sock_read_bulk(); |
78 |
|
} |
79 |
|
|
80 |
|
sub _sock_ok { |
81 |
|
my $ok = <$sock>; |
82 |
|
confess dump($ok) unless $ok eq "+OK\r\n"; |
83 |
|
} |
84 |
|
|
85 |
|
sub _sock_send { |
86 |
|
my $self = shift; |
87 |
|
warn "## _sock_send ",dump( @_ ); |
88 |
|
print $sock join(' ',@_) . "\r\n"; |
89 |
|
_sock_result(); |
90 |
|
} |
91 |
|
|
92 |
|
sub _sock_send_ok { |
93 |
|
my $self = shift; |
94 |
|
warn "## _sock_send_ok ",dump( @_ ); |
95 |
|
print $sock join(' ',@_) . "\r\n"; |
96 |
|
_sock_ok(); |
97 |
|
} |
98 |
|
|
99 |
|
sub _sock_send_bulk { |
100 |
|
my ( $self, $command, $key, $value ) = @_; |
101 |
|
print $sock "$command $key " . length($value) . "\r\n$value\r\n"; |
102 |
|
_sock_ok(); |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
=head1 Connection Handling |
=head1 Connection Handling |
107 |
|
|
108 |
=head2 quit |
=head2 quit |
138 |
=cut |
=cut |
139 |
|
|
140 |
sub set { |
sub set { |
141 |
my ( $self, $k, $v, $new ) = @_; |
my ( $self, $key, $value, $new ) = @_; |
142 |
print $sock ( $new ? "SETNX" : "SET" ) . " $k " . length($v) . "\r\n$v\r\n"; |
$self->_sock_send_bulk( "SET" . ( $new ? 'NX' : '' ), $key, $value ); |
|
my $ok = <$sock>; |
|
|
confess dump($ok) unless $ok eq "+OK\r\n"; |
|
143 |
} |
} |
144 |
|
|
145 |
=head2 get |
=head2 get |
149 |
=cut |
=cut |
150 |
|
|
151 |
sub get { |
sub get { |
152 |
my ( $self, $k ) = @_; |
my $self = shift; |
153 |
print $sock "GET $k\r\n"; |
$self->_sock_result_bulk('GET', @_); |
|
my $len = <$sock>; |
|
|
# warn "# len: ",dump($len); |
|
|
return undef if $len eq "nil\r\n"; |
|
|
my $v; |
|
|
read($sock, $v, $len) || die $!; |
|
|
# warn "# v: ",dump($v); |
|
|
my $crlf; |
|
|
read($sock, $crlf, 2); # skip cr/lf |
|
|
return $v; |
|
154 |
} |
} |
155 |
|
|
156 |
=head2 incr |
=head2 incr |
160 |
|
|
161 |
=cut |
=cut |
162 |
|
|
163 |
|
|
164 |
|
|
165 |
sub incr { |
sub incr { |
166 |
my ( $self, $key, $value ) = @_; |
my $self = shift; |
167 |
if ( defined $value ) { |
$self->_sock_send( 'INCR' . ( $#_ ? 'BY' : '' ), @_ ); |
|
print $sock "INCRBY $key $value\r\n"; |
|
|
} else { |
|
|
print $sock "INCR $key\r\n"; |
|
|
} |
|
|
my $count = <$sock>; |
|
|
warn "# $key = $count"; |
|
|
return $count; |
|
168 |
} |
} |
169 |
|
|
170 |
=head2 decr |
=head2 decr |
175 |
=cut |
=cut |
176 |
|
|
177 |
sub decr { |
sub decr { |
178 |
my ( $self, $key, $value ) = @_; |
my $self = shift; |
179 |
if ( defined $value ) { |
$self->_sock_send( 'DECR' . ( $#_ ? 'BY' : '' ), @_ ); |
|
print $sock "DECRBY $key $value\r\n"; |
|
|
} else { |
|
|
print $sock "DECR $key\r\n"; |
|
|
} |
|
|
my $count = <$sock>; |
|
|
warn "# $key = $count"; |
|
|
return $count; |
|
180 |
} |
} |
181 |
|
|
182 |
=head2 exists |
=head2 exists |
187 |
|
|
188 |
sub exists { |
sub exists { |
189 |
my ( $self, $key ) = @_; |
my ( $self, $key ) = @_; |
190 |
print $sock "EXISTS $key\r\n"; |
$self->_sock_send( 'EXISTS', $key ); |
191 |
my $found = <$sock>; |
} |
192 |
$found =~ s{\r\n$}{}; |
|
193 |
warn "# exists $key = $found"; |
=head2 del |
194 |
return $found; |
|
195 |
|
$r->del( 'key' ) || warn "key doesn't exist"; |
196 |
|
|
197 |
|
=cut |
198 |
|
|
199 |
|
sub del { |
200 |
|
my ( $self, $key ) = @_; |
201 |
|
$self->_sock_send( 'DEL', $key ); |
202 |
|
} |
203 |
|
|
204 |
|
=head2 type |
205 |
|
|
206 |
|
$r->type( 'key' ); # = string |
207 |
|
|
208 |
|
=cut |
209 |
|
|
210 |
|
sub type { |
211 |
|
my ( $self, $key ) = @_; |
212 |
|
$self->_sock_send( 'TYPE', $key ); |
213 |
|
} |
214 |
|
|
215 |
|
=head1 Commands operating on the key space |
216 |
|
|
217 |
|
=head2 keys |
218 |
|
|
219 |
|
my @keys = $r->keys( '*glob_pattern*' ); |
220 |
|
|
221 |
|
=cut |
222 |
|
|
223 |
|
sub keys { |
224 |
|
my ( $self, $glob ) = @_; |
225 |
|
return split(/\s/, $self->_sock_result_bulk( 'KEYS', $glob )); |
226 |
|
} |
227 |
|
|
228 |
|
=head2 randomkey |
229 |
|
|
230 |
|
my $key = $r->randomkey; |
231 |
|
|
232 |
|
=cut |
233 |
|
|
234 |
|
sub randomkey { |
235 |
|
my ( $self ) = @_; |
236 |
|
$self->_sock_send( 'RANDOMKEY' ); |
237 |
|
} |
238 |
|
|
239 |
|
=head2 rename |
240 |
|
|
241 |
|
my $ok = $r->rename( 'old-key', 'new-key', $new ); |
242 |
|
|
243 |
|
=cut |
244 |
|
|
245 |
|
sub rename { |
246 |
|
my ( $self, $old, $new, $nx ) = @_; |
247 |
|
$self->_sock_send_ok( 'RENAME' . ( $nx ? 'NX' : '' ), $old, $new ); |
248 |
|
} |
249 |
|
|
250 |
|
=head2 dbsize |
251 |
|
|
252 |
|
my $nr_keys = $r->dbsize; |
253 |
|
|
254 |
|
=cut |
255 |
|
|
256 |
|
sub dbsize { |
257 |
|
my ( $self ) = @_; |
258 |
|
$self->_sock_send('DBSIZE'); |
259 |
|
} |
260 |
|
|
261 |
|
=head1 Commands operating on lists |
262 |
|
|
263 |
|
=head2 rpush |
264 |
|
|
265 |
|
$r->rpush( $key, $value ); |
266 |
|
|
267 |
|
=cut |
268 |
|
|
269 |
|
sub rpush { |
270 |
|
my ( $self, $key, $value ) = @_; |
271 |
|
$self->_sock_send_bulk('RPUSH', $key, $value); |
272 |
|
} |
273 |
|
|
274 |
|
=head2 lpush |
275 |
|
|
276 |
|
$r->lpush( $key, $value ); |
277 |
|
|
278 |
|
=cut |
279 |
|
|
280 |
|
sub lpush { |
281 |
|
my ( $self, $key, $value ) = @_; |
282 |
|
$self->_sock_send_bulk('LPUSH', $key, $value); |
283 |
|
} |
284 |
|
|
285 |
|
=head2 llen |
286 |
|
|
287 |
|
$r->llen( $key ); |
288 |
|
|
289 |
|
=cut |
290 |
|
|
291 |
|
sub llen { |
292 |
|
my ( $self, $key ) = @_; |
293 |
|
$self->_sock_send( 'LLEN', $key ); |
294 |
} |
} |
295 |
|
|
296 |
|
=head2 lrange |
297 |
|
|
298 |
|
my @list = $r->lrange( $key, $start, $end ); |
299 |
|
|
300 |
|
=cut |
301 |
|
|
302 |
|
sub lrange { |
303 |
|
my ( $self, $key, $start, $end ) = @_; |
304 |
|
my $size = $self->_sock_send('LRANGE', $key, $start, $end); |
305 |
|
|
306 |
|
confess $size unless $size > 0; |
307 |
|
$size--; |
308 |
|
|
309 |
|
my @list = ( 0 .. $size ); |
310 |
|
foreach ( 0 .. $size ) { |
311 |
|
$list[ $_ ] = _sock_read_bulk(); |
312 |
|
} |
313 |
|
|
314 |
|
warn "## lrange $key $start $end = [$size] ", dump( @list ); |
315 |
|
return @list; |
316 |
|
} |
317 |
|
|
318 |
|
=head2 ltrim |
319 |
|
|
320 |
|
my $ok = $r->ltrim( $key, $start, $end ); |
321 |
|
|
322 |
|
=cut |
323 |
|
|
324 |
|
sub ltrim { |
325 |
|
my ( $self, $key, $start, $end ) = @_; |
326 |
|
$self->_sock_send_ok( 'LTRIM', $key, $start, $end ); |
327 |
|
} |
328 |
|
|
329 |
|
=head2 lindex |
330 |
|
|
331 |
|
$r->lindex( $key, $index ); |
332 |
|
|
333 |
|
=cut |
334 |
|
|
335 |
|
sub lindex { |
336 |
|
my ( $self, $key, $index ) = @_; |
337 |
|
$self->_sock_result_bulk( 'lindex', $key, $index ); |
338 |
|
} |
339 |
|
|
340 |
|
|
341 |
=head1 AUTHOR |
=head1 AUTHOR |
342 |
|
|
343 |
Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >> |
Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >> |