/[Redis.pre-github]/lib/Redis.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/Redis.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 12 by dpavlin, Sat Mar 21 23:23:37 2009 UTC revision 27 by dpavlin, Sun Mar 22 13:44:19 2009 UTC
# Line 58  sub _sock_result { Line 58  sub _sock_result {
58          return $result;          return $result;
59  }  }
60    
61  sub _sock_result_bulk {  sub _sock_read_bulk {
62          my $len = <$sock>;          my $len = <$sock>;
63          warn "# len: ",dump($len);          warn "## bulk len: ",dump($len);
64          return undef if $len eq "nil\r\n";          return undef if $len eq "nil\r\n";
65          my $v;          my $v;
66          read($sock, $v, $len) || die $!;          read($sock, $v, $len) || die $!;
67          warn "# v: ",dump($v);          warn "## bulk v: ",dump($v);
68          my $crlf;          my $crlf;
69          read($sock, $crlf, 2); # skip cr/lf          read($sock, $crlf, 2); # skip cr/lf
70          return $v;          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_raw {
100            my $self = shift;
101            warn "## _sock_send_bulk ",dump( @_ );
102            my $value = pop;
103            print $sock join(' ',@_) . ' ' . length($value) . "\r\n$value\r\n";
104    }
105    
106    sub _sock_send_bulk {
107            __sock_send_bulk_raw( @_ );
108            __sock_ok();
109    }
110    
111    sub _sock_send_bulk_number {
112            __sock_send_bulk_raw( @_ );
113            my $v = _sock_result();
114            confess $v unless $v =~ m{^\-?\d+$};
115            return $v;
116    }
117    
118  =head1 Connection Handling  =head1 Connection Handling
119    
120  =head2 quit  =head2 quit
# Line 105  sub ping { Line 150  sub ping {
150  =cut  =cut
151    
152  sub set {  sub set {
153          my ( $self, $k, $v, $new ) = @_;          my ( $self, $key, $value, $new ) = @_;
154          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";  
155  }  }
156    
157  =head2 get  =head2 get
# Line 118  sub set { Line 161  sub set {
161  =cut  =cut
162    
163  sub get {  sub get {
164          my ( $self, $k ) = @_;          my $self = shift;
165          print $sock "GET $k\r\n";          $self->_sock_result_bulk('GET', @_);
         _sock_result_bulk();  
166  }  }
167    
168  =head2 incr  =head2 incr
# Line 133  sub get { Line 175  sub get {
175                    
176    
177  sub incr {  sub incr {
178          my ( $self, $key, $value ) = @_;          my $self = shift;
179          if ( defined $value ) {          $self->_sock_send( 'INCR' . ( $#_ ? 'BY' : '' ), @_ );
                 print $sock "INCRBY $key $value\r\n";  
         } else {  
                 print $sock "INCR $key\r\n";  
         }  
         _sock_result();  
180  }  }
181    
182  =head2 decr  =head2 decr
# Line 150  sub incr { Line 187  sub incr {
187  =cut  =cut
188    
189  sub decr {  sub decr {
190          my ( $self, $key, $value ) = @_;          my $self = shift;
191          if ( defined $value ) {          $self->_sock_send( 'DECR' . ( $#_ ? 'BY' : '' ), @_ );
                 print $sock "DECRBY $key $value\r\n";  
         } else {  
                 print $sock "DECR $key\r\n";  
         }  
         _sock_result();  
192  }  }
193    
194  =head2 exists  =head2 exists
# Line 167  sub decr { Line 199  sub decr {
199    
200  sub exists {  sub exists {
201          my ( $self, $key ) = @_;          my ( $self, $key ) = @_;
202          print $sock "EXISTS $key\r\n";          $self->_sock_send( 'EXISTS', $key );
         _sock_result();  
203  }  }
204    
205  =head2 del  =head2 del
# Line 179  sub exists { Line 210  sub exists {
210    
211  sub del {  sub del {
212          my ( $self, $key ) = @_;          my ( $self, $key ) = @_;
213          print $sock "DEL $key\r\n";          $self->_sock_send( 'DEL', $key );
         _sock_result();  
214  }  }
215    
216  =head2 type  =head2 type
# Line 191  sub del { Line 221  sub del {
221    
222  sub type {  sub type {
223          my ( $self, $key ) = @_;          my ( $self, $key ) = @_;
224          print $sock "TYPE $key\r\n";          $self->_sock_send( 'TYPE', $key );
         _sock_result();  
225  }  }
226    
227  =head1 Commands operating on the key space  =head1 Commands operating on the key space
# Line 205  sub type { Line 234  sub type {
234    
235  sub keys {  sub keys {
236          my ( $self, $glob ) = @_;          my ( $self, $glob ) = @_;
237          print $sock "KEYS $glob\r\n";          return split(/\s/, $self->_sock_result_bulk( 'KEYS', $glob ));
238          return split(/\s/, _sock_result_bulk());  }
239    
240    =head2 randomkey
241    
242      my $key = $r->randomkey;
243    
244    =cut
245    
246    sub randomkey {
247            my ( $self ) = @_;
248            $self->_sock_send( 'RANDOMKEY' );
249    }
250    
251    =head2 rename
252    
253      my $ok = $r->rename( 'old-key', 'new-key', $new );
254    
255    =cut
256    
257    sub rename {
258            my ( $self, $old, $new, $nx ) = @_;
259            $self->_sock_send_ok( 'RENAME' . ( $nx ? 'NX' : '' ), $old, $new );
260    }
261    
262    =head2 dbsize
263    
264      my $nr_keys = $r->dbsize;
265    
266    =cut
267    
268    sub dbsize {
269            my ( $self ) = @_;
270            $self->_sock_send('DBSIZE');
271    }
272    
273    =head1 Commands operating on lists
274    
275    =head2 rpush
276    
277      $r->rpush( $key, $value );
278    
279    =cut
280    
281    sub rpush {
282            my ( $self, $key, $value ) = @_;
283            $self->_sock_send_bulk('RPUSH', $key, $value);
284    }
285    
286    =head2 lpush
287    
288      $r->lpush( $key, $value );
289    
290    =cut
291    
292    sub lpush {
293            my ( $self, $key, $value ) = @_;
294            $self->_sock_send_bulk('LPUSH', $key, $value);
295    }
296    
297    =head2 llen
298    
299      $r->llen( $key );
300    
301    =cut
302    
303    sub llen {
304            my ( $self, $key ) = @_;
305            $self->_sock_send( 'LLEN', $key );
306    }
307    
308    =head2 lrange
309    
310      my @list = $r->lrange( $key, $start, $end );
311    
312    =cut
313    
314    sub lrange {
315            my ( $self, $key, $start, $end ) = @_;
316            my $size = $self->_sock_send('LRANGE', $key, $start, $end);
317    
318            confess $size unless $size > 0;
319            $size--;
320    
321            my @list = ( 0 .. $size );
322            foreach ( 0 .. $size ) {
323                    $list[ $_ ] = _sock_read_bulk();
324            }
325    
326            warn "## lrange $key $start $end = [$size] ", dump( @list );
327            return @list;
328    }
329    
330    =head2 ltrim
331    
332      my $ok = $r->ltrim( $key, $start, $end );
333    
334    =cut
335    
336    sub ltrim {
337            my ( $self, $key, $start, $end ) = @_;
338            $self->_sock_send_ok( 'LTRIM', $key, $start, $end );
339    }
340    
341    =head2 lindex
342    
343      $r->lindex( $key, $index );
344    
345    =cut
346    
347    sub lindex {
348            my ( $self, $key, $index ) = @_;
349            $self->_sock_result_bulk( 'LINDEX', $key, $index );
350    }
351    
352    =head2 lset
353    
354      $r->lset( $key, $index, $value );
355    
356    =cut
357    
358    sub lset {
359            my ( $self, $key, $index, $value ) = @_;
360            $self->_sock_send_bulk( 'LSET', $key, $index, $value );
361    }
362    
363    =head2 lrem
364    
365      my $modified_count = $r->lrem( $key, $count, $value );
366    
367    =cut
368    
369    sub lrem {
370            my ( $self, $key, $count, $value ) = @_;
371            $self->_sock_send_bulk_number( 'LREM', $key, $count, $value );
372    }
373    
374    =head2 lpop
375    
376      my $value = $r->lpop( $key );
377    
378    =cut
379    
380    sub lpop {
381            my ( $self, $key ) = @_;
382            $self->_sock_result_bulk( 'lpop', $key );
383    }
384    
385    =head2 rpop
386    
387      my $value = $r->rpop( $key );
388    
389    =cut
390    
391    sub rpop {
392            my ( $self, $key ) = @_;
393            $self->_sock_result_bulk( 'rpop', $key );
394  }  }
395    
396  =head1 AUTHOR  =head1 AUTHOR

Legend:
Removed from v.12  
changed lines
  Added in v.27

  ViewVC Help
Powered by ViewVC 1.1.26