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',
60 rpush => 1, lpush => 1,
74 # we don't want DESTROY to fallback into AUTOLOAD
83 my $sock = $self->{sock} || die "no server connected";
85 my $command = $AUTOLOAD;
88 warn "## $command ",Dumper(@_) if $self->{debug};
92 if ( defined $bulk_command->{$command} ) {
94 $value = '' if ! defined $value;
112 warn ">> $send" if $self->{debug};
115 if ( $command eq 'quit' ) {
116 close( $sock ) || die "can't close socket: $!";
120 my $result = <$sock>;
122 $self->{sock} = $sock = IO::Socket::INET->new(
123 PeerAddr => $self->{server},
129 $result = <$sock> || die "can't read socket: $!";
131 Encode::_utf8_on($result);
132 warn "<< $result" if $self->{debug};
133 my $type = substr($result,0,1);
134 $result = substr($result,1,-2);
136 if ( $command eq 'info' ) {
138 foreach my $l ( split(/\r\n/, $self->__read_bulk($result) ) ) {
139 my ($n,$v) = split(/:/, $l, 2);
143 } elsif ( $command eq 'keys' ) {
144 my $keys = $self->__read_bulk($result);
145 return split(/\s/, $keys) if $keys;
149 if ( $type eq '-' ) {
150 confess "[$command] $result";
151 } elsif ( $type eq '+' ) {
153 } elsif ( $type eq '$' ) {
154 return $self->__read_bulk($result);
155 } elsif ( $type eq '*' ) {
156 return $self->__read_multi_bulk($result);
157 } elsif ( $type eq ':' ) {
158 return $result; # FIXME check if int?
160 confess "unknown type: $type", $self->__read_line();
165 my ($self,$len) = @_;
166 return undef if $len < 0;
170 read($self->{sock}, $v, $len) || die $!;
171 Encode::_utf8_on($v);
172 warn "<< ",Dumper($v),$/ if $self->{debug};
175 read($self->{sock}, $crlf, 2); # skip cr/lf
179 sub __read_multi_bulk {
180 my ($self,$size) = @_;
181 return undef if $size < 0;
182 my $sock = $self->{sock};
186 my @list = ( 0 .. $size );
187 foreach ( 0 .. $size ) {
188 $list[ $_ ] = $self->__read_bulk( substr(<$sock>,1,-2) );
191 warn "## list = ", Dumper( @list ) if $self->{debug};
199 =head1 Connection Handling
207 $r->ping || die "no server?";
209 =head1 Commands operating on string values
213 $r->set( foo => 'bar' );
215 $r->setnx( foo => 42 );
219 my $value = $r->get( 'foo' );
223 my @values = $r->mget( 'foo', 'bar', 'baz' );
229 $r->incrby('tripplets', 3);
235 $r->decrby('tripplets', 3);
239 $r->exists( 'key' ) && print "got key!";
243 $r->del( 'key' ) || warn "key doesn't exist";
247 $r->type( 'key' ); # = string
249 =head1 Commands operating on the key space
253 my @keys = $r->keys( '*glob_pattern*' );
257 my $key = $r->randomkey;
261 my $ok = $r->rename( 'old-key', 'new-key', $new );
265 my $nr_keys = $r->dbsize;
267 =head1 Commands operating on lists
269 See also L<Redis::List> for tie interface.
273 $r->rpush( $key, $value );
277 $r->lpush( $key, $value );
285 my @list = $r->lrange( $key, $start, $end );
289 my $ok = $r->ltrim( $key, $start, $end );
293 $r->lindex( $key, $index );
297 $r->lset( $key, $index, $value );
301 my $modified_count = $r->lrem( $key, $count, $value );
305 my $value = $r->lpop( $key );
309 my $value = $r->rpop( $key );
311 =head1 Commands operating on sets
315 $r->sadd( $key, $member );
319 $r->srem( $key, $member );
323 my $elements = $r->scard( $key );
327 $r->sismember( $key, $member );
331 $r->sinter( $key1, $key2, ... );
335 my $ok = $r->sinterstore( $dstkey, $key1, $key2, ... );
337 =head1 Multiple databases handling commands
341 $r->select( $dbindex ); # 0 for new clients
345 $r->move( $key, $dbindex );
359 $r->sort("key BY pattern LIMIT start end GET pattern ASC|DESC ALPHA');
361 =head1 Persistence control commands
379 =head1 Remote server control commands
383 my $info_hash = $r->info;
387 Since Redis knows nothing about encoding, we are forcing utf-8 flag on all data received from Redis.
388 This change is introduced in 1.2001 version.
390 This allows us to round-trip utf-8 encoded characters correctly, but might be problem if you push
391 binary junk into Redis and expect to get it back without utf-8 flag turned on.
395 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
399 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
400 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
401 automatically be notified of progress on your bug as I make changes.
408 You can find documentation for this module with the perldoc command.
415 You can also look for information at:
419 =item * RT: CPAN's request tracker
421 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
423 =item * AnnoCPAN: Annotated CPAN documentation
425 L<http://annocpan.org/dist/Redis>
429 L<http://cpanratings.perl.org/d/Redis>
433 L<http://search.cpan.org/dist/Redis>
438 =head1 ACKNOWLEDGEMENTS
441 =head1 COPYRIGHT & LICENSE
443 Copyright 2009-2010 Dobrica Pavlinusic, all rights reserved.
445 This program is free software; you can redistribute it and/or modify it
446 under the same terms as Perl itself.