| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Tie::Redis::Connection; | 
| 2 |  |  |  |  |  |  | { | 
| 3 |  |  |  |  |  |  | $Tie::Redis::Connection::VERSION = '0.26'; | 
| 4 |  |  |  |  |  |  | } | 
| 5 |  |  |  |  |  |  | # ABSTRACT: Connection to Redis | 
| 6 | 4 |  |  | 4 |  | 19 | use strict; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 124 |  | 
| 7 | 4 |  |  | 4 |  | 19 | use warnings; | 
|  | 4 |  |  |  |  | 6 |  | 
|  | 4 |  |  |  |  | 137 |  | 
| 8 | 4 |  |  | 4 |  | 4534 | use IO::Socket::IP; | 
|  | 4 |  |  |  |  | 200759 |  | 
|  | 4 |  |  |  |  | 36 |  | 
| 9 | 4 |  |  | 4 |  | 7500 | use Protocol::Redis; | 
|  | 4 |  |  |  |  | 12851 |  | 
|  | 4 |  |  |  |  | 122 |  | 
| 10 | 4 |  |  | 4 |  | 27 | use Carp (); | 
|  | 4 |  |  |  |  | 9 |  | 
|  | 4 |  |  |  |  | 97 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 4 |  |  | 4 |  | 20 | use constant DEBUG => $ENV{TIE_REDIS_DEBUG}; | 
|  | 4 |  |  |  |  | 10 |  | 
|  | 4 |  |  |  |  | 361 |  | 
| 13 | 4 | 50 |  |  |  | 38 | use constant PR_CLASS => eval { require Protocol::Redis::XS; 1 } | 
|  | 4 |  |  |  |  | 4403 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 14 | 4 |  |  | 4 |  | 22 | ? "Protocol::Redis::XS" : "Protocol::Redis"; | 
|  | 4 |  |  |  |  | 9 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | our $AUTOLOAD; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | sub new { | 
| 19 | 1 |  |  | 1 | 0 | 3 | my($class, %args) = @_; | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 1 |  | 50 |  |  | 15 | my $host = delete $args{host} || 'localhost'; | 
| 22 | 1 |  | 50 |  |  | 4 | my $port = delete $args{port} || 6379; | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 1 | 50 |  |  |  | 5 | if (my $encoding = $args{encoding}) { | 
| 25 | 0 |  |  |  |  | 0 | $args{encoding} = Encode::find_encoding($encoding); | 
| 26 | 0 | 0 |  |  |  | 0 | Carp::croak qq{Encoding "$encoding" not found} unless ref $args{encoding}; | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | bless { | 
| 30 | 1 |  | 50 |  |  | 11 | _sock => (IO::Socket::IP->new("$host:$port") || return), | 
| 31 |  |  |  |  |  |  | _pr   => PR_CLASS->new(api => 1), | 
| 32 |  |  |  |  |  |  | host  => $host, | 
| 33 |  |  |  |  |  |  | port  => $port, | 
| 34 |  |  |  |  |  |  | %args, | 
| 35 |  |  |  |  |  |  | }, $class; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub DESTROY { | 
| 39 | 0 |  |  | 0 |  |  | close shift->{_sock}; | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | sub AUTOLOAD { | 
| 43 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 44 | 0 |  |  |  |  |  | (my $method = $AUTOLOAD) =~ s/.*:://; | 
| 45 | 0 |  |  |  |  |  | $self->_cmd($method, @_); | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | sub _cmd { | 
| 49 | 0 |  |  | 0 |  |  | my($self, $cmd, @args) = @_; | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 0 |  |  |  |  |  | warn "TR>> $cmd @args\n" if DEBUG; | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 0 | 0 |  |  |  |  | $self->{_sock}->syswrite( | 
| 54 |  |  |  |  |  |  | $self->{_pr}->encode({type => "*", data => [ | 
| 55 |  |  |  |  |  |  | map +{ type => '$', data => $_ }, $cmd, @args | 
| 56 |  |  |  |  |  |  | ]}) | 
| 57 |  |  |  |  |  |  | ) or return; | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 0 |  |  |  |  |  | my $message; | 
| 60 | 0 |  |  |  |  |  | do { | 
| 61 | 0 | 0 |  |  |  |  | $self->{_sock}->sysread(my $buf, 8192) or return; | 
| 62 | 0 |  |  |  |  |  | $self->{_pr}->parse($buf); | 
| 63 | 0 |  |  |  |  |  | $message = $self->{_pr}->get_message; | 
| 64 |  |  |  |  |  |  | } while not $message; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 0 | 0 |  |  |  |  | if($message->{type} eq '*') { | 
|  |  | 0 |  |  |  |  |  | 
| 67 | 0 |  |  |  |  |  | warn "TR<< ", (join " ", map $_->{data}, @{$message->{data}}), "\n" if DEBUG; | 
| 68 | 0 |  |  |  |  |  | my @data = map $_->{data}, @{$message->{data}}; | 
|  | 0 |  |  |  |  |  |  | 
| 69 | 0 | 0 |  |  |  |  | wantarray ? @data : \@data; | 
| 70 |  |  |  |  |  |  | } elsif($message->{type} eq '-') { | 
| 71 | 0 |  |  |  |  |  | Carp::croak "$cmd: " . $message->{data}; | 
| 72 |  |  |  |  |  |  | } else { | 
| 73 | 0 |  |  |  |  |  | warn "TR<< $message->{data}\n" if DEBUG; | 
| 74 | 0 |  |  |  |  |  | $message->{data}; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | 1; | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | __END__ |