12 Redis - perl binding for Redis database
16 our $VERSION = '1.2001';
21 Pure perl bindings for L<http://code.google.com/p/redis/>
23 This version supports protocol 1.2 or later of Redis available at
25 L<git://github.com/antirez/redis>
28 lists commands which are exercised in test suite, but
29 additinal commands will work correctly since protocol
30 specifies enough information to support almost all commands
31 with same peace of code with a little help of C<AUTOLOAD>.
37 my $r = Redis->new; # $ENV{REDIS_SERVER} or 127.0.0.1:6379
39 my $r = Redis->new( server => '192.168.0.1:6379', debug = 0 );
46 $self->{debug} ||= $ENV{REDIS_DEBUG};
48 $self->{sock} = IO::Socket::INET->new(
49 PeerAddr => $self->{server} || $ENV{REDIS_SERVER} || '127.0.0.1:6379',
59 rpush => 1, lpush => 1,
73 # we don't want DESTROY to fallback into AUTOLOAD
82 my $sock = $self->{sock} || die "no server connected";
84 my $command = $AUTOLOAD;
87 warn "## $command ",Dumper(@_) if $self->{debug};
91 if ( defined $bulk_command->{$command} ) {
93 $value = '' if ! defined $value;
111 warn ">> $send" if $self->{debug};
114 if ( $command eq 'quit' ) {
115 close( $sock ) || die "can't close socket: $!";
119 my $result = <$sock> || die "can't read socket: $!";
120 warn "<< $result" if $self->{debug};
121 my $type = substr($result,0,1);
122 $result = substr($result,1,-2);
124 if ( $command eq 'info' ) {
126 foreach my $l ( split(/\r\n/, $self->__read_bulk($result) ) ) {
127 my ($n,$v) = split(/:/, $l, 2);
131 } elsif ( $command eq 'keys' ) {
132 my $keys = $self->__read_bulk($result);
133 return split(/\s/, $keys) if $keys;
137 if ( $type eq '-' ) {
138 confess "[$command] $result";
139 } elsif ( $type eq '+' ) {
141 } elsif ( $type eq '$' ) {
142 return $self->__read_bulk($result);
143 } elsif ( $type eq '*' ) {
144 return $self->__read_multi_bulk($result);
145 } elsif ( $type eq ':' ) {
146 return $result; # FIXME check if int?
148 confess "unknown type: $type", $self->__read_line();
153 my ($self,$len) = @_;
154 return undef if $len < 0;
158 read($self->{sock}, $v, $len) || die $!;
159 warn "<< ",Dumper($v),$/ if $self->{debug};
162 read($self->{sock}, $crlf, 2); # skip cr/lf
166 sub __read_multi_bulk {
167 my ($self,$size) = @_;
168 return undef if $size < 0;
169 my $sock = $self->{sock};
173 my @list = ( 0 .. $size );
174 foreach ( 0 .. $size ) {
175 $list[ $_ ] = $self->__read_bulk( substr(<$sock>,1,-2) );
178 warn "## list = ", Dumper( @list ) if $self->{debug};
186 =head1 Connection Handling
194 $r->ping || die "no server?";
196 =head1 Commands operating on string values
200 $r->set( foo => 'bar' );
202 $r->setnx( foo => 42 );
206 my $value = $r->get( 'foo' );
210 my @values = $r->mget( 'foo', 'bar', 'baz' );
216 $r->incrby('tripplets', 3);
222 $r->decrby('tripplets', 3);
226 $r->exists( 'key' ) && print "got key!";
230 $r->del( 'key' ) || warn "key doesn't exist";
234 $r->type( 'key' ); # = string
236 =head1 Commands operating on the key space
240 my @keys = $r->keys( '*glob_pattern*' );
244 my $key = $r->randomkey;
248 my $ok = $r->rename( 'old-key', 'new-key', $new );
252 my $nr_keys = $r->dbsize;
254 =head1 Commands operating on lists
256 See also L<Redis::List> for tie interface.
260 $r->rpush( $key, $value );
264 $r->lpush( $key, $value );
272 my @list = $r->lrange( $key, $start, $end );
276 my $ok = $r->ltrim( $key, $start, $end );
280 $r->lindex( $key, $index );
284 $r->lset( $key, $index, $value );
288 my $modified_count = $r->lrem( $key, $count, $value );
292 my $value = $r->lpop( $key );
296 my $value = $r->rpop( $key );
298 =head1 Commands operating on sets
302 $r->sadd( $key, $member );
306 $r->srem( $key, $member );
310 my $elements = $r->scard( $key );
314 $r->sismember( $key, $member );
318 $r->sinter( $key1, $key2, ... );
322 my $ok = $r->sinterstore( $dstkey, $key1, $key2, ... );
324 =head1 Multiple databases handling commands
328 $r->select( $dbindex ); # 0 for new clients
332 $r->move( $key, $dbindex );
346 $r->sort("key BY pattern LIMIT start end GET pattern ASC|DESC ALPHA');
348 =head1 Persistence control commands
366 =head1 Remote server control commands
370 my $info_hash = $r->info;
374 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
378 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
379 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
380 automatically be notified of progress on your bug as I make changes.
387 You can find documentation for this module with the perldoc command.
394 You can also look for information at:
398 =item * RT: CPAN's request tracker
400 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
402 =item * AnnoCPAN: Annotated CPAN documentation
404 L<http://annocpan.org/dist/Redis>
408 L<http://cpanratings.perl.org/d/Redis>
412 L<http://search.cpan.org/dist/Redis>
417 =head1 ACKNOWLEDGEMENTS
420 =head1 COPYRIGHT & LICENSE
422 Copyright 2009-2010 Dobrica Pavlinusic, all rights reserved.
424 This program is free software; you can redistribute it and/or modify it
425 under the same terms as Perl itself.