/[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 11 by dpavlin, Sat Mar 21 23:09:48 2009 UTC revision 21 by dpavlin, Sun Mar 22 10:36:22 2009 UTC
# Line 58  sub _sock_result { Line 58  sub _sock_result {
58          return $result;          return $result;
59  }  }
60    
61    sub _sock_read_bulk {
62            my $len = <$sock>;
63            warn "## bulk len: ",dump($len);
64            return undef if $len eq "nil\r\n";
65            my $v;
66            read($sock, $v, $len) || die $!;
67            warn "## bulk v: ",dump($v);
68            my $crlf;
69            read($sock, $crlf, 2); # skip cr/lf
70            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, $command, $key, $value ) = @_;
101            print $sock "$command $key " . length($value) . "\r\n$value\r\n";
102            _sock_ok();
103    }
104    
105    
106  =head1 Connection Handling  =head1 Connection Handling
107    
108  =head2 quit  =head2 quit
# Line 93  sub ping { Line 138  sub ping {
138  =cut  =cut
139    
140  sub set {  sub set {
141          my ( $self, $k, $v, $new ) = @_;          my ( $self, $key, $value, $new ) = @_;
142          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";  
143  }  }
144    
145  =head2 get  =head2 get
# Line 106  sub set { Line 149  sub set {
149  =cut  =cut
150    
151  sub get {  sub get {
152          my ( $self, $k ) = @_;          my $self = shift;
153          print $sock "GET $k\r\n";          $self->_sock_result_bulk('GET', @_);
         my $len = <$sock>;  
 #       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;  
154  }  }
155    
156  =head2 incr  =head2 incr
# Line 129  sub get { Line 163  sub get {
163                    
164    
165  sub incr {  sub incr {
166          my ( $self, $key, $value ) = @_;          my $self = shift;
167          if ( defined $value ) {          $self->_sock_send( 'INCR' . ( $#_ ? 'BY' : '' ), @_ );
                 print $sock "INCRBY $key $value\r\n";  
         } else {  
                 print $sock "INCR $key\r\n";  
         }  
         _sock_result();  
168  }  }
169    
170  =head2 decr  =head2 decr
# Line 146  sub incr { Line 175  sub incr {
175  =cut  =cut
176    
177  sub decr {  sub decr {
178          my ( $self, $key, $value ) = @_;          my $self = shift;
179          if ( defined $value ) {          $self->_sock_send( 'DECR' . ( $#_ ? 'BY' : '' ), @_ );
                 print $sock "DECRBY $key $value\r\n";  
         } else {  
                 print $sock "DECR $key\r\n";  
         }  
         _sock_result();  
180  }  }
181    
182  =head2 exists  =head2 exists
# Line 163  sub decr { Line 187  sub decr {
187    
188  sub exists {  sub exists {
189          my ( $self, $key ) = @_;          my ( $self, $key ) = @_;
190          print $sock "EXISTS $key\r\n";          $self->_sock_send( 'EXISTS', $key );
         _sock_result();  
191  }  }
192    
193  =head2 del  =head2 del
# Line 175  sub exists { Line 198  sub exists {
198    
199  sub del {  sub del {
200          my ( $self, $key ) = @_;          my ( $self, $key ) = @_;
201          print $sock "DEL $key\r\n";          $self->_sock_send( 'DEL', $key );
         _sock_result();  
202  }  }
203    
204  =head2 type  =head2 type
# Line 187  sub del { Line 209  sub del {
209    
210  sub type {  sub type {
211          my ( $self, $key ) = @_;          my ( $self, $key ) = @_;
212          print $sock "type $key\r\n";          $self->_sock_send( 'TYPE', $key );
213          _sock_result();  }
214    
215    =head1 Commands operating on the key space
216    
217    =head2 keys
218    
219      my @keys = $r->keys( '*glob_pattern*' );
220    
221    =cut
222    
223    sub keys {
224            my ( $self, $glob ) = @_;
225            return split(/\s/, $self->_sock_result_bulk( 'KEYS', $glob ));
226    }
227    
228    =head2 randomkey
229    
230      my $key = $r->randomkey;
231    
232    =cut
233    
234    sub randomkey {
235            my ( $self ) = @_;
236            $self->_sock_send( 'RANDOMKEY' );
237    }
238    
239    =head2 rename
240    
241      my $ok = $r->rename( 'old-key', 'new-key', $new );
242    
243    =cut
244    
245    sub rename {
246            my ( $self, $old, $new, $nx ) = @_;
247            $self->_sock_send_ok( 'RENAME' . ( $nx ? 'NX' : '' ), $old, $new );
248    }
249    
250    =head2 dbsize
251    
252      my $nr_keys = $r->dbsize;
253    
254    =cut
255    
256    sub dbsize {
257            my ( $self ) = @_;
258            $self->_sock_send('DBSIZE');
259    }
260    
261    =head1 Commands operating on lists
262    
263    =head2 rpush
264    
265      $r->rpush( $key, $value );
266    
267    =cut
268    
269    sub rpush {
270            my ( $self, $key, $value ) = @_;
271            $self->_sock_send_bulk('RPUSH', $key, $value);
272    }
273    
274    =head2 lpush
275    
276      $r->lpush( $key, $value );
277    
278    =cut
279    
280    sub lpush {
281            my ( $self, $key, $value ) = @_;
282            $self->_sock_send_bulk('LPUSH', $key, $value);
283    }
284    
285    =head2 llen
286    
287      $r->llen( $key );
288    
289    =cut
290    
291    sub llen {
292            my ( $self, $key ) = @_;
293            $self->_sock_send( 'LLEN', $key );
294    }
295    
296    =head2 lrange
297    
298      my @list = $r->lrange( $key, $start, $end );
299    
300    =cut
301    
302    sub lrange {
303            my ( $self, $key, $start, $end ) = @_;
304            my $size = $self->_sock_send('LRANGE', $key, $start, $end);
305    
306            confess $size unless $size > 0;
307            $size--;
308    
309            my @list = ( 0 .. $size );
310            foreach ( 0 .. $size ) {
311                    $list[ $_ ] = _sock_read_bulk();
312            }
313    
314            warn "## lrange $key $start $end = [$size] ", dump( @list );
315            return @list;
316  }  }
317    
318  =head1 AUTHOR  =head1 AUTHOR

Legend:
Removed from v.11  
changed lines
  Added in v.21

  ViewVC Help
Powered by ViewVC 1.1.26