Move QUIT command out of the common case
[perl-Redis.git] / lib / Redis.pm
index 49eed27..ca065b9 100644 (file)
@@ -48,10 +48,11 @@ sub new {
        $self->{debug} ||= $ENV{REDIS_DEBUG};
        $self->{encoding} ||= 'utf8';    ## default to lax utf8
 
+       $self->{server} ||= $ENV{REDIS_SERVER} || '127.0.0.1:6379';
        $self->{sock} = IO::Socket::INET->new(
-               PeerAddr => $self->{server} || $ENV{REDIS_SERVER} || '127.0.0.1:6379',
+               PeerAddr => $self->{server},
                Proto => 'tcp',
-       ) || die $!;
+       ) || confess("Could not connect to Redis server at $self->{server}: $!");
 
        return bless($self, $class);
 }
@@ -59,33 +60,21 @@ sub new {
 # we don't want DESTROY to fallback into AUTOLOAD
 sub DESTROY {}
 
+
+### Deal with common, general case, Redis commands
 our $AUTOLOAD;
 sub AUTOLOAD {
        my $self = shift;
-       my $sock = $self->{sock} || die "no server connected";
+       my $sock = $self->{sock} || confess("Not connected to any server");
        my $enc = $self->{encoding};
        my $deb = $self->{debug};
 
        my $command = $AUTOLOAD;
        $command =~ s/.*://;
-       warn "[SEND] $command ",Dumper([@_]) if $deb;
-
-       my $n_elems = scalar(@_)+1;
-       my $send = "\*$n_elems\r\n";
-       for my $str (uc($command), @_) {
-         my $bin = $enc? encode($enc, $str) : $str;
-         $send .= defined($bin)? '$'.length($bin)."\r\n$bin\r\n" : "\$-1\r\n";
-       }
 
-       warn "[SEND RAW] $send" if $deb;
-       print $sock $send;
+       $self->__send_command($command, @_);
 
-       if ( $command eq 'quit' ) {
-               close( $sock ) || die "can't close socket: $!";
-               return 1;
-       }
-
-       my $result = <$sock> || die "can't read socket: $!";
+       my $result = <$sock> || confess("Can't read socket: $!");
        my $type = substr($result,0,1);
        $result = substr($result,1,-2);
 
@@ -122,6 +111,50 @@ sub AUTOLOAD {
        }
 }
 
+
+### Commands with extra logic
+
+sub quit {
+  my ($self) = @_;
+
+  $self->__send_command('QUIT');
+
+  close(delete $self->{sock}) || confess("Can't close socket: $!");
+  return 1;
+}
+
+
+### Socket operations
+
+sub __send_command {
+  my $self = shift;
+  my $cmd  = uc(shift);
+  my $enc  = $self->{encoding};
+  my $deb  = $self->{debug};
+
+  warn "[SEND] $cmd ", Dumper([@_]) if $deb;
+
+  ## Encode command using multi-bulk format
+  my $n_elems = scalar(@_) + 1;
+  my $buf     = "\*$n_elems\r\n";
+  for my $elem ($cmd, @_) {
+    my $bin = $enc ? encode($enc, $elem) : $elem;
+    $buf .= defined($bin) ? '$' . length($bin) . "\r\n$bin\r\n" : "\$-1\r\n";
+  }
+
+  ## Send command, take care for partial writes
+  warn "[SEND RAW] $buf" if $deb;
+  my $sock = $self->{sock} || confess("Not connected to any server");
+  while ($buf) {
+    my $len = syswrite $sock, $buf, length $buf;
+    confess("Could not write to Redis server: $!")
+      unless $len;
+    substr $buf, 0, $len, "";
+  }
+
+  return;
+}
+
 sub __read_bulk {
        my ($self,$len) = @_;
        return if $len < 0;
@@ -129,7 +162,7 @@ sub __read_bulk {
        my $enc = $self->{encoding};
        my $v = '';
        if ( $len > 0 ) {
-               read($self->{sock}, $v, $len) || die $!;
+               read($self->{sock}, $v, $len) || confess("Could not read from sock: $!");
                $v = decode($enc, $v) if $enc;
        }
        my $crlf;