/[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 8 by dpavlin, Sat Mar 21 22:48:46 2009 UTC revision 19 by dpavlin, Sun Mar 22 09:46:14 2009 UTC
# Line 51  sub new { Line 51  sub new {
51          $self;          $self;
52  }  }
53    
54    sub _sock_result {
55            my $result = <$sock>;
56            warn "# result: ",dump( $result );
57            $result =~ s{\r\n$}{} || warn "can't find cr/lf";
58            return $result;
59    }
60    
61    sub _sock_result_bulk {
62            my $len = <$sock>;
63            warn "# len: ",dump($len);
64            return undef if $len eq "nil\r\n";
65            my $v;
66            read($sock, $v, $len) || die $!;
67            warn "# v: ",dump($v);
68            my $crlf;
69            read($sock, $crlf, 2); # skip cr/lf
70            return $v;
71    }
72    
73    sub _sock_ok {
74            my $ok = <$sock>;
75            confess dump($ok) unless $ok eq "+OK\r\n";
76    }
77    
78    sub _sock_send_bulk {
79            my ( $self, $command, $key, $value ) = @_;
80            print $sock "$command $key " . length($value) . "\r\n$value\r\n";
81            _sock_ok();
82    }
83    
84    
85  =head1 Connection Handling  =head1 Connection Handling
86    
87  =head2 quit  =head2 quit
# Line 86  sub ping { Line 117  sub ping {
117  =cut  =cut
118    
119  sub set {  sub set {
120          my ( $self, $k, $v, $new ) = @_;          my ( $self, $key, $value, $new ) = @_;
121          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";  
122  }  }
123    
124  =head2 get  =head2 get
# Line 101  sub set { Line 130  sub set {
130  sub get {  sub get {
131          my ( $self, $k ) = @_;          my ( $self, $k ) = @_;
132          print $sock "GET $k\r\n";          print $sock "GET $k\r\n";
133          my $len = <$sock>;          _sock_result_bulk();
 #       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;  
134  }  }
135    
136  =head2 incr  =head2 incr
# Line 119  sub get { Line 140  sub get {
140    
141  =cut  =cut
142    
143            
144    
145  sub incr {  sub incr {
146          my ( $self, $key, $value ) = @_;          my ( $self, $key, $value ) = @_;
147          if ( defined $value ) {          if ( defined $value ) {
# Line 126  sub incr { Line 149  sub incr {
149          } else {          } else {
150                  print $sock "INCR $key\r\n";                  print $sock "INCR $key\r\n";
151          }          }
152          my $count = <$sock>;          _sock_result();
         warn "# $key = $count";  
         return $count;  
153  }  }
154    
155  =head2 decr  =head2 decr
# Line 145  sub decr { Line 166  sub decr {
166          } else {          } else {
167                  print $sock "DECR $key\r\n";                  print $sock "DECR $key\r\n";
168          }          }
169          my $count = <$sock>;          _sock_result();
170          warn "# $key = $count";  }
171          return $count;  
172    =head2 exists
173    
174      $r->exists( 'key' ) && print "got key!";
175    
176    =cut
177    
178    sub exists {
179            my ( $self, $key ) = @_;
180            print $sock "EXISTS $key\r\n";
181            _sock_result();
182    }
183    
184    =head2 del
185    
186      $r->del( 'key' ) || warn "key doesn't exist";
187    
188    =cut
189    
190    sub del {
191            my ( $self, $key ) = @_;
192            print $sock "DEL $key\r\n";
193            _sock_result();
194    }
195    
196    =head2 type
197    
198      $r->type( 'key' ); # = string
199    
200    =cut
201    
202    sub type {
203            my ( $self, $key ) = @_;
204            print $sock "TYPE $key\r\n";
205            _sock_result();
206    }
207    
208    =head1 Commands operating on the key space
209    
210    =head2 keys
211    
212      my @keys = $r->keys( '*glob_pattern*' );
213    
214    =cut
215    
216    sub keys {
217            my ( $self, $glob ) = @_;
218            print $sock "KEYS $glob\r\n";
219            return split(/\s/, _sock_result_bulk());
220    }
221    
222    =head2 randomkey
223    
224      my $key = $r->randomkey;
225    
226    =cut
227    
228    sub randomkey {
229            my ( $self ) = @_;
230            print $sock "RANDOMKEY\r\n";
231            _sock_result();
232    }
233    
234    =head2 rename
235    
236      my $ok = $r->rename( 'old-key', 'new-key', $new );
237    
238    =cut
239    
240    sub rename {
241            my ( $self, $old, $new, $nx ) = @_;
242            print $sock "RENAME" . ( $nx ? 'NX' : '' ) . " $old $new\r\n";
243            _sock_ok();
244    }
245    
246    =head2 dbsize
247    
248      my $nr_keys = $r->dbsize;
249    
250    =cut
251    
252    sub dbsize {
253            my ( $self ) = @_;
254            print $sock "DBSIZE\r\n";
255            _sock_result();
256    }
257    
258    =head1 Commands operating on lists
259    
260    =head2 rpush
261    
262      $r->rpush( $key, $value );
263    
264    =cut
265    
266    sub rpush {
267            my ( $self, $key, $value ) = @_;
268            $self->_sock_send_bulk('RPUSH', $key, $value);
269    }
270    
271    =head2 lpush
272    
273      $r->lpush( $key, $value );
274    
275    =cut
276    
277    sub lpush {
278            my ( $self, $key, $value ) = @_;
279            $self->_sock_send_bulk('LPUSH', $key, $value);
280  }  }
281    
282  =head1 AUTHOR  =head1 AUTHOR

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

  ViewVC Help
Powered by ViewVC 1.1.26