X-Git-Url: http://git.rot13.org/?p=perl-Redis.git;a=blobdiff_plain;f=lib%2FRedis.pm;h=ba874887cb451aa96b91c7e169775945b679384f;hp=6c88fd53c7b404c1eaeb4ef233c14379197e0502;hb=17bcbd7825f411e1b55e15b4e034433a9b1c4811;hpb=63abbe056d199d6634093dbca16407b5070c0af0 diff --git a/lib/Redis.pm b/lib/Redis.pm index 6c88fd5..ba87488 100644 --- a/lib/Redis.pm +++ b/lib/Redis.pm @@ -70,6 +70,18 @@ sub _sock_result_bulk { return $v; } +sub _sock_ok { + my $ok = <$sock>; + confess dump($ok) unless $ok eq "+OK\r\n"; +} + +sub _sock_send_bulk { + my ( $self, $command, $key, $value ) = @_; + print $sock "$command $key " . length($value) . "\r\n$value\r\n"; + _sock_ok(); +} + + =head1 Connection Handling =head2 quit @@ -105,10 +117,8 @@ sub ping { =cut sub set { - my ( $self, $k, $v, $new ) = @_; - print $sock ( $new ? "SETNX" : "SET" ) . " $k " . length($v) . "\r\n$v\r\n"; - my $ok = <$sock>; - confess dump($ok) unless $ok eq "+OK\r\n"; + my ( $self, $key, $value, $new ) = @_; + $self->_sock_send_bulk( "SET" . ( $new ? 'NX' : '' ), $key, $value ); } =head2 get @@ -216,11 +226,59 @@ sub keys { =cut sub randomkey { - my ( $self, $glob ) = @_; + my ( $self ) = @_; print $sock "RANDOMKEY\r\n"; _sock_result(); } +=head2 rename + + my $ok = $r->rename( 'old-key', 'new-key', $new ); + +=cut + +sub rename { + my ( $self, $old, $new, $nx ) = @_; + print $sock "RENAME" . ( $nx ? 'NX' : '' ) . " $old $new\r\n"; + _sock_ok(); +} + +=head2 dbsize + + my $nr_keys = $r->dbsize; + +=cut + +sub dbsize { + my ( $self ) = @_; + print $sock "DBSIZE\r\n"; + _sock_result(); +} + +=head1 Commands operating on lists + +=head2 rpush + + $r->rpush( $key, $value ); + +=cut + +sub rpush { + my ( $self, $key, $value ) = @_; + $self->_sock_send_bulk('RPUSH', $key, $value); +} + +=head2 lpush + + $r->lpush( $key, $value ); + +=cut + +sub lpush { + my ( $self, $key, $value ) = @_; + $self->_sock_send_bulk('LPUSH', $key, $value); +} + =head1 AUTHOR Dobrica Pavlinusic, C<< >>