5 |
|
|
6 |
use IO::Socket::INET; |
use IO::Socket::INET; |
7 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
8 |
|
use Carp qw/confess/; |
9 |
|
|
10 |
=head1 NAME |
=head1 NAME |
11 |
|
|
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_result_bulk { |
62 |
|
my $len = <$sock>; |
63 |
|
warn "# len: ",dump($len); |
64 |
|
return undef if $len eq "nil\r\n"; |
65 |
|
my $v; |
66 |
|
read($sock, $v, $len) || die $!; |
67 |
|
warn "# v: ",dump($v); |
68 |
|
my $crlf; |
69 |
|
read($sock, $crlf, 2); # skip cr/lf |
70 |
|
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 { |
79 |
|
my $self = shift; |
80 |
|
print $sock join(' ',@_) . "\r\n"; |
81 |
|
_sock_result(); |
82 |
|
} |
83 |
|
|
84 |
|
sub _sock_send_bulk { |
85 |
|
my ( $self, $command, $key, $value ) = @_; |
86 |
|
print $sock "$command $key " . length($value) . "\r\n$value\r\n"; |
87 |
|
_sock_ok(); |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
=head1 Connection Handling |
=head1 Connection Handling |
92 |
|
|
93 |
=head2 quit |
=head2 quit |
118 |
|
|
119 |
=head2 set |
=head2 set |
120 |
|
|
121 |
$r->set( foo => 'bar' ); |
$r->set( foo => 'bar', $new ); |
122 |
|
|
123 |
=cut |
=cut |
124 |
|
|
125 |
sub set { |
sub set { |
126 |
my ( $self, $k, $v ) = @_; |
my ( $self, $key, $value, $new ) = @_; |
127 |
print $sock "SET $k " . length($v) . "\r\n$v\r\n"; |
$self->_sock_send_bulk( "SET" . ( $new ? 'NX' : '' ), $key, $value ); |
|
my $ok = <$sock>; |
|
|
die dump($ok) unless $ok eq "+OK\r\n"; |
|
128 |
} |
} |
129 |
|
|
130 |
=head2 get |
=head2 get |
136 |
sub get { |
sub get { |
137 |
my ( $self, $k ) = @_; |
my ( $self, $k ) = @_; |
138 |
print $sock "GET $k\r\n"; |
print $sock "GET $k\r\n"; |
139 |
my $len = <$sock>; |
_sock_result_bulk(); |
|
# 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; |
|
140 |
} |
} |
141 |
|
|
142 |
|
=head2 incr |
143 |
|
|
144 |
|
$r->incr('counter'); |
145 |
|
$r->incr('tripplets', 3); |
146 |
|
|
147 |
|
=cut |
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
sub incr { |
152 |
|
my ( $self, $key, $value ) = @_; |
153 |
|
if ( defined $value ) { |
154 |
|
print $sock "INCRBY $key $value\r\n"; |
155 |
|
} else { |
156 |
|
print $sock "INCR $key\r\n"; |
157 |
|
} |
158 |
|
_sock_result(); |
159 |
|
} |
160 |
|
|
161 |
|
=head2 decr |
162 |
|
|
163 |
|
$r->decr('counter'); |
164 |
|
$r->decr('tripplets', 3); |
165 |
|
|
166 |
|
=cut |
167 |
|
|
168 |
|
sub decr { |
169 |
|
my ( $self, $key, $value ) = @_; |
170 |
|
if ( defined $value ) { |
171 |
|
print $sock "DECRBY $key $value\r\n"; |
172 |
|
} else { |
173 |
|
print $sock "DECR $key\r\n"; |
174 |
|
} |
175 |
|
_sock_result(); |
176 |
|
} |
177 |
|
|
178 |
|
=head2 exists |
179 |
|
|
180 |
|
$r->exists( 'key' ) && print "got key!"; |
181 |
|
|
182 |
|
=cut |
183 |
|
|
184 |
|
sub exists { |
185 |
|
my ( $self, $key ) = @_; |
186 |
|
print $sock "EXISTS $key\r\n"; |
187 |
|
_sock_result(); |
188 |
|
} |
189 |
|
|
190 |
|
=head2 del |
191 |
|
|
192 |
|
$r->del( 'key' ) || warn "key doesn't exist"; |
193 |
|
|
194 |
|
=cut |
195 |
|
|
196 |
|
sub del { |
197 |
|
my ( $self, $key ) = @_; |
198 |
|
print $sock "DEL $key\r\n"; |
199 |
|
_sock_result(); |
200 |
|
} |
201 |
|
|
202 |
|
=head2 type |
203 |
|
|
204 |
|
$r->type( 'key' ); # = string |
205 |
|
|
206 |
|
=cut |
207 |
|
|
208 |
|
sub type { |
209 |
|
my ( $self, $key ) = @_; |
210 |
|
print $sock "TYPE $key\r\n"; |
211 |
|
_sock_result(); |
212 |
|
} |
213 |
|
|
214 |
|
=head1 Commands operating on the key space |
215 |
|
|
216 |
|
=head2 keys |
217 |
|
|
218 |
|
my @keys = $r->keys( '*glob_pattern*' ); |
219 |
|
|
220 |
|
=cut |
221 |
|
|
222 |
|
sub keys { |
223 |
|
my ( $self, $glob ) = @_; |
224 |
|
print $sock "KEYS $glob\r\n"; |
225 |
|
return split(/\s/, _sock_result_bulk()); |
226 |
|
} |
227 |
|
|
228 |
|
=head2 randomkey |
229 |
|
|
230 |
|
my $key = $r->randomkey; |
231 |
|
|
232 |
|
=cut |
233 |
|
|
234 |
|
sub randomkey { |
235 |
|
my ( $self ) = @_; |
236 |
|
print $sock "RANDOMKEY\r\n"; |
237 |
|
_sock_result(); |
238 |
|
} |
239 |
|
|
240 |
|
=head2 rename |
241 |
|
|
242 |
|
my $ok = $r->rename( 'old-key', 'new-key', $new ); |
243 |
|
|
244 |
|
=cut |
245 |
|
|
246 |
|
sub rename { |
247 |
|
my ( $self, $old, $new, $nx ) = @_; |
248 |
|
print $sock "RENAME" . ( $nx ? 'NX' : '' ) . " $old $new\r\n"; |
249 |
|
_sock_ok(); |
250 |
|
} |
251 |
|
|
252 |
|
=head2 dbsize |
253 |
|
|
254 |
|
my $nr_keys = $r->dbsize; |
255 |
|
|
256 |
|
=cut |
257 |
|
|
258 |
|
sub dbsize { |
259 |
|
my ( $self ) = @_; |
260 |
|
print $sock "DBSIZE\r\n"; |
261 |
|
_sock_result(); |
262 |
|
} |
263 |
|
|
264 |
|
=head1 Commands operating on lists |
265 |
|
|
266 |
|
=head2 rpush |
267 |
|
|
268 |
|
$r->rpush( $key, $value ); |
269 |
|
|
270 |
|
=cut |
271 |
|
|
272 |
|
sub rpush { |
273 |
|
my ( $self, $key, $value ) = @_; |
274 |
|
$self->_sock_send_bulk('RPUSH', $key, $value); |
275 |
|
} |
276 |
|
|
277 |
|
=head2 lpush |
278 |
|
|
279 |
|
$r->lpush( $key, $value ); |
280 |
|
|
281 |
|
=cut |
282 |
|
|
283 |
|
sub lpush { |
284 |
|
my ( $self, $key, $value ) = @_; |
285 |
|
$self->_sock_send_bulk('LPUSH', $key, $value); |
286 |
|
} |
287 |
|
|
288 |
|
=head2 llen |
289 |
|
|
290 |
|
$r->llen( $key ); |
291 |
|
|
292 |
|
=cut |
293 |
|
|
294 |
|
sub llen { |
295 |
|
my ( $self, $key ) = @_; |
296 |
|
$self->_sock_send( 'llen', $key ); |
297 |
|
} |
298 |
|
|
299 |
=head1 AUTHOR |
=head1 AUTHOR |
300 |
|
|