X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;ds=sidebyside;f=lib%2FRedis.pm;h=6c88fd53c7b404c1eaeb4ef233c14379197e0502;hb=63abbe056d199d6634093dbca16407b5070c0af0;hp=d03416e779b8d47ffccdae1ac0740d099cda1890;hpb=a57e5b0908eea8c9a2dbd0c0a0a36729afa5fc58;p=perl-Redis.git diff --git a/lib/Redis.pm b/lib/Redis.pm index d03416e..6c88fd5 100644 --- a/lib/Redis.pm +++ b/lib/Redis.pm @@ -51,6 +51,25 @@ sub new { $self; } +sub _sock_result { + my $result = <$sock>; + warn "# result: ",dump( $result ); + $result =~ s{\r\n$}{} || warn "can't find cr/lf"; + return $result; +} + +sub _sock_result_bulk { + my $len = <$sock>; + 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; +} + =head1 Connection Handling =head2 quit @@ -101,18 +120,106 @@ sub set { sub get { my ( $self, $k ) = @_; print $sock "GET $k\r\n"; - my $len = <$sock>; -# 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; + _sock_result_bulk(); +} + +=head2 incr + + $r->incr('counter'); + $r->incr('tripplets', 3); + +=cut + + + +sub incr { + my ( $self, $key, $value ) = @_; + if ( defined $value ) { + print $sock "INCRBY $key $value\r\n"; + } else { + print $sock "INCR $key\r\n"; + } + _sock_result(); +} + +=head2 decr + + $r->decr('counter'); + $r->decr('tripplets', 3); + +=cut + +sub decr { + my ( $self, $key, $value ) = @_; + if ( defined $value ) { + print $sock "DECRBY $key $value\r\n"; + } else { + print $sock "DECR $key\r\n"; + } + _sock_result(); +} + +=head2 exists + + $r->exists( 'key' ) && print "got key!"; + +=cut + +sub exists { + my ( $self, $key ) = @_; + print $sock "EXISTS $key\r\n"; + _sock_result(); +} + +=head2 del + + $r->del( 'key' ) || warn "key doesn't exist"; + +=cut + +sub del { + my ( $self, $key ) = @_; + print $sock "DEL $key\r\n"; + _sock_result(); +} + +=head2 type + + $r->type( 'key' ); # = string + +=cut + +sub type { + my ( $self, $key ) = @_; + print $sock "TYPE $key\r\n"; + _sock_result(); } +=head1 Commands operating on the key space + +=head2 keys + + my @keys = $r->keys( '*glob_pattern*' ); + +=cut +sub keys { + my ( $self, $glob ) = @_; + print $sock "KEYS $glob\r\n"; + return split(/\s/, _sock_result_bulk()); +} + +=head2 randomkey + + my $key = $r->randomkey; + +=cut + +sub randomkey { + my ( $self, $glob ) = @_; + print $sock "RANDOMKEY\r\n"; + _sock_result(); +} =head1 AUTHOR