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};
68 my $command = $AUTOLOAD;
70 warn "## $command ",Dumper([@_]) if $self->{debug};
72 my $n_elems = scalar(@_)+1;
73 my $send = "\*$n_elems\r\n";
74 for my $str (uc($command), @_) {
75 my $bin = $enc? encode($enc, $str) : $str;
76 $send .= defined($bin)? '$'.length($bin)."\r\n$bin\r\n" : "\$-1\r\n";
79 warn ">> $send" if $self->{debug};
82 if ( $command eq 'quit' ) {
83 close( $sock ) || die "can't close socket: $!";
87 my $result = <$sock> || die "can't read socket: $!";
88 my $type = substr($result,0,1);
89 $result = substr($result,1,-2);
91 $result = decode($enc, $result) if $enc;
92 warn "<< Response: '$type$result'," if $self->{debug};
94 if ( $command eq 'info' ) {
96 foreach my $l ( split(/\r\n/, $self->__read_bulk($result) ) ) {
97 my ($n,$v) = split(/:/, $l, 2);
101 } elsif ( $command eq 'keys' ) {
102 return $self->__read_multi_bulk($result)
104 my $keys = $self->__read_bulk($result);
105 return split(/\s/, $keys) if $keys;
109 if ( $type eq '-' ) {
110 confess "[$command] $result";
111 } elsif ( $type eq '+' ) {
113 } elsif ( $type eq '$' ) {
114 return $self->__read_bulk($result);
115 } elsif ( $type eq '*' ) {
116 return $self->__read_multi_bulk($result);
117 } elsif ( $type eq ':' ) {
118 return $result; # FIXME check if int?
120 confess "unknown type: $type", $self->__read_line();
125 my ($self,$len) = @_;
128 my $enc = $self->{encoding};
131 read($self->{sock}, $v, $len) || die $!;
132 $v = decode($enc, $v) if $enc;
133 warn "<< read_bulk ".Dumper($v) if $self->{debug};
136 read($self->{sock}, $crlf, 2); # skip cr/lf
140 sub __read_multi_bulk {
141 my ($self,$size) = @_;
142 return if $size <= 0;
144 my $sock = $self->{sock};
145 my $enc = $self->{encoding};
148 my $v = $self->__read_bulk( substr(<$sock>,1,-2) );
149 $v = decode($enc, $v) if $enc;
150 warn "<< read_multi_bulk ($size) ".Dumper($v) if $self->{debug};
154 warn "<< multi_bunk list = ".Dumper( \@list ) if $self->{debug};
162 =head1 Connection Handling
170 $r->ping || die "no server?";
172 =head1 Commands operating on string values
176 $r->set( foo => 'bar' );
178 $r->setnx( foo => 42 );
182 my $value = $r->get( 'foo' );
186 my @values = $r->mget( 'foo', 'bar', 'baz' );
192 $r->incrby('tripplets', 3);
198 $r->decrby('tripplets', 3);
202 $r->exists( 'key' ) && print "got key!";
206 $r->del( 'key' ) || warn "key doesn't exist";
210 $r->type( 'key' ); # = string
212 =head1 Commands operating on the key space
216 my @keys = $r->keys( '*glob_pattern*' );
220 my $key = $r->randomkey;
224 my $ok = $r->rename( 'old-key', 'new-key', $new );
228 my $nr_keys = $r->dbsize;
230 =head1 Commands operating on lists
232 See also L<Redis::List> for tie interface.
236 $r->rpush( $key, $value );
240 $r->lpush( $key, $value );
248 my @list = $r->lrange( $key, $start, $end );
252 my $ok = $r->ltrim( $key, $start, $end );
256 $r->lindex( $key, $index );
260 $r->lset( $key, $index, $value );
264 my $modified_count = $r->lrem( $key, $count, $value );
268 my $value = $r->lpop( $key );
272 my $value = $r->rpop( $key );
274 =head1 Commands operating on sets
278 $r->sadd( $key, $member );
282 $r->srem( $key, $member );
286 my $elements = $r->scard( $key );
290 $r->sismember( $key, $member );
294 $r->sinter( $key1, $key2, ... );
298 my $ok = $r->sinterstore( $dstkey, $key1, $key2, ... );
300 =head1 Multiple databases handling commands
304 $r->select( $dbindex ); # 0 for new clients
308 $r->move( $key, $dbindex );
322 $r->sort("key BY pattern LIMIT start end GET pattern ASC|DESC ALPHA');
324 =head1 Persistence control commands
342 =head1 Remote server control commands
346 my $info_hash = $r->info;
350 Since Redis knows nothing about encoding, we are forcing utf-8 flag on all data received from Redis.
351 This change is introduced in 1.2001 version.
353 This allows us to round-trip utf-8 encoded characters correctly, but might be problem if you push
354 binary junk into Redis and expect to get it back without utf-8 flag turned on.
358 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
362 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
363 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
364 automatically be notified of progress on your bug as I make changes.
371 You can find documentation for this module with the perldoc command.
378 You can also look for information at:
382 =item * RT: CPAN's request tracker
384 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
386 =item * AnnoCPAN: Annotated CPAN documentation
388 L<http://annocpan.org/dist/Redis>
392 L<http://cpanratings.perl.org/d/Redis>
396 L<http://search.cpan.org/dist/Redis>
401 =head1 ACKNOWLEDGEMENTS
404 =head1 COPYRIGHT & LICENSE
406 Copyright 2009-2010 Dobrica Pavlinusic, all rights reserved.
408 This program is free software; you can redistribute it and/or modify it
409 under the same terms as Perl itself.