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
80 my $sock = $self->{sock} || die "no server connected";
82 my $command = $AUTOLOAD;
85 warn "## $command ",Dumper(@_) if $self->{debug};
89 if ( defined $bulk_command->{$command} ) {
91 $value = '' if ! defined $value;
109 warn ">> $send" if $self->{debug};
112 if ( $command eq 'quit' ) {
113 close( $sock ) || die "can't close socket: $!";
117 my $result = <$sock> || die "can't read socket: $!";
118 warn "<< $result" if $self->{debug};
119 my $type = substr($result,0,1);
120 $result = substr($result,1,-2);
122 if ( $command eq 'info' ) {
124 foreach my $l ( split(/\r\n/, $self->__read_bulk($result) ) ) {
125 my ($n,$v) = split(/:/, $l, 2);
129 } elsif ( $command eq 'keys' ) {
130 my $keys = $self->__read_bulk($result);
131 return split(/\s/, $keys) if $keys;
135 if ( $type eq '-' ) {
136 confess "[$command] $result";
137 } elsif ( $type eq '+' ) {
139 } elsif ( $type eq '$' ) {
140 return $self->__read_bulk($result);
141 } elsif ( $type eq '*' ) {
142 return $self->__read_multi_bulk($result);
143 } elsif ( $type eq ':' ) {
144 return $result; # FIXME check if int?
146 confess "unknown type: $type", $self->__read_line();
151 my ($self,$len) = @_;
152 return undef if $len < 0;
156 read($self->{sock}, $v, $len) || die $!;
157 warn "<< ",Dumper($v),$/ if $self->{debug};
160 read($self->{sock}, $crlf, 2); # skip cr/lf
164 sub __read_multi_bulk {
165 my ($self,$size) = @_;
166 return undef if $size < 0;
167 my $sock = $self->{sock};
171 my @list = ( 0 .. $size );
172 foreach ( 0 .. $size ) {
173 $list[ $_ ] = $self->__read_bulk( substr(<$sock>,1,-2) );
176 warn "## list = ", Dumper( @list ) if $self->{debug};
184 =head1 Connection Handling
192 $r->ping || die "no server?";
194 =head1 Commands operating on string values
198 $r->set( foo => 'bar' );
200 $r->setnx( foo => 42 );
204 my $value = $r->get( 'foo' );
208 my @values = $r->mget( 'foo', 'bar', 'baz' );
214 $r->incrby('tripplets', 3);
220 $r->decrby('tripplets', 3);
224 $r->exists( 'key' ) && print "got key!";
228 $r->del( 'key' ) || warn "key doesn't exist";
232 $r->type( 'key' ); # = string
234 =head1 Commands operating on the key space
238 my @keys = $r->keys( '*glob_pattern*' );
242 my $key = $r->randomkey;
246 my $ok = $r->rename( 'old-key', 'new-key', $new );
250 my $nr_keys = $r->dbsize;
252 =head1 Commands operating on lists
254 See also L<Redis::List> for tie interface.
258 $r->rpush( $key, $value );
262 $r->lpush( $key, $value );
270 my @list = $r->lrange( $key, $start, $end );
274 my $ok = $r->ltrim( $key, $start, $end );
278 $r->lindex( $key, $index );
282 $r->lset( $key, $index, $value );
286 my $modified_count = $r->lrem( $key, $count, $value );
290 my $value = $r->lpop( $key );
294 my $value = $r->rpop( $key );
296 =head1 Commands operating on sets
300 $r->sadd( $key, $member );
304 $r->srem( $key, $member );
308 my $elements = $r->scard( $key );
312 $r->sismember( $key, $member );
316 $r->sinter( $key1, $key2, ... );
320 my $ok = $r->sinterstore( $dstkey, $key1, $key2, ... );
322 =head1 Multiple databases handling commands
326 $r->select( $dbindex ); # 0 for new clients
330 $r->move( $key, $dbindex );
344 $r->sort("key BY pattern LIMIT start end GET pattern ASC|DESC ALPHA');
346 =head1 Persistence control commands
364 =head1 Remote server control commands
368 my $info_hash = $r->info;
372 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
376 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
377 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
378 automatically be notified of progress on your bug as I make changes.
385 You can find documentation for this module with the perldoc command.
392 You can also look for information at:
396 =item * RT: CPAN's request tracker
398 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
400 =item * AnnoCPAN: Annotated CPAN documentation
402 L<http://annocpan.org/dist/Redis>
406 L<http://cpanratings.perl.org/d/Redis>
410 L<http://search.cpan.org/dist/Redis>
415 =head1 ACKNOWLEDGEMENTS
418 =head1 COPYRIGHT & LICENSE
420 Copyright 2009-2010 Dobrica Pavlinusic, all rights reserved.
422 This program is free software; you can redistribute it and/or modify it
423 under the same terms as Perl itself.