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);
105 if ( $type eq '-' ) {
106 confess "[$command] $result";
107 } elsif ( $type eq '+' ) {
109 } elsif ( $type eq '$' ) {
110 return $self->__read_bulk($result);
111 } elsif ( $type eq '*' ) {
112 return $self->__read_multi_bulk($result);
113 } elsif ( $type eq ':' ) {
114 return $result; # FIXME check if int?
116 confess "unknown type: $type", $self->__read_line();
121 my ($self,$len) = @_;
122 return undef if $len < 0;
126 read($self->{sock}, $v, $len) || die $!;
127 Encode::_utf8_on($v);
128 warn "<< ",Dumper($v),$/ if $self->{debug};
131 read($self->{sock}, $crlf, 2); # skip cr/lf
135 sub __read_multi_bulk {
136 my ($self,$size) = @_;
137 return undef if $size < 0;
138 my $sock = $self->{sock};
142 my @list = ( 0 .. $size );
143 foreach ( 0 .. $size ) {
144 $list[ $_ ] = $self->__read_bulk( substr(<$sock>,1,-2) );
147 warn "## list = ", Dumper( @list ) if $self->{debug};
155 =head1 Connection Handling
163 $r->ping || die "no server?";
165 =head1 Commands operating on string values
169 $r->set( foo => 'bar' );
171 $r->setnx( foo => 42 );
175 my $value = $r->get( 'foo' );
179 my @values = $r->mget( 'foo', 'bar', 'baz' );
185 $r->incrby('tripplets', 3);
191 $r->decrby('tripplets', 3);
195 $r->exists( 'key' ) && print "got key!";
199 $r->del( 'key' ) || warn "key doesn't exist";
203 $r->type( 'key' ); # = string
205 =head1 Commands operating on the key space
209 my @keys = $r->keys( '*glob_pattern*' );
213 my $key = $r->randomkey;
217 my $ok = $r->rename( 'old-key', 'new-key', $new );
221 my $nr_keys = $r->dbsize;
223 =head1 Commands operating on lists
225 See also L<Redis::List> for tie interface.
229 $r->rpush( $key, $value );
233 $r->lpush( $key, $value );
241 my @list = $r->lrange( $key, $start, $end );
245 my $ok = $r->ltrim( $key, $start, $end );
249 $r->lindex( $key, $index );
253 $r->lset( $key, $index, $value );
257 my $modified_count = $r->lrem( $key, $count, $value );
261 my $value = $r->lpop( $key );
265 my $value = $r->rpop( $key );
267 =head1 Commands operating on sets
271 $r->sadd( $key, $member );
275 $r->srem( $key, $member );
279 my $elements = $r->scard( $key );
283 $r->sismember( $key, $member );
287 $r->sinter( $key1, $key2, ... );
291 my $ok = $r->sinterstore( $dstkey, $key1, $key2, ... );
293 =head1 Multiple databases handling commands
297 $r->select( $dbindex ); # 0 for new clients
301 $r->move( $key, $dbindex );
315 $r->sort("key BY pattern LIMIT start end GET pattern ASC|DESC ALPHA');
317 =head1 Persistence control commands
335 =head1 Remote server control commands
339 my $info_hash = $r->info;
343 Since Redis knows nothing about encoding, we are forcing utf-8 flag on all data received from Redis.
344 This change is introduced in 1.2001 version.
346 This allows us to round-trip utf-8 encoded characters correctly, but might be problem if you push
347 binary junk into Redis and expect to get it back without utf-8 flag turned on.
351 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
355 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
356 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
357 automatically be notified of progress on your bug as I make changes.
364 You can find documentation for this module with the perldoc command.
371 You can also look for information at:
375 =item * RT: CPAN's request tracker
377 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
379 =item * AnnoCPAN: Annotated CPAN documentation
381 L<http://annocpan.org/dist/Redis>
385 L<http://cpanratings.perl.org/d/Redis>
389 L<http://search.cpan.org/dist/Redis>
394 =head1 ACKNOWLEDGEMENTS
397 =head1 COPYRIGHT & LICENSE
399 Copyright 2009-2010 Dobrica Pavlinusic, all rights reserved.
401 This program is free software; you can redistribute it and/or modify it
402 under the same terms as Perl itself.