X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;ds=sidebyside;f=lib%2FRedis.pm;h=6c88fd53c7b404c1eaeb4ef233c14379197e0502;hb=63abbe056d199d6634093dbca16407b5070c0af0;hp=c5597df58503e333250181c7ee968bfdbc72cb9d;hpb=5ecd629987e0a79871fdbad482628d70e33e4495;p=perl-Redis.git diff --git a/lib/Redis.pm b/lib/Redis.pm index c5597df..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,15 +120,7 @@ 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 @@ -119,12 +130,6 @@ sub get { =cut -sub sock_result { - my $result = <$sock>; - warn "# result: ",dump( $result ); - $result =~ s{\r\n$}{} || warn "can't find cr/lf"; - return $result; -} sub incr { @@ -134,7 +139,7 @@ sub incr { } else { print $sock "INCR $key\r\n"; } - sock_result(); + _sock_result(); } =head2 decr @@ -151,7 +156,7 @@ sub decr { } else { print $sock "DECR $key\r\n"; } - sock_result(); + _sock_result(); } =head2 exists @@ -163,7 +168,7 @@ sub decr { sub exists { my ( $self, $key ) = @_; print $sock "EXISTS $key\r\n"; - sock_result(); + _sock_result(); } =head2 del @@ -175,7 +180,45 @@ sub exists { sub del { my ( $self, $key ) = @_; print $sock "DEL $key\r\n"; - sock_result(); + _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