/[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 53 by dpavlin, Tue Mar 24 22:51:53 2009 UTC revision 70 by dpavlin, Wed Mar 17 20:26:07 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    use Encode;
10    
11  =head1 NAME  =head1 NAME
12    
# Line 13  Redis - perl binding for Redis database Line 14  Redis - perl binding for Redis database
14    
15  =cut  =cut
16    
17  our $VERSION = '0.08';  our $VERSION = '1.2001';
18    
19    
20  =head1 DESCRIPTION  =head1 DESCRIPTION
21    
22  Pure perl bindings for L<http://code.google.com/p/redis/>  Pure perl bindings for L<http://code.google.com/p/redis/>
23    
24  This version support git version 0.08 of Redis available at  This version supports protocol 1.2 or later of Redis available at
25    
26  L<git://github.com/antirez/redis>  L<git://github.com/antirez/redis>
27    
# Line 34  with same peace of code with a little he Line 35  with same peace of code with a little he
35    
36  =head2 new  =head2 new
37    
38    my $r = Redis->new;    my $r = Redis->new; # $ENV{REDIS_SERVER} or 127.0.0.1:6379
39    
40  =cut    my $r = Redis->new( server => '192.168.0.1:6379', debug = 0 );
   
 our $debug = $ENV{REDIS} || 0;  
41    
42  our $sock;  =cut
 my $server = '127.0.0.1:6379';  
43    
44  sub new {  sub new {
45          my $class = shift;          my $class = shift;
46          my $self = {};          my $self = {@_};
47          bless($self, $class);          $self->{debug} ||= $ENV{REDIS_DEBUG};
48    
49          warn "# opening socket to $server";          $self->{sock} = IO::Socket::INET->new(
50                    PeerAddr => $self->{server} || $ENV{REDIS_SERVER} || '127.0.0.1:6379',
         $sock ||= IO::Socket::INET->new(  
                 PeerAddr => $server,  
51                  Proto => 'tcp',                  Proto => 'tcp',
52          ) || die $!;          ) || die $!;
53    
54            bless($self, $class);
55          $self;          $self;
56  }  }
57    
# Line 65  my $bulk_command = { Line 62  my $bulk_command = {
62          sadd => 1,      srem => 1,          sadd => 1,      srem => 1,
63          sismember => 1,          sismember => 1,
64          echo => 1,          echo => 1,
65            getset => 1,
66            smove => 1,
67            zadd => 1,
68            zrem => 1,
69            zscore => 1,
70            zincrby => 1,
71            append => 1,
72  };  };
73    
74  # we don't want DESTROY to fallback into AUTOLOAD  # we don't want DESTROY to fallback into AUTOLOAD
# Line 74  our $AUTOLOAD; Line 78  our $AUTOLOAD;
78  sub AUTOLOAD {  sub AUTOLOAD {
79          my $self = shift;          my $self = shift;
80    
81            use bytes;
82    
83            my $sock = $self->{sock} || die "no server connected";
84    
85          my $command = $AUTOLOAD;          my $command = $AUTOLOAD;
86          $command =~ s/.*://;          $command =~ s/.*://;
87    
88          warn "## $command ",dump(@_) if $debug;          warn "## $command ",Dumper(@_) if $self->{debug};
89    
90          my $send;          my $send;
91    
92          if ( defined $bulk_command->{$command} ) {          if ( defined $bulk_command->{$command} ) {
93                  my $value = pop;                  my $value = pop;
94                    $value = '' if ! defined $value;
95                  $send                  $send
96                          = uc($command)                          = uc($command)
97                          . ' '                          . ' '
98                          . join(' ', @_)                          . join(' ', @_)
99                          . ' '                          . ' '
100                          . length($value)                          . length( $value )
101                          . "\r\n$value\r\n"                          . "\r\n$value\r\n"
102                          ;                          ;
103          } else {          } else {
# Line 100  sub AUTOLOAD { Line 109  sub AUTOLOAD {
109                          ;                          ;
110          }          }
111    
112          warn ">> $send" if $debug;          warn ">> $send" if $self->{debug};
113          print $sock $send;          print $sock $send;
114    
115          if ( $command eq 'quit' ) {          if ( $command eq 'quit' ) {
# Line 109  sub AUTOLOAD { Line 118  sub AUTOLOAD {
118          }          }
119    
120          my $result = <$sock> || die "can't read socket: $!";          my $result = <$sock> || die "can't read socket: $!";
121          warn "<< $result" if $debug;          Encode::_utf8_on($result);
122            warn "<< $result" if $self->{debug};
123          my $type = substr($result,0,1);          my $type = substr($result,0,1);
124          $result = substr($result,1,-2);          $result = substr($result,1,-2);
125    
126          if ( $command eq 'info' ) {          if ( $command eq 'info' ) {
127                  my $hash;                  my $hash;
128                  foreach my $l ( split(/\r\n/, __sock_read_bulk($result) ) ) {                  foreach my $l ( split(/\r\n/, $self->__read_bulk($result) ) ) {
129                          my ($n,$v) = split(/:/, $l, 2);                          my ($n,$v) = split(/:/, $l, 2);
130                          $hash->{$n} = $v;                          $hash->{$n} = $v;
131                  }                  }
132                  return $hash;                  return $hash;
133          } elsif ( $command eq 'keys' ) {          } elsif ( $command eq 'keys' ) {
134                  return split(/\s/, __sock_read_bulk($result));                  my $keys = $self->__read_bulk($result);
135                    return split(/\s/, $keys) if $keys;
136                    return;
137          }          }
138    
139          if ( $type eq '-' ) {          if ( $type eq '-' ) {
140                  confess $result;                  confess "[$command] $result";
141          } elsif ( $type eq '+' ) {          } elsif ( $type eq '+' ) {
142                  return $result;                  return $result;
143          } elsif ( $type eq '$' ) {          } elsif ( $type eq '$' ) {
144                  return __sock_read_bulk($result);                  return $self->__read_bulk($result);
145          } elsif ( $type eq '*' ) {          } elsif ( $type eq '*' ) {
146                  return __sock_read_multi_bulk($result);                  return $self->__read_multi_bulk($result);
147          } elsif ( $type eq ':' ) {          } elsif ( $type eq ':' ) {
148                  return $result; # FIXME check if int?                  return $result; # FIXME check if int?
149          } else {          } else {
150                  confess "unknown type: $type", __sock_read_line();                  confess "unknown type: $type", $self->__read_line();
151          }          }
152  }  }
153    
154  sub __sock_read_bulk {  sub __read_bulk {
155          my $len = shift;          my ($self,$len) = @_;
156          return undef if $len < 0;          return undef if $len < 0;
157    
158          my $v;          my $v;
159          if ( $len > 0 ) {          if ( $len > 0 ) {
160                  read($sock, $v, $len) || die $!;                  read($self->{sock}, $v, $len) || die $!;
161                  warn "<< ",dump($v),$/ if $debug;                  Encode::_utf8_on($v);
162                    warn "<< ",Dumper($v),$/ if $self->{debug};
163          }          }
164          my $crlf;          my $crlf;
165          read($sock, $crlf, 2); # skip cr/lf          read($self->{sock}, $crlf, 2); # skip cr/lf
166          return $v;          return $v;
167  }  }
168    
169  sub __sock_read_multi_bulk {  sub __read_multi_bulk {
170          my $size = shift;          my ($self,$size) = @_;
171          return undef if $size < 0;          return undef if $size < 0;
172            my $sock = $self->{sock};
173    
174          $size--;          $size--;
175    
176          my @list = ( 0 .. $size );          my @list = ( 0 .. $size );
177          foreach ( 0 .. $size ) {          foreach ( 0 .. $size ) {
178                  $list[ $_ ] = __sock_read_bulk( substr(<$sock>,1,-2) );                  $list[ $_ ] = $self->__read_bulk( substr(<$sock>,1,-2) );
179          }          }
180    
181          warn "## list = ", dump( @list ) if $debug;          warn "## list = ", Dumper( @list ) if $self->{debug};
182          return @list;          return @list;
183  }  }
184    
# Line 408  L<http://search.cpan.org/dist/Redis> Line 422  L<http://search.cpan.org/dist/Redis>
422    
423  =head1 COPYRIGHT & LICENSE  =head1 COPYRIGHT & LICENSE
424    
425  Copyright 2009 Dobrica Pavlinusic, all rights reserved.  Copyright 2009-2010 Dobrica Pavlinusic, all rights reserved.
426    
427  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
428  under the same terms as Perl itself.  under the same terms as Perl itself.

Legend:
Removed from v.53  
changed lines
  Added in v.70

  ViewVC Help
Powered by ViewVC 1.1.26