/[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 65 by dpavlin, Wed Mar 17 16:24:28 2010 UTC
# Line 4  use warnings; Line 4  use warnings;
4  use strict;  use strict;
5    
6  use IO::Socket::INET;  use IO::Socket::INET;
7  use Data::Dump qw/dump/;  use Data::Dumper;
8  use Carp qw/confess/;  use Carp qw/confess/;
9    
10  =head1 NAME  =head1 NAME
# 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 65  my $bulk_command = { Line 61  my $bulk_command = {
61          sadd => 1,      srem => 1,          sadd => 1,      srem => 1,
62          sismember => 1,          sismember => 1,
63          echo => 1,          echo => 1,
64            getset => 1,
65            smove => 1,
66            zadd => 1,
67            zrem => 1,
68            zscore => 1,
69            zincrby => 1,
70            append => 1,
71  };  };
72    
73  # we don't want DESTROY to fallback into AUTOLOAD  # we don't want DESTROY to fallback into AUTOLOAD
# Line 74  our $AUTOLOAD; Line 77  our $AUTOLOAD;
77  sub AUTOLOAD {  sub AUTOLOAD {
78          my $self = shift;          my $self = shift;
79    
80            my $sock = $self->{sock} || die "no server connected";
81    
82          my $command = $AUTOLOAD;          my $command = $AUTOLOAD;
83          $command =~ s/.*://;          $command =~ s/.*://;
84    
85          warn "## $command ",dump(@_) if $debug;          warn "## $command ",Dumper(@_) if $self->{debug};
86    
87          my $send;          my $send;
88    
# Line 101  sub AUTOLOAD { Line 106  sub AUTOLOAD {
106                          ;                          ;
107          }          }
108    
109          warn ">> $send" if $debug;          warn ">> $send" if $self->{debug};
110          print $sock $send;          print $sock $send;
111    
112          if ( $command eq 'quit' ) {          if ( $command eq 'quit' ) {
# Line 110  sub AUTOLOAD { Line 115  sub AUTOLOAD {
115          }          }
116    
117          my $result = <$sock> || die "can't read socket: $!";          my $result = <$sock> || die "can't read socket: $!";
118          warn "<< $result" if $debug;          warn "<< $result" if $self->{debug};
119          my $type = substr($result,0,1);          my $type = substr($result,0,1);
120          $result = substr($result,1,-2);          $result = substr($result,1,-2);
121    
122          if ( $command eq 'info' ) {          if ( $command eq 'info' ) {
123                  my $hash;                  my $hash;
124                  foreach my $l ( split(/\r\n/, __sock_read_bulk($result) ) ) {                  foreach my $l ( split(/\r\n/, $self->__read_bulk($result) ) ) {
125                          my ($n,$v) = split(/:/, $l, 2);                          my ($n,$v) = split(/:/, $l, 2);
126                          $hash->{$n} = $v;                          $hash->{$n} = $v;
127                  }                  }
128                  return $hash;                  return $hash;
129          } elsif ( $command eq 'keys' ) {          } elsif ( $command eq 'keys' ) {
130                  my $keys = __sock_read_bulk($result);                  my $keys = $self->__read_bulk($result);
131                  return split(/\s/, $keys) if $keys;                  return split(/\s/, $keys) if $keys;
132                  return;                  return;
133          }          }
134    
135          if ( $type eq '-' ) {          if ( $type eq '-' ) {
136                  confess $result;                  confess "[$command] $result";
137          } elsif ( $type eq '+' ) {          } elsif ( $type eq '+' ) {
138                  return $result;                  return $result;
139          } elsif ( $type eq '$' ) {          } elsif ( $type eq '$' ) {
140                  return __sock_read_bulk($result);                  return $self->__read_bulk($result);
141          } elsif ( $type eq '*' ) {          } elsif ( $type eq '*' ) {
142                  return __sock_read_multi_bulk($result);                  return $self->__read_multi_bulk($result);
143          } elsif ( $type eq ':' ) {          } elsif ( $type eq ':' ) {
144                  return $result; # FIXME check if int?                  return $result; # FIXME check if int?
145          } else {          } else {
146                  confess "unknown type: $type", __sock_read_line();                  confess "unknown type: $type", $self->__read_line();
147          }          }
148  }  }
149    
150  sub __sock_read_bulk {  sub __read_bulk {
151          my $len = shift;          my ($self,$len) = @_;
152          return undef if $len < 0;          return undef if $len < 0;
153    
154          my $v;          my $v;
155          if ( $len > 0 ) {          if ( $len > 0 ) {
156                  read($sock, $v, $len) || die $!;                  read($self->{sock}, $v, $len) || die $!;
157                  warn "<< ",dump($v),$/ if $debug;                  warn "<< ",Dumper($v),$/ if $self->{debug};
158          }          }
159          my $crlf;          my $crlf;
160          read($sock, $crlf, 2); # skip cr/lf          read($self->{sock}, $crlf, 2); # skip cr/lf
161          return $v;          return $v;
162  }  }
163    
164  sub __sock_read_multi_bulk {  sub __read_multi_bulk {
165          my $size = shift;          my ($self,$size) = @_;
166          return undef if $size < 0;          return undef if $size < 0;
167            my $sock = $self->{sock};
168    
169          $size--;          $size--;
170    
171          my @list = ( 0 .. $size );          my @list = ( 0 .. $size );
172          foreach ( 0 .. $size ) {          foreach ( 0 .. $size ) {
173                  $list[ $_ ] = __sock_read_bulk( substr(<$sock>,1,-2) );                  $list[ $_ ] = $self->__read_bulk( substr(<$sock>,1,-2) );
174          }          }
175    
176          warn "## list = ", dump( @list ) if $debug;          warn "## list = ", Dumper( @list ) if $self->{debug};
177          return @list;          return @list;
178  }  }
179    

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

  ViewVC Help
Powered by ViewVC 1.1.26