12 Redis - perl binding for Redis database
16 our $VERSION = '0.0801';
21 Pure perl bindings for L<http://code.google.com/p/redis/>
23 This version support git version 0.08 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,
66 # we don't want DESTROY to fallback into AUTOLOAD
73 my $sock = $self->{sock} || die "no server connected";
75 my $command = $AUTOLOAD;
78 warn "## $command ",Dumper(@_) if $self->{debug};
82 if ( defined $bulk_command->{$command} ) {
84 $value = '' if ! defined $value;
102 warn ">> $send" if $self->{debug};
105 if ( $command eq 'quit' ) {
106 close( $sock ) || die "can't close socket: $!";
110 my $result = <$sock> || die "can't read socket: $!";
111 warn "<< $result" if $self->{debug};
112 my $type = substr($result,0,1);
113 $result = substr($result,1,-2);
115 if ( $command eq 'info' ) {
117 foreach my $l ( split(/\r\n/, $self->__read_bulk($result) ) ) {
118 my ($n,$v) = split(/:/, $l, 2);
122 } elsif ( $command eq 'keys' ) {
123 my $keys = $self->__read_bulk($result);
124 return split(/\s/, $keys) if $keys;
128 if ( $type eq '-' ) {
129 confess "[$command] $result";
130 } elsif ( $type eq '+' ) {
132 } elsif ( $type eq '$' ) {
133 return $self->__read_bulk($result);
134 } elsif ( $type eq '*' ) {
135 return $self->__read_multi_bulk($result);
136 } elsif ( $type eq ':' ) {
137 return $result; # FIXME check if int?
139 confess "unknown type: $type", $self->__read_line();
144 my ($self,$len) = @_;
145 return undef if $len < 0;
149 read($self->{sock}, $v, $len) || die $!;
150 warn "<< ",Dumper($v),$/ if $self->{debug};
153 read($self->{sock}, $crlf, 2); # skip cr/lf
157 sub __read_multi_bulk {
158 my ($self,$size) = @_;
159 return undef if $size < 0;
160 my $sock = $self->{sock};
164 my @list = ( 0 .. $size );
165 foreach ( 0 .. $size ) {
166 $list[ $_ ] = $self->__read_bulk( substr(<$sock>,1,-2) );
169 warn "## list = ", Dumper( @list ) if $self->{debug};
177 =head1 Connection Handling
185 $r->ping || die "no server?";
187 =head1 Commands operating on string values
191 $r->set( foo => 'bar' );
193 $r->setnx( foo => 42 );
197 my $value = $r->get( 'foo' );
201 my @values = $r->mget( 'foo', 'bar', 'baz' );
207 $r->incrby('tripplets', 3);
213 $r->decrby('tripplets', 3);
217 $r->exists( 'key' ) && print "got key!";
221 $r->del( 'key' ) || warn "key doesn't exist";
225 $r->type( 'key' ); # = string
227 =head1 Commands operating on the key space
231 my @keys = $r->keys( '*glob_pattern*' );
235 my $key = $r->randomkey;
239 my $ok = $r->rename( 'old-key', 'new-key', $new );
243 my $nr_keys = $r->dbsize;
245 =head1 Commands operating on lists
247 See also L<Redis::List> for tie interface.
251 $r->rpush( $key, $value );
255 $r->lpush( $key, $value );
263 my @list = $r->lrange( $key, $start, $end );
267 my $ok = $r->ltrim( $key, $start, $end );
271 $r->lindex( $key, $index );
275 $r->lset( $key, $index, $value );
279 my $modified_count = $r->lrem( $key, $count, $value );
283 my $value = $r->lpop( $key );
287 my $value = $r->rpop( $key );
289 =head1 Commands operating on sets
293 $r->sadd( $key, $member );
297 $r->srem( $key, $member );
301 my $elements = $r->scard( $key );
305 $r->sismember( $key, $member );
309 $r->sinter( $key1, $key2, ... );
313 my $ok = $r->sinterstore( $dstkey, $key1, $key2, ... );
315 =head1 Multiple databases handling commands
319 $r->select( $dbindex ); # 0 for new clients
323 $r->move( $key, $dbindex );
337 $r->sort("key BY pattern LIMIT start end GET pattern ASC|DESC ALPHA');
339 =head1 Persistence control commands
357 =head1 Remote server control commands
361 my $info_hash = $r->info;
365 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
369 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
370 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
371 automatically be notified of progress on your bug as I make changes.
378 You can find documentation for this module with the perldoc command.
385 You can also look for information at:
389 =item * RT: CPAN's request tracker
391 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
393 =item * AnnoCPAN: Annotated CPAN documentation
395 L<http://annocpan.org/dist/Redis>
399 L<http://cpanratings.perl.org/d/Redis>
403 L<http://search.cpan.org/dist/Redis>
408 =head1 ACKNOWLEDGEMENTS
411 =head1 COPYRIGHT & LICENSE
413 Copyright 2009 Dobrica Pavlinusic, all rights reserved.
415 This program is free software; you can redistribute it and/or modify it
416 under the same terms as Perl itself.