/[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 2 by dpavlin, Sat Mar 21 21:25:52 2009 UTC revision 15 by dpavlin, Sat Mar 21 23:39:20 2009 UTC
# Line 5  use strict; Line 5  use strict;
5    
6  use IO::Socket::INET;  use IO::Socket::INET;
7  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
8    use Carp qw/confess/;
9    
10  =head1 NAME  =head1 NAME
11    
# Line 50  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  =head1 Connection Handling  =head1 Connection Handling
79    
80  =head2 quit  =head2 quit
# Line 66  sub quit { Line 91  sub quit {
91    
92  =head2 ping  =head2 ping
93    
94          $r->ping || die "no server?";    $r->ping || die "no server?";
95    
96  =cut  =cut
97    
# Line 76  sub ping { Line 101  sub ping {
101          die "ping failed, got ", dump($pong) unless $pong eq "+PONG\r\n";          die "ping failed, got ", dump($pong) unless $pong eq "+PONG\r\n";
102  }  }
103    
104    =head1 Commands operating on string values
105    
106    =head2 set
107    
108      $r->set( foo => 'bar', $new );
109    
110    =cut
111    
112    sub set {
113            my ( $self, $k, $v, $new ) = @_;
114            print $sock "SET" . ( $new ? 'NX' : '' ) . " $k " . length($v) . "\r\n$v\r\n";
115            _sock_ok();
116    }
117    
118    =head2 get
119    
120      my $value = $r->get( 'foo' );
121    
122    =cut
123    
124    sub get {
125            my ( $self, $k ) = @_;
126            print $sock "GET $k\r\n";
127            _sock_result_bulk();
128    }
129    
130    =head2 incr
131    
132      $r->incr('counter');
133      $r->incr('tripplets', 3);
134    
135    =cut
136    
137            
138    
139    sub incr {
140            my ( $self, $key, $value ) = @_;
141            if ( defined $value ) {
142                    print $sock "INCRBY $key $value\r\n";
143            } else {
144                    print $sock "INCR $key\r\n";
145            }
146            _sock_result();
147    }
148    
149    =head2 decr
150    
151      $r->decr('counter');
152      $r->decr('tripplets', 3);
153    
154    =cut
155    
156    sub decr {
157            my ( $self, $key, $value ) = @_;
158            if ( defined $value ) {
159                    print $sock "DECRBY $key $value\r\n";
160            } else {
161                    print $sock "DECR $key\r\n";
162            }
163            _sock_result();
164    }
165    
166    =head2 exists
167    
168      $r->exists( 'key' ) && print "got key!";
169    
170    =cut
171    
172    sub exists {
173            my ( $self, $key ) = @_;
174            print $sock "EXISTS $key\r\n";
175            _sock_result();
176    }
177    
178    =head2 del
179    
180      $r->del( 'key' ) || warn "key doesn't exist";
181    
182    =cut
183    
184    sub del {
185            my ( $self, $key ) = @_;
186            print $sock "DEL $key\r\n";
187            _sock_result();
188    }
189    
190    =head2 type
191    
192      $r->type( 'key' ); # = string
193    
194    =cut
195    
196    sub type {
197            my ( $self, $key ) = @_;
198            print $sock "TYPE $key\r\n";
199            _sock_result();
200    }
201    
202    =head1 Commands operating on the key space
203    
204    =head2 keys
205    
206      my @keys = $r->keys( '*glob_pattern*' );
207    
208    =cut
209    
210    sub keys {
211            my ( $self, $glob ) = @_;
212            print $sock "KEYS $glob\r\n";
213            return split(/\s/, _sock_result_bulk());
214    }
215    
216    =head2 randomkey
217    
218      my $key = $r->randomkey;
219    
220    =cut
221    
222    sub randomkey {
223            my ( $self ) = @_;
224            print $sock "RANDOMKEY\r\n";
225            _sock_result();
226    }
227    
228    =head2 rename
229    
230      my $ok = $r->rename( 'old-key', 'new-key', $new );
231    
232    =cut
233    
234    sub rename {
235            my ( $self, $old, $new, $nx ) = @_;
236            print $sock "RENAME" . ( $nx ? 'NX' : '' ) . " $old $new\r\n";
237            _sock_ok();
238    }
239    
240  =head1 AUTHOR  =head1 AUTHOR
241    
242  Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>  Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>

Legend:
Removed from v.2  
changed lines
  Added in v.15

  ViewVC Help
Powered by ViewVC 1.1.26