+sub keys {
+ my $self = shift;
+ $self->__is_valid_command('KEYS');
+
+ $self->__send_command('KEYS', @_);
+
+ my @keys = $self->__read_response('INFO', \my $type);
+ return @keys if $type eq '*';
+
+ ## Support redis <= 1.2.6
+ return split(/\s/, $keys[0]) if $keys[0];
+ return;
+}
+
+
+### Mode validation
+sub __is_valid_command {
+ my ($self, $cmd) = @_;
+
+ return unless $self->{is_subscriber};
+ return if $cmd =~ /^P?(UN)?SUBSCRIBE$/i;
+ confess("Cannot use command '$cmd' while in SUBSCRIBE mode, ");
+}
+
+
+### 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_response {
+ my ($self, $command, $type_r) = @_;
+
+ my ($type, $result) = $self->__read_sock;
+ $$type_r = $type if $type_r;
+
+ if ($type eq '-') {
+ confess "[$command] $result, ";
+ }
+ elsif ($type eq '+') {
+ return $result;
+ }
+ elsif ($type eq '$') {
+ return if $result < 0;
+ return $self->__read_sock($result);
+ }
+ elsif ($type eq '*') {
+ my @list;
+ while ($result--) {
+ push @list, $self->__read_response($command);
+ }
+ return @list;
+ }
+ elsif ($type eq ':') {
+ return $result;
+ }
+ else {
+ confess "unknown answer type: $type ($result), ";
+ }
+}
+
+sub __read_sock {
+ my ($self, $len) = @_;
+ my $sock = $self->{sock} || confess("Not connected to any server");
+ my $enc = $self->{encoding};
+ my $deb = $self->{debug};
+ my $rbuf = \($self->{rbuf});
+
+ my ($data, $type) = ('', '');
+ my $read_size = $self->{read_size};
+ $read_size = $len + 2 if defined $len && $len + 2 > $read_size;
+
+ while (1) {
+ ## Read NN bytes, strip \r\n at the end
+ if (defined $len) {
+ if (length($$rbuf) >= $len + 2) {
+ $data = substr(substr($$rbuf, 0, $len + 2, ''), 0, -2);
+ last;
+ }
+ }
+ ## No len, means line more, read until \r\n
+ elsif ($$rbuf =~ s/^(.)([^\015\012]*)\015\012//) {
+ ($type, $data) = ($1, $2);
+ last;
+ }
+
+ my $bytes = sysread $sock, $$rbuf, $read_size, length $$rbuf;
+ confess("Error while reading from Redis server: $!")
+ unless defined $bytes;
+ confess("Redis server closed connection") unless $bytes;
+ }
+
+ $data = decode($enc, $data) if $enc;
+ warn "[RECV] '$type$data'" if $self->{debug};
+
+ return ($type, $data) if $type;
+ return $data;
+}
+
+sub __can_read_sock {
+ my ($self) = @_;
+ my $sock = $self->{sock};
+ my $rbuf = \($self->{rbuf});
+
+ return 1 if $$rbuf;
+ __fh_nonblocking($sock, 1);
+ my $bytes = sysread $sock, $$rbuf, $self->{read_size}, length $$rbuf;
+ __fh_nonblocking($sock, 0);
+ return 1 if $bytes;
+ return 0;
+}
+
+
+### Copied from AnyEvent::Util
+BEGIN {
+ *__fh_nonblocking = ($^O eq 'MSWin32')
+ ? sub($$) { ioctl $_[0], 0x8004667e, pack "L", $_[1]; } # FIONBIO
+ : sub($$) { fcntl $_[0], F_SETFL, $_[1] ? O_NONBLOCK : 0; };
+}
+
+
+1;
+
+__END__
+