/[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 20 by dpavlin, Sun Mar 22 09:51:34 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_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 {  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";
# Line 77  sub _sock_ok { Line 84  sub _sock_ok {
84    
85  sub _sock_send {  sub _sock_send {
86          my $self = shift;          my $self = shift;
87            warn "## _sock_send ",dump( @_ );
88          print $sock join(' ',@_) . "\r\n";          print $sock join(' ',@_) . "\r\n";
89          _sock_result();          _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 {  sub _sock_send_bulk {
100          my ( $self, $command, $key, $value ) = @_;          my ( $self, $command, $key, $value ) = @_;
101          print $sock "$command $key " . length($value) . "\r\n$value\r\n";          print $sock "$command $key " . length($value) . "\r\n$value\r\n";
# Line 134  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', @_);
         _sock_result_bulk();  
154  }  }
155    
156  =head2 incr  =head2 incr
# Line 149  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 166  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 183  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 195  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 207  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 );
         _sock_result();  
213  }  }
214    
215  =head1 Commands operating on the key space  =head1 Commands operating on the key space
# Line 221  sub type { Line 222  sub type {
222    
223  sub keys {  sub keys {
224          my ( $self, $glob ) = @_;          my ( $self, $glob ) = @_;
225          print $sock "KEYS $glob\r\n";          return split(/\s/, $self->_sock_result_bulk( 'KEYS', $glob ));
         return split(/\s/, _sock_result_bulk());  
226  }  }
227    
228  =head2 randomkey  =head2 randomkey
# Line 233  sub keys { Line 233  sub keys {
233    
234  sub randomkey {  sub randomkey {
235          my ( $self ) = @_;          my ( $self ) = @_;
236          print $sock "RANDOMKEY\r\n";          $self->_sock_send( 'RANDOMKEY' );
         _sock_result();  
237  }  }
238    
239  =head2 rename  =head2 rename
# Line 245  sub randomkey { Line 244  sub randomkey {
244    
245  sub rename {  sub rename {
246          my ( $self, $old, $new, $nx ) = @_;          my ( $self, $old, $new, $nx ) = @_;
247          print $sock "RENAME" . ( $nx ? 'NX' : '' ) . " $old $new\r\n";          $self->_sock_send_ok( 'RENAME' . ( $nx ? 'NX' : '' ), $old, $new );
         _sock_ok();  
248  }  }
249    
250  =head2 dbsize  =head2 dbsize
# Line 257  sub rename { Line 255  sub rename {
255    
256  sub dbsize {  sub dbsize {
257          my ( $self ) = @_;          my ( $self ) = @_;
258          print $sock "DBSIZE\r\n";          $self->_sock_send('DBSIZE');
         _sock_result();  
259  }  }
260    
261  =head1 Commands operating on lists  =head1 Commands operating on lists
# Line 293  sub lpush { Line 290  sub lpush {
290    
291  sub llen {  sub llen {
292          my ( $self, $key ) = @_;          my ( $self, $key ) = @_;
293          $self->_sock_send( 'llen', $key );          $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.20  
changed lines
  Added in v.21

  ViewVC Help
Powered by ViewVC 1.1.26