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
66 my $sock = $self->{sock} || confess("Not connected to any server");
67 my $enc = $self->{encoding};
68 my $deb = $self->{debug};
70 my $command = $AUTOLOAD;
72 warn "[SEND] $command ",Dumper([@_]) if $deb;
74 my $n_elems = scalar(@_)+1;
75 my $send = "\*$n_elems\r\n";
76 for my $str (uc($command), @_) {
77 my $bin = $enc? encode($enc, $str) : $str;
78 $send .= defined($bin)? '$'.length($bin)."\r\n$bin\r\n" : "\$-1\r\n";
81 warn "[SEND RAW] $send" if $deb;
84 if ( $command eq 'quit' ) {
85 close( $sock ) || confess("Can't close socket: $!");
89 my $result = <$sock> || confess("Can't read socket: $!");
90 my $type = substr($result,0,1);
91 $result = substr($result,1,-2);
93 $result = decode($enc, $result) if $enc;
94 warn "[RECV] '$type$result'" if $deb;
96 if ( $command eq 'info' ) {
98 foreach my $l ( split(/\r\n/, $self->__read_bulk($result) ) ) {
99 my ($n,$v) = split(/:/, $l, 2);
103 } elsif ( $command eq 'keys' ) {
104 return $self->__read_multi_bulk($result)
106 my $keys = $self->__read_bulk($result);
107 return split(/\s/, $keys) if $keys;
111 if ( $type eq '-' ) {
112 confess "[$command] $result";
113 } elsif ( $type eq '+' ) {
115 } elsif ( $type eq '$' ) {
116 return $self->__read_bulk($result);
117 } elsif ( $type eq '*' ) {
118 return $self->__read_multi_bulk($result);
119 } elsif ( $type eq ':' ) {
120 return $result; # FIXME check if int?
122 confess "unknown type: $type", $self->__read_line();
127 my ($self,$len) = @_;
130 my $enc = $self->{encoding};
133 read($self->{sock}, $v, $len) || confess("Could not read from sock: $!");
134 $v = decode($enc, $v) if $enc;
137 read($self->{sock}, $crlf, 2); # skip cr/lf
139 warn "[PARSE] read_bulk ".Dumper($v) if $self->{debug};
143 sub __read_multi_bulk {
144 my ($self,$size) = @_;
145 return if $size <= 0;
147 my $sock = $self->{sock};
148 my $deb = $self->{debug};
149 my $enc = $self->{encoding};
152 my $v = $self->__read_bulk( substr(<$sock>,1,-2) );
153 $v = decode($enc, $v) if $enc;
154 warn " [PARSE] read_multi_bulk ($size) ".Dumper($v) if $deb;
158 warn "[PARSE] multi_bulk ".Dumper( \@list ) if $deb;
166 =head1 Connection Handling
174 $r->ping || die "no server?";
176 =head1 Commands operating on string values
180 $r->set( foo => 'bar' );
182 $r->setnx( foo => 42 );
186 my $value = $r->get( 'foo' );
190 my @values = $r->mget( 'foo', 'bar', 'baz' );
196 $r->incrby('tripplets', 3);
202 $r->decrby('tripplets', 3);
206 $r->exists( 'key' ) && print "got key!";
210 $r->del( 'key' ) || warn "key doesn't exist";
214 $r->type( 'key' ); # = string
216 =head1 Commands operating on the key space
220 my @keys = $r->keys( '*glob_pattern*' );
224 my $key = $r->randomkey;
228 my $ok = $r->rename( 'old-key', 'new-key', $new );
232 my $nr_keys = $r->dbsize;
234 =head1 Commands operating on lists
236 See also L<Redis::List> for tie interface.
240 $r->rpush( $key, $value );
244 $r->lpush( $key, $value );
252 my @list = $r->lrange( $key, $start, $end );
256 my $ok = $r->ltrim( $key, $start, $end );
260 $r->lindex( $key, $index );
264 $r->lset( $key, $index, $value );
268 my $modified_count = $r->lrem( $key, $count, $value );
272 my $value = $r->lpop( $key );
276 my $value = $r->rpop( $key );
278 =head1 Commands operating on sets
282 $r->sadd( $key, $member );
286 $r->srem( $key, $member );
290 my $elements = $r->scard( $key );
294 $r->sismember( $key, $member );
298 $r->sinter( $key1, $key2, ... );
302 my $ok = $r->sinterstore( $dstkey, $key1, $key2, ... );
304 =head1 Multiple databases handling commands
308 $r->select( $dbindex ); # 0 for new clients
312 $r->move( $key, $dbindex );
326 $r->sort("key BY pattern LIMIT start end GET pattern ASC|DESC ALPHA');
328 =head1 Persistence control commands
346 =head1 Remote server control commands
350 my $info_hash = $r->info;
354 Since Redis knows nothing about encoding, we are forcing utf-8 flag on all data received from Redis.
355 This change is introduced in 1.2001 version.
357 This allows us to round-trip utf-8 encoded characters correctly, but might be problem if you push
358 binary junk into Redis and expect to get it back without utf-8 flag turned on.
362 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
366 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
367 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
368 automatically be notified of progress on your bug as I make changes.
375 You can find documentation for this module with the perldoc command.
382 You can also look for information at:
386 =item * RT: CPAN's request tracker
388 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
390 =item * AnnoCPAN: Annotated CPAN documentation
392 L<http://annocpan.org/dist/Redis>
396 L<http://cpanratings.perl.org/d/Redis>
400 L<http://search.cpan.org/dist/Redis>
405 =head1 ACKNOWLEDGEMENTS
408 =head1 COPYRIGHT & LICENSE
410 Copyright 2009-2010 Dobrica Pavlinusic, all rights reserved.
412 This program is free software; you can redistribute it and/or modify it
413 under the same terms as Perl itself.