/[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 69 by dpavlin, Wed Mar 17 18:22:09 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 = '1.2001';
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 supports protocol 1.2 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            use bytes;
81    
82            my $sock = $self->{sock} || die "no server connected";
83    
84          my $command = $AUTOLOAD;          my $command = $AUTOLOAD;
85          $command =~ s/.*://;          $command =~ s/.*://;
86    
87          warn "## $command ",dump(@_) if $debug;          warn "## $command ",Dumper(@_) if $self->{debug};
88    
89          my $send;          my $send;
90    
# Line 101  sub AUTOLOAD { Line 108  sub AUTOLOAD {
108                          ;                          ;
109          }          }
110    
111          warn ">> $send" if $debug;          warn ">> $send" if $self->{debug};
112          print $sock $send;          print $sock $send;
113    
114          if ( $command eq 'quit' ) {          if ( $command eq 'quit' ) {
# Line 110  sub AUTOLOAD { Line 117  sub AUTOLOAD {
117          }          }
118    
119          my $result = <$sock> || die "can't read socket: $!";          my $result = <$sock> || die "can't read socket: $!";
120          warn "<< $result" if $debug;          warn "<< $result" if $self->{debug};
121          my $type = substr($result,0,1);          my $type = substr($result,0,1);
122          $result = substr($result,1,-2);          $result = substr($result,1,-2);
123    
124          if ( $command eq 'info' ) {          if ( $command eq 'info' ) {
125                  my $hash;                  my $hash;
126                  foreach my $l ( split(/\r\n/, __sock_read_bulk($result) ) ) {                  foreach my $l ( split(/\r\n/, $self->__read_bulk($result) ) ) {
127                          my ($n,$v) = split(/:/, $l, 2);                          my ($n,$v) = split(/:/, $l, 2);
128                          $hash->{$n} = $v;                          $hash->{$n} = $v;
129                  }                  }
130                  return $hash;                  return $hash;
131          } elsif ( $command eq 'keys' ) {          } elsif ( $command eq 'keys' ) {
132                  my $keys = __sock_read_bulk($result);                  my $keys = $self->__read_bulk($result);
133                  return split(/\s/, $keys) if $keys;                  return split(/\s/, $keys) if $keys;
134                  return;                  return;
135          }          }
136    
137          if ( $type eq '-' ) {          if ( $type eq '-' ) {
138                  confess $result;                  confess "[$command] $result";
139          } elsif ( $type eq '+' ) {          } elsif ( $type eq '+' ) {
140                  return $result;                  return $result;
141          } elsif ( $type eq '$' ) {          } elsif ( $type eq '$' ) {
142                  return __sock_read_bulk($result);                  return $self->__read_bulk($result);
143          } elsif ( $type eq '*' ) {          } elsif ( $type eq '*' ) {
144                  return __sock_read_multi_bulk($result);                  return $self->__read_multi_bulk($result);
145          } elsif ( $type eq ':' ) {          } elsif ( $type eq ':' ) {
146                  return $result; # FIXME check if int?                  return $result; # FIXME check if int?
147          } else {          } else {
148                  confess "unknown type: $type", __sock_read_line();                  confess "unknown type: $type", $self->__read_line();
149          }          }
150  }  }
151    
152  sub __sock_read_bulk {  sub __read_bulk {
153          my $len = shift;          my ($self,$len) = @_;
154          return undef if $len < 0;          return undef if $len < 0;
155    
156          my $v;          my $v;
157          if ( $len > 0 ) {          if ( $len > 0 ) {
158                  read($sock, $v, $len) || die $!;                  read($self->{sock}, $v, $len) || die $!;
159                  warn "<< ",dump($v),$/ if $debug;                  warn "<< ",Dumper($v),$/ if $self->{debug};
160          }          }
161          my $crlf;          my $crlf;
162          read($sock, $crlf, 2); # skip cr/lf          read($self->{sock}, $crlf, 2); # skip cr/lf
163          return $v;          return $v;
164  }  }
165    
166  sub __sock_read_multi_bulk {  sub __read_multi_bulk {
167          my $size = shift;          my ($self,$size) = @_;
168          return undef if $size < 0;          return undef if $size < 0;
169            my $sock = $self->{sock};
170    
171          $size--;          $size--;
172    
173          my @list = ( 0 .. $size );          my @list = ( 0 .. $size );
174          foreach ( 0 .. $size ) {          foreach ( 0 .. $size ) {
175                  $list[ $_ ] = __sock_read_bulk( substr(<$sock>,1,-2) );                  $list[ $_ ] = $self->__read_bulk( substr(<$sock>,1,-2) );
176          }          }
177    
178          warn "## list = ", dump( @list ) if $debug;          warn "## list = ", Dumper( @list ) if $self->{debug};
179          return @list;          return @list;
180  }  }
181    
# Line 411  L<http://search.cpan.org/dist/Redis> Line 419  L<http://search.cpan.org/dist/Redis>
419    
420  =head1 COPYRIGHT & LICENSE  =head1 COPYRIGHT & LICENSE
421    
422  Copyright 2009 Dobrica Pavlinusic, all rights reserved.  Copyright 2009-2010 Dobrica Pavlinusic, all rights reserved.
423    
424  This program is free software; you can redistribute it and/or modify it  This program is free software; you can redistribute it and/or modify it
425  under the same terms as Perl itself.  under the same terms as Perl itself.

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

  ViewVC Help
Powered by ViewVC 1.1.26