/[Redis.pre-github]/lib/Redis.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/Redis.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 55 by dpavlin, Tue Mar 24 23:43:24 2009 UTC revision 61 by dpavlin, Sat Sep 12 15:08:59 2009 UTC
# Line 13  Redis - perl binding for Redis database Line 13  Redis - perl binding for Redis database
13    
14  =cut  =cut
15    
16  our $VERSION = '0.08';  our $VERSION = '0.0801';
17    
18    
19  =head1 DESCRIPTION  =head1 DESCRIPTION
20    
21  Pure perl bindings for L<http://code.google.com/p/redis/>  Pure perl bindings for L<http://code.google.com/p/redis/>
22    
23  This version support git version 0.08 of Redis available at  This version support git version 0.08 or later of Redis available at
24    
25  L<git://github.com/antirez/redis>  L<git://github.com/antirez/redis>
26    
# Line 34  with same peace of code with a little he Line 34  with same peace of code with a little he
34    
35  =head2 new  =head2 new
36    
37    my $r = Redis->new;    my $r = Redis->new; # $ENV{REDIS_SERVER} or 127.0.0.1:6379
38    
39  =cut    my $r = Redis->new( server => '192.168.0.1:6379', debug = 0 );
   
 our $debug = $ENV{REDIS} || 0;  
40    
41  our $sock;  =cut
 my $server = '127.0.0.1:6379';  
42    
43  sub new {  sub new {
44          my $class = shift;          my $class = shift;
45          my $self = {};          my $self = {@_};
46          bless($self, $class);          $self->{debug} ||= $ENV{REDIS_DEBUG};
47    
48          warn "# opening socket to $server";          $self->{sock} = IO::Socket::INET->new(
49                    PeerAddr => $self->{server} || $ENV{REDIS_SERVER} || '127.0.0.1:6379',
         $sock ||= IO::Socket::INET->new(  
                 PeerAddr => $server,  
50                  Proto => 'tcp',                  Proto => 'tcp',
51          ) || die $!;          ) || die $!;
52    
53            bless($self, $class);
54          $self;          $self;
55  }  }
56    
# Line 74  our $AUTOLOAD; Line 70  our $AUTOLOAD;
70  sub AUTOLOAD {  sub AUTOLOAD {
71          my $self = shift;          my $self = shift;
72    
73            my $sock = $self->{sock} || die "no server connected";
74    
75          my $command = $AUTOLOAD;          my $command = $AUTOLOAD;
76          $command =~ s/.*://;          $command =~ s/.*://;
77    
78          warn "## $command ",dump(@_) if $debug;          warn "## $command ",dump(@_) if $self->{debug};
79    
80          my $send;          my $send;
81    
# Line 101  sub AUTOLOAD { Line 99  sub AUTOLOAD {
99                          ;                          ;
100          }          }
101    
102          warn ">> $send" if $debug;          warn ">> $send" if $self->{debug};
103          print $sock $send;          print $sock $send;
104    
105          if ( $command eq 'quit' ) {          if ( $command eq 'quit' ) {
# Line 110  sub AUTOLOAD { Line 108  sub AUTOLOAD {
108          }          }
109    
110          my $result = <$sock> || die "can't read socket: $!";          my $result = <$sock> || die "can't read socket: $!";
111          warn "<< $result" if $debug;          warn "<< $result" if $self->{debug};
112          my $type = substr($result,0,1);          my $type = substr($result,0,1);
113          $result = substr($result,1,-2);          $result = substr($result,1,-2);
114    
115          if ( $command eq 'info' ) {          if ( $command eq 'info' ) {
116                  my $hash;                  my $hash;
117                  foreach my $l ( split(/\r\n/, __sock_read_bulk($result) ) ) {                  foreach my $l ( split(/\r\n/, $self->__read_bulk($result) ) ) {
118                          my ($n,$v) = split(/:/, $l, 2);                          my ($n,$v) = split(/:/, $l, 2);
119                          $hash->{$n} = $v;                          $hash->{$n} = $v;
120                  }                  }
121                  return $hash;                  return $hash;
122          } elsif ( $command eq 'keys' ) {          } elsif ( $command eq 'keys' ) {
123                  my $keys = __sock_read_bulk($result);                  my $keys = $self->__read_bulk($result);
124                  return split(/\s/, $keys) if $keys;                  return split(/\s/, $keys) if $keys;
125                  return;                  return;
126          }          }
# Line 132  sub AUTOLOAD { Line 130  sub AUTOLOAD {
130          } elsif ( $type eq '+' ) {          } elsif ( $type eq '+' ) {
131                  return $result;                  return $result;
132          } elsif ( $type eq '$' ) {          } elsif ( $type eq '$' ) {
133                  return __sock_read_bulk($result);                  return $self->__read_bulk($result);
134          } elsif ( $type eq '*' ) {          } elsif ( $type eq '*' ) {
135                  return __sock_read_multi_bulk($result);                  return $self->__read_multi_bulk($result);
136          } elsif ( $type eq ':' ) {          } elsif ( $type eq ':' ) {
137                  return $result; # FIXME check if int?                  return $result; # FIXME check if int?
138          } else {          } else {
139                  confess "unknown type: $type", __sock_read_line();                  confess "unknown type: $type", $self->__read_line();
140          }          }
141  }  }
142    
143  sub __sock_read_bulk {  sub __read_bulk {
144          my $len = shift;          my ($self,$len) = @_;
145          return undef if $len < 0;          return undef if $len < 0;
146    
147          my $v;          my $v;
148          if ( $len > 0 ) {          if ( $len > 0 ) {
149                  read($sock, $v, $len) || die $!;                  read($self->{sock}, $v, $len) || die $!;
150                  warn "<< ",dump($v),$/ if $debug;                  warn "<< ",dump($v),$/ if $self->{debug};
151          }          }
152          my $crlf;          my $crlf;
153          read($sock, $crlf, 2); # skip cr/lf          read($self->{sock}, $crlf, 2); # skip cr/lf
154          return $v;          return $v;
155  }  }
156    
157  sub __sock_read_multi_bulk {  sub __read_multi_bulk {
158          my $size = shift;          my ($self,$size) = @_;
159          return undef if $size < 0;          return undef if $size < 0;
160            my $sock = $self->{sock};
161    
162          $size--;          $size--;
163    
164          my @list = ( 0 .. $size );          my @list = ( 0 .. $size );
165          foreach ( 0 .. $size ) {          foreach ( 0 .. $size ) {
166                  $list[ $_ ] = __sock_read_bulk( substr(<$sock>,1,-2) );                  $list[ $_ ] = $self->__read_bulk( substr(<$sock>,1,-2) );
167          }          }
168    
169          warn "## list = ", dump( @list ) if $debug;          warn "## list = ", dump( @list ) if $self->{debug};
170          return @list;          return @list;
171  }  }
172    

Legend:
Removed from v.55  
changed lines
  Added in v.61

  ViewVC Help
Powered by ViewVC 1.1.26