/[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 13 by dpavlin, Sat Mar 21 23:26:46 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  =head1 Connection Handling  =head1 Connection Handling
74    
75  =head2 quit  =head2 quit
# Line 66  sub quit { Line 86  sub quit {
86    
87  =head2 ping  =head2 ping
88    
89          $r->ping || die "no server?";    $r->ping || die "no server?";
90    
91  =cut  =cut
92    
# Line 76  sub ping { Line 96  sub ping {
96          die "ping failed, got ", dump($pong) unless $pong eq "+PONG\r\n";          die "ping failed, got ", dump($pong) unless $pong eq "+PONG\r\n";
97  }  }
98    
99    =head1 Commands operating on string values
100    
101    =head2 set
102    
103      $r->set( foo => 'bar', $new );
104    
105    =cut
106    
107    sub set {
108            my ( $self, $k, $v, $new ) = @_;
109            print $sock ( $new ? "SETNX" : "SET" ) . " $k " . length($v) . "\r\n$v\r\n";
110            my $ok = <$sock>;
111            confess dump($ok) unless $ok eq "+OK\r\n";
112    }
113    
114    =head2 get
115    
116      my $value = $r->get( 'foo' );
117    
118    =cut
119    
120    sub get {
121            my ( $self, $k ) = @_;
122            print $sock "GET $k\r\n";
123            _sock_result_bulk();
124    }
125    
126    =head2 incr
127    
128      $r->incr('counter');
129      $r->incr('tripplets', 3);
130    
131    =cut
132    
133            
134    
135    sub incr {
136            my ( $self, $key, $value ) = @_;
137            if ( defined $value ) {
138                    print $sock "INCRBY $key $value\r\n";
139            } else {
140                    print $sock "INCR $key\r\n";
141            }
142            _sock_result();
143    }
144    
145    =head2 decr
146    
147      $r->decr('counter');
148      $r->decr('tripplets', 3);
149    
150    =cut
151    
152    sub decr {
153            my ( $self, $key, $value ) = @_;
154            if ( defined $value ) {
155                    print $sock "DECRBY $key $value\r\n";
156            } else {
157                    print $sock "DECR $key\r\n";
158            }
159            _sock_result();
160    }
161    
162    =head2 exists
163    
164      $r->exists( 'key' ) && print "got key!";
165    
166    =cut
167    
168    sub exists {
169            my ( $self, $key ) = @_;
170            print $sock "EXISTS $key\r\n";
171            _sock_result();
172    }
173    
174    =head2 del
175    
176      $r->del( 'key' ) || warn "key doesn't exist";
177    
178    =cut
179    
180    sub del {
181            my ( $self, $key ) = @_;
182            print $sock "DEL $key\r\n";
183            _sock_result();
184    }
185    
186    =head2 type
187    
188      $r->type( 'key' ); # = string
189    
190    =cut
191    
192    sub type {
193            my ( $self, $key ) = @_;
194            print $sock "TYPE $key\r\n";
195            _sock_result();
196    }
197    
198    =head1 Commands operating on the key space
199    
200    =head2 keys
201    
202      my @keys = $r->keys( '*glob_pattern*' );
203    
204    =cut
205    
206    sub keys {
207            my ( $self, $glob ) = @_;
208            print $sock "KEYS $glob\r\n";
209            return split(/\s/, _sock_result_bulk());
210    }
211    
212    =head2 randomkey
213    
214      my $key = $r->randomkey;
215    
216    =cut
217    
218    sub randomkey {
219            my ( $self, $glob ) = @_;
220            print $sock "RANDOMKEY\r\n";
221            _sock_result();
222    }
223    
224  =head1 AUTHOR  =head1 AUTHOR
225    
226  Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>  Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>

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

  ViewVC Help
Powered by ViewVC 1.1.26