/[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 24 by dpavlin, Sun Mar 22 13:18:11 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 {
100            my $self = shift;
101            my $value = pop;
102            print $sock join(' ',@_) . ' ' . length($value) . "\r\n$value\r\n";
103            _sock_ok();
104    }
105    
106    
107  =head1 Connection Handling  =head1 Connection Handling
108    
109  =head2 quit  =head2 quit
# Line 105  sub ping { Line 139  sub ping {
139  =cut  =cut
140    
141  sub set {  sub set {
142          my ( $self, $k, $v, $new ) = @_;          my ( $self, $key, $value, $new ) = @_;
143          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";  
144  }  }
145    
146  =head2 get  =head2 get
# Line 118  sub set { Line 150  sub set {
150  =cut  =cut
151    
152  sub get {  sub get {
153          my ( $self, $k ) = @_;          my $self = shift;
154          print $sock "GET $k\r\n";          $self->_sock_result_bulk('GET', @_);
         _sock_result_bulk();  
155  }  }
156    
157  =head2 incr  =head2 incr
# Line 133  sub get { Line 164  sub get {
164                    
165    
166  sub incr {  sub incr {
167          my ( $self, $key, $value ) = @_;          my $self = shift;
168          if ( defined $value ) {          $self->_sock_send( 'INCR' . ( $#_ ? 'BY' : '' ), @_ );
                 print $sock "INCRBY $key $value\r\n";  
         } else {  
                 print $sock "INCR $key\r\n";  
         }  
         _sock_result();  
169  }  }
170    
171  =head2 decr  =head2 decr
# Line 150  sub incr { Line 176  sub incr {
176  =cut  =cut
177    
178  sub decr {  sub decr {
179          my ( $self, $key, $value ) = @_;          my $self = shift;
180          if ( defined $value ) {          $self->_sock_send( 'DECR' . ( $#_ ? 'BY' : '' ), @_ );
                 print $sock "DECRBY $key $value\r\n";  
         } else {  
                 print $sock "DECR $key\r\n";  
         }  
         _sock_result();  
181  }  }
182    
183  =head2 exists  =head2 exists
# Line 167  sub decr { Line 188  sub decr {
188    
189  sub exists {  sub exists {
190          my ( $self, $key ) = @_;          my ( $self, $key ) = @_;
191          print $sock "EXISTS $key\r\n";          $self->_sock_send( 'EXISTS', $key );
         _sock_result();  
192  }  }
193    
194  =head2 del  =head2 del
# Line 179  sub exists { Line 199  sub exists {
199    
200  sub del {  sub del {
201          my ( $self, $key ) = @_;          my ( $self, $key ) = @_;
202          print $sock "DEL $key\r\n";          $self->_sock_send( 'DEL', $key );
         _sock_result();  
203  }  }
204    
205  =head2 type  =head2 type
# Line 191  sub del { Line 210  sub del {
210    
211  sub type {  sub type {
212          my ( $self, $key ) = @_;          my ( $self, $key ) = @_;
213          print $sock "TYPE $key\r\n";          $self->_sock_send( 'TYPE', $key );
         _sock_result();  
214  }  }
215    
216  =head1 Commands operating on the key space  =head1 Commands operating on the key space
# Line 205  sub type { Line 223  sub type {
223    
224  sub keys {  sub keys {
225          my ( $self, $glob ) = @_;          my ( $self, $glob ) = @_;
226          print $sock "KEYS $glob\r\n";          return split(/\s/, $self->_sock_result_bulk( 'KEYS', $glob ));
227          return split(/\s/, _sock_result_bulk());  }
228    
229    =head2 randomkey
230    
231      my $key = $r->randomkey;
232    
233    =cut
234    
235    sub randomkey {
236            my ( $self ) = @_;
237            $self->_sock_send( 'RANDOMKEY' );
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            $self->_sock_send_ok( 'RENAME' . ( $nx ? 'NX' : '' ), $old, $new );
249    }
250    
251    =head2 dbsize
252    
253      my $nr_keys = $r->dbsize;
254    
255    =cut
256    
257    sub dbsize {
258            my ( $self ) = @_;
259            $self->_sock_send('DBSIZE');
260    }
261    
262    =head1 Commands operating on lists
263    
264    =head2 rpush
265    
266      $r->rpush( $key, $value );
267    
268    =cut
269    
270    sub rpush {
271            my ( $self, $key, $value ) = @_;
272            $self->_sock_send_bulk('RPUSH', $key, $value);
273    }
274    
275    =head2 lpush
276    
277      $r->lpush( $key, $value );
278    
279    =cut
280    
281    sub lpush {
282            my ( $self, $key, $value ) = @_;
283            $self->_sock_send_bulk('LPUSH', $key, $value);
284    }
285    
286    =head2 llen
287    
288      $r->llen( $key );
289    
290    =cut
291    
292    sub llen {
293            my ( $self, $key ) = @_;
294            $self->_sock_send( 'LLEN', $key );
295    }
296    
297    =head2 lrange
298    
299      my @list = $r->lrange( $key, $start, $end );
300    
301    =cut
302    
303    sub lrange {
304            my ( $self, $key, $start, $end ) = @_;
305            my $size = $self->_sock_send('LRANGE', $key, $start, $end);
306    
307            confess $size unless $size > 0;
308            $size--;
309    
310            my @list = ( 0 .. $size );
311            foreach ( 0 .. $size ) {
312                    $list[ $_ ] = _sock_read_bulk();
313            }
314    
315            warn "## lrange $key $start $end = [$size] ", dump( @list );
316            return @list;
317    }
318    
319    =head2 ltrim
320    
321      my $ok = $r->ltrim( $key, $start, $end );
322    
323    =cut
324    
325    sub ltrim {
326            my ( $self, $key, $start, $end ) = @_;
327            $self->_sock_send_ok( 'LTRIM', $key, $start, $end );
328    }
329    
330    =head2 lindex
331    
332      $r->lindex( $key, $index );
333    
334    =cut
335    
336    sub lindex {
337            my ( $self, $key, $index ) = @_;
338            $self->_sock_result_bulk( 'LINDEX', $key, $index );
339    }
340    
341    =head2 lset
342    
343      $r->lset( $key, $index, $value );
344    
345    =cut
346    
347    sub lset {
348            my ( $self, $key, $index, $value ) = @_;
349            $self->_sock_send_bulk( 'LSET', $key, $index, $value );
350  }  }
351    
352  =head1 AUTHOR  =head1 AUTHOR

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

  ViewVC Help
Powered by ViewVC 1.1.26