13 Redis - perl binding for Redis database
17 our $VERSION = '1.2001';
22 Pure perl bindings for L<http://code.google.com/p/redis/>
24 This version supports protocol 1.2 or later of Redis available at
26 L<git://github.com/antirez/redis>
29 lists commands which are exercised in test suite, but
30 additinal commands will work correctly since protocol
31 specifies enough information to support almost all commands
32 with same peace of code with a little help of C<AUTOLOAD>.
38 my $r = Redis->new; # $ENV{REDIS_SERVER} or 127.0.0.1:6379
40 my $r = Redis->new( server => '192.168.0.1:6379', debug = 0 );
47 $self->{debug} ||= $ENV{REDIS_DEBUG};
49 $self->{sock} = IO::Socket::INET->new(
50 PeerAddr => $self->{server} || $ENV{REDIS_SERVER} || '127.0.0.1:6379',
58 my $multi_bulk_command = {
65 rpush => 1, lpush => 1,
79 # we don't want DESTROY to fallback into AUTOLOAD
88 my $sock = $self->{sock} || die "no server connected";
90 my $command = $AUTOLOAD;
93 warn "## $command ",Dumper(@_) if $self->{debug};
97 if ( defined $multi_bulk_command->{$command} ) {
99 '*' . (scalar(@_) + 1) . "\r\n"
100 . '$' . (length($command)) . "\r\n"
101 . uc($command) . "\r\n"
103 $send .= join "\r\n", map { '$' . length($_) . "\r\n" . $_ } @_;
105 } elsif ( defined $bulk_command->{$command} ) {
107 $value = '' if ! defined $value;
125 warn ">> $send" if $self->{debug};
128 if ( $command eq 'quit' ) {
129 close( $sock ) || die "can't close socket: $!";
133 my $result = <$sock> || die "can't read socket: $!";
134 Encode::_utf8_on($result);
135 warn "<< $result" if $self->{debug};
136 my $type = substr($result,0,1);
137 $result = substr($result,1,-2);
139 if ( $command eq 'info' ) {
141 foreach my $l ( split(/\r\n/, $self->__read_bulk($result) ) ) {
142 my ($n,$v) = split(/:/, $l, 2);
146 } elsif ( $command eq 'keys' ) {
147 my $keys = $self->__read_bulk($result);
148 return split(/\s/, $keys) if $keys;
152 if ( $type eq '-' ) {
153 confess "[$command] $result";
154 } elsif ( $type eq '+' ) {
156 } elsif ( $type eq '$' ) {
157 return $self->__read_bulk($result);
158 } elsif ( $type eq '*' ) {
159 return $self->__read_multi_bulk($result);
160 } elsif ( $type eq ':' ) {
161 return $result; # FIXME check if int?
163 confess "unknown type: $type", $self->__read_line();
168 my ($self,$len) = @_;
173 read($self->{sock}, $v, $len) || die $!;
174 Encode::_utf8_on($v);
175 warn "<< ",Dumper($v),$/ if $self->{debug};
178 read($self->{sock}, $crlf, 2); # skip cr/lf
182 sub __read_multi_bulk {
183 my ($self,$size) = @_;
185 my $sock = $self->{sock};
189 my @list = ( 0 .. $size );
190 foreach ( 0 .. $size ) {
191 $list[ $_ ] = $self->__read_bulk( substr(<$sock>,1,-2) );
194 warn "## list = ", Dumper( @list ) if $self->{debug};
202 =head1 Connection Handling
210 $r->ping || die "no server?";
212 =head1 Commands operating on string values
216 $r->set( foo => 'bar' );
218 $r->setnx( foo => 42 );
222 my $value = $r->get( 'foo' );
226 my @values = $r->mget( 'foo', 'bar', 'baz' );
232 $r->incrby('tripplets', 3);
238 $r->decrby('tripplets', 3);
242 $r->exists( 'key' ) && print "got key!";
246 $r->del( 'key' ) || warn "key doesn't exist";
250 $r->type( 'key' ); # = string
252 =head1 Commands operating on the key space
256 my @keys = $r->keys( '*glob_pattern*' );
260 my $key = $r->randomkey;
264 my $ok = $r->rename( 'old-key', 'new-key', $new );
268 my $nr_keys = $r->dbsize;
270 =head1 Commands operating on lists
272 See also L<Redis::List> for tie interface.
276 $r->rpush( $key, $value );
280 $r->lpush( $key, $value );
288 my @list = $r->lrange( $key, $start, $end );
292 my $ok = $r->ltrim( $key, $start, $end );
296 $r->lindex( $key, $index );
300 $r->lset( $key, $index, $value );
304 my $modified_count = $r->lrem( $key, $count, $value );
308 my $value = $r->lpop( $key );
312 my $value = $r->rpop( $key );
314 =head1 Commands operating on sets
318 $r->sadd( $key, $member );
322 $r->srem( $key, $member );
326 my $elements = $r->scard( $key );
330 $r->sismember( $key, $member );
334 $r->sinter( $key1, $key2, ... );
338 my $ok = $r->sinterstore( $dstkey, $key1, $key2, ... );
340 =head1 Multiple databases handling commands
344 $r->select( $dbindex ); # 0 for new clients
348 $r->move( $key, $dbindex );
362 $r->sort("key BY pattern LIMIT start end GET pattern ASC|DESC ALPHA');
364 =head1 Persistence control commands
382 =head1 Remote server control commands
386 my $info_hash = $r->info;
390 Since Redis knows nothing about encoding, we are forcing utf-8 flag on all data received from Redis.
391 This change is introduced in 1.2001 version.
393 This allows us to round-trip utf-8 encoded characters correctly, but might be problem if you push
394 binary junk into Redis and expect to get it back without utf-8 flag turned on.
398 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
402 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
403 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
404 automatically be notified of progress on your bug as I make changes.
411 You can find documentation for this module with the perldoc command.
418 You can also look for information at:
422 =item * RT: CPAN's request tracker
424 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
426 =item * AnnoCPAN: Annotated CPAN documentation
428 L<http://annocpan.org/dist/Redis>
432 L<http://cpanratings.perl.org/d/Redis>
436 L<http://search.cpan.org/dist/Redis>
441 =head1 ACKNOWLEDGEMENTS
444 =head1 COPYRIGHT & LICENSE
446 Copyright 2009-2010 Dobrica Pavlinusic, all rights reserved.
448 This program is free software; you can redistribute it and/or modify it
449 under the same terms as Perl itself.