rpush, refactor _sock_send_bulk to expect +OK
[perl-Redis.git] / lib / Redis.pm
index f0b21a2..484e9b1 100644 (file)
@@ -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
@@ -209,6 +219,55 @@ sub keys {
        return split(/\s/, _sock_result_bulk());
 }
 
+=head2 randomkey
+
+  my $key = $r->randomkey;
+
+=cut
+
+sub randomkey {
+       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);
+}
+
 =head1 AUTHOR
 
 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>