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->{server} ||= $ENV{REDIS_SERVER} || '127.0.0.1:6379';
52 $self->{sock} = IO::Socket::INET->new(
53 PeerAddr => $self->{server},
55 ) || confess("Could not connect to Redis server at $self->{server}: $!");
57 return bless($self, $class);
60 # we don't want DESTROY to fallback into AUTOLOAD
64 ### Deal with common, general case, Redis commands
68 my $sock = $self->{sock} || confess("Not connected to any server");
69 my $enc = $self->{encoding};
70 my $deb = $self->{debug};
72 my $command = $AUTOLOAD;
75 $self->__send_command($command, @_);
77 my $result = <$sock> || confess("Can't read socket: $!");
78 my $type = substr($result,0,1);
79 $result = substr($result,1,-2);
81 $result = decode($enc, $result) if $enc;
82 warn "[RECV] '$type$result'" if $deb;
84 if ( $command eq 'info' ) {
86 foreach my $l ( split(/\r\n/, $self->__read_bulk($result) ) ) {
87 my ($n,$v) = split(/:/, $l, 2);
91 } elsif ( $command eq 'keys' ) {
92 return $self->__read_multi_bulk($result)
94 my $keys = $self->__read_bulk($result);
95 return split(/\s/, $keys) if $keys;
100 confess "[$command] $result";
101 } elsif ( $type eq '+' ) {
103 } elsif ( $type eq '$' ) {
104 return $self->__read_bulk($result);
105 } elsif ( $type eq '*' ) {
106 return $self->__read_multi_bulk($result);
107 } elsif ( $type eq ':' ) {
108 return $result; # FIXME check if int?
110 confess "unknown type: $type", $self->__read_line();
115 ### Commands with extra logic
120 $self->__send_command('QUIT');
122 close(delete $self->{sock}) || confess("Can't close socket: $!");
127 ### Socket operations
132 my $enc = $self->{encoding};
133 my $deb = $self->{debug};
135 warn "[SEND] $cmd ", Dumper([@_]) if $deb;
137 ## Encode command using multi-bulk format
138 my $n_elems = scalar(@_) + 1;
139 my $buf = "\*$n_elems\r\n";
140 for my $elem ($cmd, @_) {
141 my $bin = $enc ? encode($enc, $elem) : $elem;
142 $buf .= defined($bin) ? '$' . length($bin) . "\r\n$bin\r\n" : "\$-1\r\n";
145 ## Send command, take care for partial writes
146 warn "[SEND RAW] $buf" if $deb;
147 my $sock = $self->{sock} || confess("Not connected to any server");
149 my $len = syswrite $sock, $buf, length $buf;
150 confess("Could not write to Redis server: $!")
152 substr $buf, 0, $len, "";
159 my ($self,$len) = @_;
162 my $enc = $self->{encoding};
165 read($self->{sock}, $v, $len) || confess("Could not read from sock: $!");
166 $v = decode($enc, $v) if $enc;
169 read($self->{sock}, $crlf, 2); # skip cr/lf
171 warn "[PARSE] read_bulk ".Dumper($v) if $self->{debug};
175 sub __read_multi_bulk {
176 my ($self,$size) = @_;
177 return if $size <= 0;
179 my $sock = $self->{sock};
180 my $deb = $self->{debug};
181 my $enc = $self->{encoding};
184 my $v = $self->__read_bulk( substr(<$sock>,1,-2) );
185 $v = decode($enc, $v) if $enc;
186 warn " [PARSE] read_multi_bulk ($size) ".Dumper($v) if $deb;
190 warn "[PARSE] multi_bulk ".Dumper( \@list ) if $deb;
198 =head1 Connection Handling
206 $r->ping || die "no server?";
208 =head1 Commands operating on string values
212 $r->set( foo => 'bar' );
214 $r->setnx( foo => 42 );
218 my $value = $r->get( 'foo' );
222 my @values = $r->mget( 'foo', 'bar', 'baz' );
228 $r->incrby('tripplets', 3);
234 $r->decrby('tripplets', 3);
238 $r->exists( 'key' ) && print "got key!";
242 $r->del( 'key' ) || warn "key doesn't exist";
246 $r->type( 'key' ); # = string
248 =head1 Commands operating on the key space
252 my @keys = $r->keys( '*glob_pattern*' );
256 my $key = $r->randomkey;
260 my $ok = $r->rename( 'old-key', 'new-key', $new );
264 my $nr_keys = $r->dbsize;
266 =head1 Commands operating on lists
268 See also L<Redis::List> for tie interface.
272 $r->rpush( $key, $value );
276 $r->lpush( $key, $value );
284 my @list = $r->lrange( $key, $start, $end );
288 my $ok = $r->ltrim( $key, $start, $end );
292 $r->lindex( $key, $index );
296 $r->lset( $key, $index, $value );
300 my $modified_count = $r->lrem( $key, $count, $value );
304 my $value = $r->lpop( $key );
308 my $value = $r->rpop( $key );
310 =head1 Commands operating on sets
314 $r->sadd( $key, $member );
318 $r->srem( $key, $member );
322 my $elements = $r->scard( $key );
326 $r->sismember( $key, $member );
330 $r->sinter( $key1, $key2, ... );
334 my $ok = $r->sinterstore( $dstkey, $key1, $key2, ... );
336 =head1 Multiple databases handling commands
340 $r->select( $dbindex ); # 0 for new clients
344 $r->move( $key, $dbindex );
358 $r->sort("key BY pattern LIMIT start end GET pattern ASC|DESC ALPHA');
360 =head1 Persistence control commands
378 =head1 Remote server control commands
382 my $info_hash = $r->info;
386 Since Redis knows nothing about encoding, we are forcing utf-8 flag on all data received from Redis.
387 This change is introduced in 1.2001 version.
389 This allows us to round-trip utf-8 encoded characters correctly, but might be problem if you push
390 binary junk into Redis and expect to get it back without utf-8 flag turned on.
394 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
398 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
399 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
400 automatically be notified of progress on your bug as I make changes.
407 You can find documentation for this module with the perldoc command.
414 You can also look for information at:
418 =item * RT: CPAN's request tracker
420 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
422 =item * AnnoCPAN: Annotated CPAN documentation
424 L<http://annocpan.org/dist/Redis>
428 L<http://cpanratings.perl.org/d/Redis>
432 L<http://search.cpan.org/dist/Redis>
437 =head1 ACKNOWLEDGEMENTS
440 =head1 COPYRIGHT & LICENSE
442 Copyright 2009-2010 Dobrica Pavlinusic, all rights reserved.
444 This program is free software; you can redistribute it and/or modify it
445 under the same terms as Perl itself.