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 # we don't want DESTROY to fallback into AUTOLOAD
67 my $sock = $self->{sock} || die "no server connected";
69 my $command = $AUTOLOAD;
72 warn "## $command ",Dumper(@_) if $self->{debug};
74 unshift @_, uc($command);
79 . join("", map { "\$". length($_) ."\r\n". $_ ."\r\n" } @_)
82 warn ">> $send" if $self->{debug};
85 if ( $command eq 'quit' ) {
86 close( $sock ) || die "can't close socket: $!";
90 my $result = <$sock> || die "can't read socket: $!";
91 Encode::_utf8_on($result);
92 warn "<< $result" if $self->{debug};
93 my $type = substr($result,0,1);
94 $result = substr($result,1,-2);
96 if ( $command eq 'info' ) {
98 foreach my $l ( split(/\r\n/, $self->__read_bulk($result) ) ) {
99 my ($n,$v) = split(/:/, $l, 2);
103 } elsif ( $command eq 'keys' ) {
104 my $keys = $self->__read_bulk($result);
105 return split(/\s/, $keys) if $keys;
109 if ( $type eq '-' ) {
110 confess "[$command] $result";
111 } elsif ( $type eq '+' ) {
113 } elsif ( $type eq '$' ) {
114 return $self->__read_bulk($result);
115 } elsif ( $type eq '*' ) {
116 return $self->__read_multi_bulk($result);
117 } elsif ( $type eq ':' ) {
118 return $result; # FIXME check if int?
120 confess "unknown type: $type", $self->__read_line();
125 my ($self,$len) = @_;
126 return undef if $len < 0;
130 read($self->{sock}, $v, $len) || die $!;
131 Encode::_utf8_on($v);
132 warn "<< ",Dumper($v),$/ if $self->{debug};
135 read($self->{sock}, $crlf, 2); # skip cr/lf
139 sub __read_multi_bulk {
140 my ($self,$size) = @_;
141 return undef if $size < 0;
142 my $sock = $self->{sock};
146 my @list = ( 0 .. $size );
147 foreach ( 0 .. $size ) {
148 $list[ $_ ] = $self->__read_bulk( substr(<$sock>,1,-2) );
151 warn "## list = ", Dumper( @list ) if $self->{debug};
159 =head1 Connection Handling
167 $r->ping || die "no server?";
169 =head1 Commands operating on string values
173 $r->set( foo => 'bar' );
175 $r->setnx( foo => 42 );
179 my $value = $r->get( 'foo' );
183 my @values = $r->mget( 'foo', 'bar', 'baz' );
189 $r->incrby('tripplets', 3);
195 $r->decrby('tripplets', 3);
199 $r->exists( 'key' ) && print "got key!";
203 $r->del( 'key' ) || warn "key doesn't exist";
207 $r->type( 'key' ); # = string
209 =head1 Commands operating on the key space
213 my @keys = $r->keys( '*glob_pattern*' );
217 my $key = $r->randomkey;
221 my $ok = $r->rename( 'old-key', 'new-key', $new );
225 my $nr_keys = $r->dbsize;
227 =head1 Commands operating on lists
229 See also L<Redis::List> for tie interface.
233 $r->rpush( $key, $value );
237 $r->lpush( $key, $value );
245 my @list = $r->lrange( $key, $start, $end );
249 my $ok = $r->ltrim( $key, $start, $end );
253 $r->lindex( $key, $index );
257 $r->lset( $key, $index, $value );
261 my $modified_count = $r->lrem( $key, $count, $value );
265 my $value = $r->lpop( $key );
269 my $value = $r->rpop( $key );
271 =head1 Commands operating on sets
275 $r->sadd( $key, $member );
279 $r->srem( $key, $member );
283 my $elements = $r->scard( $key );
287 $r->sismember( $key, $member );
291 $r->sinter( $key1, $key2, ... );
295 my $ok = $r->sinterstore( $dstkey, $key1, $key2, ... );
297 =head1 Multiple databases handling commands
301 $r->select( $dbindex ); # 0 for new clients
305 $r->move( $key, $dbindex );
319 $r->sort("key BY pattern LIMIT start end GET pattern ASC|DESC ALPHA');
321 =head1 Persistence control commands
339 =head1 Remote server control commands
343 my $info_hash = $r->info;
347 Since Redis knows nothing about encoding, we are forcing utf-8 flag on all data received from Redis.
348 This change is introduced in 1.2001 version.
350 This allows us to round-trip utf-8 encoded characters correctly, but might be problem if you push
351 binary junk into Redis and expect to get it back without utf-8 flag turned on.
355 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
359 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
360 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
361 automatically be notified of progress on your bug as I make changes.
368 You can find documentation for this module with the perldoc command.
375 You can also look for information at:
379 =item * RT: CPAN's request tracker
381 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
383 =item * AnnoCPAN: Annotated CPAN documentation
385 L<http://annocpan.org/dist/Redis>
389 L<http://cpanratings.perl.org/d/Redis>
393 L<http://search.cpan.org/dist/Redis>
398 =head1 ACKNOWLEDGEMENTS
401 =head1 COPYRIGHT & LICENSE
403 Copyright 2009-2010 Dobrica Pavlinusic, all rights reserved.
405 This program is free software; you can redistribute it and/or modify it
406 under the same terms as Perl itself.