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 |
=head1 Connection Handling |
=head1 Connection Handling |
79 |
|
|
80 |
=head2 quit |
=head2 quit |
111 |
|
|
112 |
sub set { |
sub set { |
113 |
my ( $self, $k, $v, $new ) = @_; |
my ( $self, $k, $v, $new ) = @_; |
114 |
print $sock ( $new ? "SETNX" : "SET" ) . " $k " . length($v) . "\r\n$v\r\n"; |
print $sock "SET" . ( $new ? 'NX' : '' ) . " $k " . length($v) . "\r\n$v\r\n"; |
115 |
my $ok = <$sock>; |
_sock_ok(); |
|
confess dump($ok) unless $ok eq "+OK\r\n"; |
|
116 |
} |
} |
117 |
|
|
118 |
=head2 get |
=head2 get |
124 |
sub get { |
sub get { |
125 |
my ( $self, $k ) = @_; |
my ( $self, $k ) = @_; |
126 |
print $sock "GET $k\r\n"; |
print $sock "GET $k\r\n"; |
127 |
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; |
|
128 |
} |
} |
129 |
|
|
130 |
=head2 incr |
=head2 incr |
134 |
|
|
135 |
=cut |
=cut |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
sub incr { |
sub incr { |
140 |
my ( $self, $key, $value ) = @_; |
my ( $self, $key, $value ) = @_; |
141 |
if ( defined $value ) { |
if ( defined $value ) { |
143 |
} else { |
} else { |
144 |
print $sock "INCR $key\r\n"; |
print $sock "INCR $key\r\n"; |
145 |
} |
} |
146 |
my $count = <$sock>; |
_sock_result(); |
147 |
warn "# $key = $count"; |
} |
148 |
return $count; |
|
149 |
|
=head2 decr |
150 |
|
|
151 |
|
$r->decr('counter'); |
152 |
|
$r->decr('tripplets', 3); |
153 |
|
|
154 |
|
=cut |
155 |
|
|
156 |
|
sub decr { |
157 |
|
my ( $self, $key, $value ) = @_; |
158 |
|
if ( defined $value ) { |
159 |
|
print $sock "DECRBY $key $value\r\n"; |
160 |
|
} else { |
161 |
|
print $sock "DECR $key\r\n"; |
162 |
|
} |
163 |
|
_sock_result(); |
164 |
|
} |
165 |
|
|
166 |
|
=head2 exists |
167 |
|
|
168 |
|
$r->exists( 'key' ) && print "got key!"; |
169 |
|
|
170 |
|
=cut |
171 |
|
|
172 |
|
sub exists { |
173 |
|
my ( $self, $key ) = @_; |
174 |
|
print $sock "EXISTS $key\r\n"; |
175 |
|
_sock_result(); |
176 |
|
} |
177 |
|
|
178 |
|
=head2 del |
179 |
|
|
180 |
|
$r->del( 'key' ) || warn "key doesn't exist"; |
181 |
|
|
182 |
|
=cut |
183 |
|
|
184 |
|
sub del { |
185 |
|
my ( $self, $key ) = @_; |
186 |
|
print $sock "DEL $key\r\n"; |
187 |
|
_sock_result(); |
188 |
|
} |
189 |
|
|
190 |
|
=head2 type |
191 |
|
|
192 |
|
$r->type( 'key' ); # = string |
193 |
|
|
194 |
|
=cut |
195 |
|
|
196 |
|
sub type { |
197 |
|
my ( $self, $key ) = @_; |
198 |
|
print $sock "TYPE $key\r\n"; |
199 |
|
_sock_result(); |
200 |
|
} |
201 |
|
|
202 |
|
=head1 Commands operating on the key space |
203 |
|
|
204 |
|
=head2 keys |
205 |
|
|
206 |
|
my @keys = $r->keys( '*glob_pattern*' ); |
207 |
|
|
208 |
|
=cut |
209 |
|
|
210 |
|
sub keys { |
211 |
|
my ( $self, $glob ) = @_; |
212 |
|
print $sock "KEYS $glob\r\n"; |
213 |
|
return split(/\s/, _sock_result_bulk()); |
214 |
|
} |
215 |
|
|
216 |
|
=head2 randomkey |
217 |
|
|
218 |
|
my $key = $r->randomkey; |
219 |
|
|
220 |
|
=cut |
221 |
|
|
222 |
|
sub randomkey { |
223 |
|
my ( $self ) = @_; |
224 |
|
print $sock "RANDOMKEY\r\n"; |
225 |
|
_sock_result(); |
226 |
|
} |
227 |
|
|
228 |
|
=head2 rename |
229 |
|
|
230 |
|
my $ok = $r->rename( 'old-key', 'new-key', $new ); |
231 |
|
|
232 |
|
=cut |
233 |
|
|
234 |
|
sub rename { |
235 |
|
my ( $self, $old, $new, $nx ) = @_; |
236 |
|
print $sock "RENAME" . ( $nx ? 'NX' : '' ) . " $old $new\r\n"; |
237 |
|
_sock_ok(); |
238 |
} |
} |
239 |
|
|
240 |
=head1 AUTHOR |
=head1 AUTHOR |