/[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 19 by dpavlin, Sun Mar 22 09:46:14 2009 UTC revision 26 by dpavlin, Sun Mar 22 13:37:49 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_ok {  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>;          my $ok = <$sock>;
82          confess dump($ok) unless $ok eq "+OK\r\n";          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 {  sub _sock_send_bulk {
107          my ( $self, $command, $key, $value ) = @_;          __sock_send_bulk_raw( @_ );
108          print $sock "$command $key " . length($value) . "\r\n$value\r\n";          __sock_ok();
         _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    
# Line 128  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 143  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 160  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 177  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 189  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 201  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 215  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 ));
         return split(/\s/, _sock_result_bulk());  
238  }  }
239    
240  =head2 randomkey  =head2 randomkey
# Line 227  sub keys { Line 245  sub keys {
245    
246  sub randomkey {  sub randomkey {
247          my ( $self ) = @_;          my ( $self ) = @_;
248          print $sock "RANDOMKEY\r\n";          $self->_sock_send( 'RANDOMKEY' );
         _sock_result();  
249  }  }
250    
251  =head2 rename  =head2 rename
# Line 239  sub randomkey { Line 256  sub randomkey {
256    
257  sub rename {  sub rename {
258          my ( $self, $old, $new, $nx ) = @_;          my ( $self, $old, $new, $nx ) = @_;
259          print $sock "RENAME" . ( $nx ? 'NX' : '' ) . " $old $new\r\n";          $self->_sock_send_ok( 'RENAME' . ( $nx ? 'NX' : '' ), $old, $new );
         _sock_ok();  
260  }  }
261    
262  =head2 dbsize  =head2 dbsize
# Line 251  sub rename { Line 267  sub rename {
267    
268  sub dbsize {  sub dbsize {
269          my ( $self ) = @_;          my ( $self ) = @_;
270          print $sock "DBSIZE\r\n";          $self->_sock_send('DBSIZE');
         _sock_result();  
271  }  }
272    
273  =head1 Commands operating on lists  =head1 Commands operating on lists
# Line 279  sub lpush { Line 294  sub lpush {
294          $self->_sock_send_bulk('LPUSH', $key, $value);          $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      $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  =head1 AUTHOR  =head1 AUTHOR
375    
376  Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>  Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>

Legend:
Removed from v.19  
changed lines
  Added in v.26

  ViewVC Help
Powered by ViewVC 1.1.26