/[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 3 by dpavlin, Sat Mar 21 21:40:53 2009 UTC revision 9 by dpavlin, Sat Mar 21 22:54:10 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 80  sub ping { Line 81  sub ping {
81    
82  =head2 set  =head2 set
83    
84    $r->set( foo => 'bar' );    $r->set( foo => 'bar', $new );
85    
86  =cut  =cut
87    
88  sub set {  sub set {
89          my ( $self, $k, $v ) = @_;          my ( $self, $k, $v, $new ) = @_;
90          print $sock "SET $k " . length($v) . "\r\n$v\r\n";          print $sock ( $new ? "SETNX" : "SET" ) . " $k " . length($v) . "\r\n$v\r\n";
91          my $ok = <$sock>;          my $ok = <$sock>;
92          die dump($ok) unless $ok eq "+OK\r\n";          confess dump($ok) unless $ok eq "+OK\r\n";
93  }  }
94    
95  =head2 get  =head2 get
# Line 101  sub get { Line 102  sub get {
102          my ( $self, $k ) = @_;          my ( $self, $k ) = @_;
103          print $sock "GET $k\r\n";          print $sock "GET $k\r\n";
104          my $len = <$sock>;          my $len = <$sock>;
105    #       warn "# len: ",dump($len);
106            return undef if $len eq "nil\r\n";
107          my $v;          my $v;
108          read($sock, $v, $len) || die $!;          read($sock, $v, $len) || die $!;
109    #       warn "# v: ",dump($v);
110            my $crlf;
111            read($sock, $crlf, 2); # skip cr/lf
112          return $v;          return $v;
113  }  }
114    
115    =head2 incr
116    
117      $r->incr('counter');
118      $r->incr('tripplets', 3);
119    
120    =cut
121    
122    sub incr {
123            my ( $self, $key, $value ) = @_;
124            if ( defined $value ) {
125                    print $sock "INCRBY $key $value\r\n";
126            } else {
127                    print $sock "INCR $key\r\n";
128            }
129            my $count = <$sock>;
130            warn "# $key = $count";
131            return $count;
132    }
133    
134    =head2 decr
135    
136      $r->decr('counter');
137      $r->decr('tripplets', 3);
138    
139    =cut
140    
141    sub decr {
142            my ( $self, $key, $value ) = @_;
143            if ( defined $value ) {
144                    print $sock "DECRBY $key $value\r\n";
145            } else {
146                    print $sock "DECR $key\r\n";
147            }
148            my $count = <$sock>;
149            warn "# $key = $count";
150            return $count;
151    }
152    
153    =head2 exists
154    
155      $r->exists( 'key' ) && print "got key!";
156    
157    =cut
158    
159    sub exists {
160            my ( $self, $key ) = @_;
161            print $sock "EXISTS $key\r\n";
162            my $found = <$sock>;
163            $found =~ s{\r\n$}{};
164            warn "# exists $key = $found";
165            return $found;
166    }
167    
168  =head1 AUTHOR  =head1 AUTHOR
169    
170  Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>  Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>

Legend:
Removed from v.3  
changed lines
  Added in v.9

  ViewVC Help
Powered by ViewVC 1.1.26