--- lib/Redis.pm 2009/03/21 22:54:10 9 +++ lib/Redis.pm 2009/03/21 23:23:37 12 @@ -51,6 +51,25 @@ $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,15 +120,7 @@ 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 @@ -119,6 +130,8 @@ =cut + + sub incr { my ( $self, $key, $value ) = @_; if ( defined $value ) { @@ -126,9 +139,7 @@ } else { print $sock "INCR $key\r\n"; } - my $count = <$sock>; - warn "# $key = $count"; - return $count; + _sock_result(); } =head2 decr @@ -145,9 +156,7 @@ } else { print $sock "DECR $key\r\n"; } - my $count = <$sock>; - warn "# $key = $count"; - return $count; + _sock_result(); } =head2 exists @@ -159,10 +168,45 @@ sub exists { my ( $self, $key ) = @_; print $sock "EXISTS $key\r\n"; - my $found = <$sock>; - $found =~ s{\r\n$}{}; - warn "# exists $key = $found"; - return $found; + _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()); } =head1 AUTHOR