/[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 10 by dpavlin, Sat Mar 21 23:05:02 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 66  sub quit { Line 67  sub quit {
67    
68  =head2 ping  =head2 ping
69    
70          $r->ping || die "no server?";    $r->ping || die "no server?";
71    
72  =cut  =cut
73    
# Line 76  sub ping { Line 77  sub ping {
77          die "ping failed, got ", dump($pong) unless $pong eq "+PONG\r\n";          die "ping failed, got ", dump($pong) unless $pong eq "+PONG\r\n";
78  }  }
79    
80    =head1 Commands operating on string values
81    
82    =head2 set
83    
84      $r->set( foo => 'bar', $new );
85    
86    =cut
87    
88    sub set {
89            my ( $self, $k, $v, $new ) = @_;
90            print $sock ( $new ? "SETNX" : "SET" ) . " $k " . length($v) . "\r\n$v\r\n";
91            my $ok = <$sock>;
92            confess dump($ok) unless $ok eq "+OK\r\n";
93    }
94    
95    =head2 get
96    
97      my $value = $r->get( 'foo' );
98    
99    =cut
100    
101    sub get {
102            my ( $self, $k ) = @_;
103            print $sock "GET $k\r\n";
104            my $len = <$sock>;
105    #       warn "# len: ",dump($len);
106            return undef if $len eq "nil\r\n";
107            my $v;
108            read($sock, $v, $len) || die $!;
109    #       warn "# v: ",dump($v);
110            my $crlf;
111            read($sock, $crlf, 2); # skip cr/lf
112            return $v;
113    }
114    
115    =head2 incr
116    
117      $r->incr('counter');
118      $r->incr('tripplets', 3);
119    
120    =cut
121    
122    sub sock_result {
123            my $result = <$sock>;
124            warn "# result: ",dump( $result );
125            $result =~ s{\r\n$}{} || warn "can't find cr/lf";
126            return $result;
127    }
128            
129    
130    sub incr {
131            my ( $self, $key, $value ) = @_;
132            if ( defined $value ) {
133                    print $sock "INCRBY $key $value\r\n";
134            } else {
135                    print $sock "INCR $key\r\n";
136            }
137            sock_result();
138    }
139    
140    =head2 decr
141    
142      $r->decr('counter');
143      $r->decr('tripplets', 3);
144    
145    =cut
146    
147    sub decr {
148            my ( $self, $key, $value ) = @_;
149            if ( defined $value ) {
150                    print $sock "DECRBY $key $value\r\n";
151            } else {
152                    print $sock "DECR $key\r\n";
153            }
154            sock_result();
155    }
156    
157    =head2 exists
158    
159      $r->exists( 'key' ) && print "got key!";
160    
161    =cut
162    
163    sub exists {
164            my ( $self, $key ) = @_;
165            print $sock "EXISTS $key\r\n";
166            sock_result();
167    }
168    
169    =head2 del
170    
171      $r->del( 'key' ) || warn "key doesn't exist";
172    
173    =cut
174    
175    sub del {
176            my ( $self, $key ) = @_;
177            print $sock "DEL $key\r\n";
178            sock_result();
179    }
180    
181  =head1 AUTHOR  =head1 AUTHOR
182    
183  Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>  Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>

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

  ViewVC Help
Powered by ViewVC 1.1.26