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 );
48 $self->{debug} ||= $ENV{REDIS_DEBUG};
49 $self->{encoding} ||= 'utf8'; ## default to lax utf8
51 $self->{sock} = IO::Socket::INET->new(
52 PeerAddr => $self->{server} || $ENV{REDIS_SERVER} || '127.0.0.1:6379',
56 return bless($self, $class);
59 # we don't want DESTROY to fallback into AUTOLOAD
65 my $sock = $self->{sock} || die "no server connected";
66 my $enc = $self->{encoding};
67 my $deb = $self->{debug};
69 my $command = $AUTOLOAD;
71 warn "[SEND] $command ",Dumper([@_]) if $deb;
73 my $n_elems = scalar(@_)+1;
74 my $send = "\*$n_elems\r\n";
75 for my $str (uc($command), @_) {
76 my $bin = $enc? encode($enc, $str) : $str;
77 $send .= defined($bin)? '$'.length($bin)."\r\n$bin\r\n" : "\$-1\r\n";
80 warn "[SEND RAW] $send" if $deb;
83 if ( $command eq 'quit' ) {
84 close( $sock ) || die "can't close socket: $!";
88 my $result = <$sock> || die "can't read socket: $!";
89 my $type = substr($result,0,1);
90 $result = substr($result,1,-2);
92 $result = decode($enc, $result) if $enc;
93 warn "[RECV] '$type$result'" if $deb;
95 if ( $command eq 'info' ) {
97 foreach my $l ( split(/\r\n/, $self->__read_bulk($result) ) ) {
98 my ($n,$v) = split(/:/, $l, 2);
102 } elsif ( $command eq 'keys' ) {
103 return $self->__read_multi_bulk($result)
105 my $keys = $self->__read_bulk($result);
106 return split(/\s/, $keys) if $keys;
110 if ( $type eq '-' ) {
111 confess "[$command] $result";
112 } elsif ( $type eq '+' ) {
114 } elsif ( $type eq '$' ) {
115 return $self->__read_bulk($result);
116 } elsif ( $type eq '*' ) {
117 return $self->__read_multi_bulk($result);
118 } elsif ( $type eq ':' ) {
119 return $result; # FIXME check if int?
121 confess "unknown type: $type", $self->__read_line();
126 my ($self,$len) = @_;
129 my $enc = $self->{encoding};
132 read($self->{sock}, $v, $len) || die $!;
133 $v = decode($enc, $v) if $enc;
136 read($self->{sock}, $crlf, 2); # skip cr/lf
138 warn "[PARSE] read_bulk ".Dumper($v) if $self->{debug};
142 sub __read_multi_bulk {
143 my ($self,$size) = @_;
144 return if $size <= 0;
146 my $sock = $self->{sock};
147 my $deb = $self->{debug};
148 my $enc = $self->{encoding};
151 my $v = $self->__read_bulk( substr(<$sock>,1,-2) );
152 $v = decode($enc, $v) if $enc;
153 warn " [PARSE] read_multi_bulk ($size) ".Dumper($v) if $deb;
157 warn "[PARSE] multi_bulk ".Dumper( \@list ) if $deb;
165 =head1 Connection Handling
173 $r->ping || die "no server?";
175 =head1 Commands operating on string values
179 $r->set( foo => 'bar' );
181 $r->setnx( foo => 42 );
185 my $value = $r->get( 'foo' );
189 my @values = $r->mget( 'foo', 'bar', 'baz' );
195 $r->incrby('tripplets', 3);
201 $r->decrby('tripplets', 3);
205 $r->exists( 'key' ) && print "got key!";
209 $r->del( 'key' ) || warn "key doesn't exist";
213 $r->type( 'key' ); # = string
215 =head1 Commands operating on the key space
219 my @keys = $r->keys( '*glob_pattern*' );
223 my $key = $r->randomkey;
227 my $ok = $r->rename( 'old-key', 'new-key', $new );
231 my $nr_keys = $r->dbsize;
233 =head1 Commands operating on lists
235 See also L<Redis::List> for tie interface.
239 $r->rpush( $key, $value );
243 $r->lpush( $key, $value );
251 my @list = $r->lrange( $key, $start, $end );
255 my $ok = $r->ltrim( $key, $start, $end );
259 $r->lindex( $key, $index );
263 $r->lset( $key, $index, $value );
267 my $modified_count = $r->lrem( $key, $count, $value );
271 my $value = $r->lpop( $key );
275 my $value = $r->rpop( $key );
277 =head1 Commands operating on sets
281 $r->sadd( $key, $member );
285 $r->srem( $key, $member );
289 my $elements = $r->scard( $key );
293 $r->sismember( $key, $member );
297 $r->sinter( $key1, $key2, ... );
301 my $ok = $r->sinterstore( $dstkey, $key1, $key2, ... );
303 =head1 Multiple databases handling commands
307 $r->select( $dbindex ); # 0 for new clients
311 $r->move( $key, $dbindex );
325 $r->sort("key BY pattern LIMIT start end GET pattern ASC|DESC ALPHA');
327 =head1 Persistence control commands
345 =head1 Remote server control commands
349 my $info_hash = $r->info;
353 Since Redis knows nothing about encoding, we are forcing utf-8 flag on all data received from Redis.
354 This change is introduced in 1.2001 version.
356 This allows us to round-trip utf-8 encoded characters correctly, but might be problem if you push
357 binary junk into Redis and expect to get it back without utf-8 flag turned on.
361 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
365 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
366 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
367 automatically be notified of progress on your bug as I make changes.
374 You can find documentation for this module with the perldoc command.
381 You can also look for information at:
385 =item * RT: CPAN's request tracker
387 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
389 =item * AnnoCPAN: Annotated CPAN documentation
391 L<http://annocpan.org/dist/Redis>
395 L<http://cpanratings.perl.org/d/Redis>
399 L<http://search.cpan.org/dist/Redis>
404 =head1 ACKNOWLEDGEMENTS
407 =head1 COPYRIGHT & LICENSE
409 Copyright 2009-2010 Dobrica Pavlinusic, all rights reserved.
411 This program is free software; you can redistribute it and/or modify it
412 under the same terms as Perl itself.