70 |
return $v; |
return $v; |
71 |
} |
} |
72 |
|
|
73 |
|
sub _sock_ok { |
74 |
|
my $ok = <$sock>; |
75 |
|
confess dump($ok) unless $ok eq "+OK\r\n"; |
76 |
|
} |
77 |
|
|
78 |
|
sub _sock_send_bulk { |
79 |
|
my ( $self, $command, $key, $value ) = @_; |
80 |
|
print $sock "$command $key " . length($value) . "\r\n$value\r\n"; |
81 |
|
_sock_ok(); |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
=head1 Connection Handling |
=head1 Connection Handling |
86 |
|
|
87 |
=head2 quit |
=head2 quit |
117 |
=cut |
=cut |
118 |
|
|
119 |
sub set { |
sub set { |
120 |
my ( $self, $k, $v, $new ) = @_; |
my ( $self, $key, $value, $new ) = @_; |
121 |
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"; |
|
122 |
} |
} |
123 |
|
|
124 |
=head2 get |
=head2 get |
219 |
return split(/\s/, _sock_result_bulk()); |
return split(/\s/, _sock_result_bulk()); |
220 |
} |
} |
221 |
|
|
222 |
|
=head2 randomkey |
223 |
|
|
224 |
|
my $key = $r->randomkey; |
225 |
|
|
226 |
|
=cut |
227 |
|
|
228 |
|
sub randomkey { |
229 |
|
my ( $self ) = @_; |
230 |
|
print $sock "RANDOMKEY\r\n"; |
231 |
|
_sock_result(); |
232 |
|
} |
233 |
|
|
234 |
|
=head2 rename |
235 |
|
|
236 |
|
my $ok = $r->rename( 'old-key', 'new-key', $new ); |
237 |
|
|
238 |
|
=cut |
239 |
|
|
240 |
|
sub rename { |
241 |
|
my ( $self, $old, $new, $nx ) = @_; |
242 |
|
print $sock "RENAME" . ( $nx ? 'NX' : '' ) . " $old $new\r\n"; |
243 |
|
_sock_ok(); |
244 |
|
} |
245 |
|
|
246 |
|
=head2 dbsize |
247 |
|
|
248 |
|
my $nr_keys = $r->dbsize; |
249 |
|
|
250 |
|
=cut |
251 |
|
|
252 |
|
sub dbsize { |
253 |
|
my ( $self ) = @_; |
254 |
|
print $sock "DBSIZE\r\n"; |
255 |
|
_sock_result(); |
256 |
|
} |
257 |
|
|
258 |
|
=head1 Commands operating on lists |
259 |
|
|
260 |
|
=head2 rpush |
261 |
|
|
262 |
|
$r->rpush( $key, $value ); |
263 |
|
|
264 |
|
=cut |
265 |
|
|
266 |
|
sub rpush { |
267 |
|
my ( $self, $key, $value ) = @_; |
268 |
|
$self->_sock_send_bulk('RPUSH', $key, $value); |
269 |
|
} |
270 |
|
|
271 |
|
=head2 lpush |
272 |
|
|
273 |
|
$r->lpush( $key, $value ); |
274 |
|
|
275 |
|
=cut |
276 |
|
|
277 |
|
sub lpush { |
278 |
|
my ( $self, $key, $value ) = @_; |
279 |
|
$self->_sock_send_bulk('LPUSH', $key, $value); |
280 |
|
} |
281 |
|
|
282 |
=head1 AUTHOR |
=head1 AUTHOR |
283 |
|
|
284 |
Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >> |
Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >> |