/[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 24 by dpavlin, Sun Mar 22 13:18:11 2009 UTC revision 37 by dpavlin, Sun Mar 22 18:07:46 2009 UTC
# Line 9  use Carp qw/confess/; Line 9  use Carp qw/confess/;
9    
10  =head1 NAME  =head1 NAME
11    
12  Redis - The great new Redis!  Redis - perl binding for Redis database
13    
14  =cut  =cut
15    
# Line 20  our $VERSION = '0.01'; Line 20  our $VERSION = '0.01';
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 of Redis available at
24    L<git://github.com/antirez/redis>
25    
26      use Redis;      use Redis;
27    
28      my $r = Redis->new();      my $r = Redis->new();
29    
   
   
   
30  =head1 FUNCTIONS  =head1 FUNCTIONS
31    
32  =head2 new  =head2 new
33    
34  =cut  =cut
35    
36    our $debug = $ENV{REDIS} || 0;
37    
38  our $sock;  our $sock;
39  my $server = '127.0.0.1:6379';  my $server = '127.0.0.1:6379';
40    
# Line 53  sub new { Line 55  sub new {
55    
56  sub _sock_result {  sub _sock_result {
57          my $result = <$sock>;          my $result = <$sock>;
58          warn "# result: ",dump( $result );          warn "## result: ",dump( $result ) if $debug;
59          $result =~ s{\r\n$}{} || warn "can't find cr/lf";          $result =~ s{\r\n$}{} || warn "can't find cr/lf";
60          return $result;          return $result;
61  }  }
62    
63  sub _sock_read_bulk {  sub _sock_read_bulk {
64          my $len = <$sock>;          my $len = <$sock>;
65          warn "## bulk len: ",dump($len);          warn "## bulk len: ",dump($len) if $debug;
66          return undef if $len eq "nil\r\n";          return undef if $len eq "nil\r\n";
67          my $v;          my $v;
68          read($sock, $v, $len) || die $!;          if ( $len > 0 ) {
69          warn "## bulk v: ",dump($v);                  read($sock, $v, $len) || die $!;
70                    warn "## bulk v: ",dump($v) if $debug;
71            }
72          my $crlf;          my $crlf;
73          read($sock, $crlf, 2); # skip cr/lf          read($sock, $crlf, 2); # skip cr/lf
74          return $v;          return $v;
# Line 72  sub _sock_read_bulk { Line 76  sub _sock_read_bulk {
76    
77  sub _sock_result_bulk {  sub _sock_result_bulk {
78          my $self = shift;          my $self = shift;
79          warn "## _sock_result_bulk ",dump( @_ );          warn "## _sock_result_bulk ",dump( @_ ) if $debug;
80          print $sock join(' ',@_) . "\r\n";          print $sock join(' ',@_) . "\r\n";
81          _sock_read_bulk();          _sock_read_bulk();
82  }  }
83    
84  sub _sock_ok {  sub _sock_result_bulk_list {
85            my $self = shift;
86            warn "## _sock_result_bulk_list ",dump( @_ ) if $debug;
87    
88            my $size = $self->_sock_send( @_ );
89            confess $size unless $size > 0;
90            $size--;
91    
92            my @list = ( 0 .. $size );
93            foreach ( 0 .. $size ) {
94                    $list[ $_ ] = _sock_read_bulk();
95            }
96    
97            warn "## list = ", dump( @list ) if $debug;
98            return @list;
99    }
100    
101    sub __sock_ok {
102          my $ok = <$sock>;          my $ok = <$sock>;
103            return undef if $ok eq "nil\r\n";
104          confess dump($ok) unless $ok eq "+OK\r\n";          confess dump($ok) unless $ok eq "+OK\r\n";
105  }  }
106    
107  sub _sock_send {  sub _sock_send {
108          my $self = shift;          my $self = shift;
109          warn "## _sock_send ",dump( @_ );          warn "## _sock_send ",dump( @_ ) if $debug;
110          print $sock join(' ',@_) . "\r\n";          print $sock join(' ',@_) . "\r\n";
111          _sock_result();          _sock_result();
112  }  }
113    
114  sub _sock_send_ok {  sub _sock_send_ok {
115          my $self = shift;          my $self = shift;
116          warn "## _sock_send_ok ",dump( @_ );          warn "## _sock_send_ok ",dump( @_ ) if $debug;
117          print $sock join(' ',@_) . "\r\n";          print $sock join(' ',@_) . "\r\n";
118          _sock_ok();          __sock_ok();
119  }  }
120    
121  sub _sock_send_bulk {  sub __sock_send_bulk_raw {
122          my $self = shift;          my $self = shift;
123            warn "## _sock_send_bulk ",dump( @_ ) if $debug;
124          my $value = pop;          my $value = pop;
125          print $sock join(' ',@_) . ' ' . length($value) . "\r\n$value\r\n";          $value = '' unless defined $value; # FIXME errr? nil?
126          _sock_ok();          print $sock join(' ',@_) . ' ' . length($value) . "\r\n$value\r\n"
127    }
128    
129    sub _sock_send_bulk {
130            __sock_send_bulk_raw( @_ );
131            __sock_ok();
132  }  }
133    
134    sub _sock_send_bulk_number {
135            __sock_send_bulk_raw( @_ );
136            my $v = _sock_result();
137            confess $v unless $v =~ m{^\-?\d+$};
138            return $v;
139    }
140    
141  =head1 Connection Handling  =head1 Connection Handling
142    
# Line 261  sub dbsize { Line 295  sub dbsize {
295    
296  =head1 Commands operating on lists  =head1 Commands operating on lists
297    
298    See also L<Redis::List> for tie interface.
299    
300  =head2 rpush  =head2 rpush
301    
302    $r->rpush( $key, $value );    $r->rpush( $key, $value );
# Line 302  sub llen { Line 338  sub llen {
338    
339  sub lrange {  sub lrange {
340          my ( $self, $key, $start, $end ) = @_;          my ( $self, $key, $start, $end ) = @_;
341          my $size = $self->_sock_send('LRANGE', $key, $start, $end);          $self->_sock_result_bulk_list('LRANGE', $key, $start, $end);
   
         confess $size unless $size > 0;  
         $size--;  
   
         my @list = ( 0 .. $size );  
         foreach ( 0 .. $size ) {  
                 $list[ $_ ] = _sock_read_bulk();  
         }  
   
         warn "## lrange $key $start $end = [$size] ", dump( @list );  
         return @list;  
342  }  }
343    
344  =head2 ltrim  =head2 ltrim
# Line 349  sub lset { Line 374  sub lset {
374          $self->_sock_send_bulk( 'LSET', $key, $index, $value );          $self->_sock_send_bulk( 'LSET', $key, $index, $value );
375  }  }
376    
377    =head2 lrem
378    
379      my $modified_count = $r->lrem( $key, $count, $value );
380    
381    =cut
382    
383    sub lrem {
384            my ( $self, $key, $count, $value ) = @_;
385            $self->_sock_send_bulk_number( 'LREM', $key, $count, $value );
386    }
387    
388    =head2 lpop
389    
390      my $value = $r->lpop( $key );
391    
392    =cut
393    
394    sub lpop {
395            my ( $self, $key ) = @_;
396            $self->_sock_result_bulk( 'LPOP', $key );
397    }
398    
399    =head2 rpop
400    
401      my $value = $r->rpop( $key );
402    
403    =cut
404    
405    sub rpop {
406            my ( $self, $key ) = @_;
407            $self->_sock_result_bulk( 'RPOP', $key );
408    }
409    
410    =head1 Commands operating on sets
411    
412    =head2 sadd
413    
414      $r->sadd( $key, $member );
415    
416    =cut
417    
418    sub sadd {
419            my ( $self, $key, $member ) = @_;
420            $self->_sock_send_bulk_number( 'SADD', $key, $member );
421    }
422    
423    =head2 srem
424    
425      $r->srem( $key, $member );
426    
427    =cut
428    
429    sub srem {
430            my ( $self, $key, $member ) = @_;
431            $self->_sock_send_bulk_number( 'SREM', $key, $member );
432    }
433    
434    =head2 scard
435    
436      my $elements = $r->scard( $key );
437    
438    =cut
439    
440    sub scard {
441            my ( $self, $key ) = @_;
442            $self->_sock_send( 'SCARD', $key );
443    }
444    
445    =head2 sismember
446    
447      $r->sismember( $key, $member );
448    
449    =cut
450    
451    sub sismember {
452            my ( $self, $key, $member ) = @_;
453            $self->_sock_send_bulk_number( 'SISMEMBER', $key, $member );
454    }
455    
456    =head2 sinter
457    
458      $r->sinter( $key1, $key2, ... );
459    
460    =cut
461    
462    sub sinter {
463            my $self = shift;
464            $self->_sock_result_bulk_list( 'SINTER', @_ );
465    }
466    
467    =head2 sinterstore
468    
469      my $ok = $r->sinterstore( $dstkey, $key1, $key2, ... );
470    
471    =cut
472    
473    sub sinterstore {
474            my $self = shift;
475            $self->_sock_send_ok( 'SINTERSTORE', @_ );
476    }
477    
478  =head1 AUTHOR  =head1 AUTHOR
479    
480  Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>  Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>

Legend:
Removed from v.24  
changed lines
  Added in v.37

  ViewVC Help
Powered by ViewVC 1.1.26