7 use Data::Dump qw/dump/;
12 Redis - perl binding for Redis database
16 our $VERSION = '0.08';
21 Pure perl bindings for L<http://code.google.com/p/redis/>
23 This version support git version 0.08 of Redis available at
25 L<git://github.com/antirez/redis>
28 lists commands which are exercised in test suite, but
29 additinal commands will work correctly since protocol
30 specifies enough information to support almost all commands
31 with same peace of code with a little help of C<AUTOLOAD>.
41 our $debug = $ENV{REDIS} || 0;
44 my $server = '127.0.0.1:6379';
51 warn "# opening socket to $server";
53 $sock ||= IO::Socket::INET->new(
63 rpush => 1, lpush => 1,
70 # we don't want DESTROY to fallback into AUTOLOAD
77 my $command = $AUTOLOAD;
80 warn "## $command ",dump(@_) if $debug;
84 if ( defined $bulk_command->{$command} ) {
103 warn ">> $send" if $debug;
106 if ( $command eq 'quit' ) {
107 close( $sock ) || die "can't close socket: $!";
111 my $result = <$sock> || die "can't read socket: $!";
112 warn "<< $result" if $debug;
113 my $type = substr($result,0,1);
114 $result = substr($result,1,-2);
116 if ( $command eq 'info' ) {
118 foreach my $l ( split(/\r\n/, __sock_read_bulk($result) ) ) {
119 my ($n,$v) = split(/:/, $l, 2);
123 } elsif ( $command eq 'keys' ) {
124 return split(/\s/, __sock_read_bulk($result));
127 if ( $type eq '-' ) {
129 } elsif ( $type eq '+' ) {
131 } elsif ( $type eq '$' ) {
132 return __sock_read_bulk($result);
133 } elsif ( $type eq '*' ) {
134 return __sock_read_multi_bulk($result);
135 } elsif ( $type eq ':' ) {
136 return $result; # FIXME check if int?
138 confess "unknown type: $type", __sock_read_line();
142 sub __sock_read_bulk {
144 return undef if $len < 0;
148 read($sock, $v, $len) || die $!;
149 warn "<< ",dump($v),$/ if $debug;
152 read($sock, $crlf, 2); # skip cr/lf
156 sub __sock_read_multi_bulk {
158 return undef if $size < 0;
162 my @list = ( 0 .. $size );
163 foreach ( 0 .. $size ) {
164 $list[ $_ ] = __sock_read_bulk( substr(<$sock>,1,-2) );
167 warn "## list = ", dump( @list ) if $debug;
175 =head1 Connection Handling
183 $r->ping || die "no server?";
185 =head1 Commands operating on string values
189 $r->set( foo => 'bar' );
191 $r->setnx( foo => 42 );
195 my $value = $r->get( 'foo' );
199 my @values = $r->mget( 'foo', 'bar', 'baz' );
205 $r->incrby('tripplets', 3);
211 $r->decrby('tripplets', 3);
215 $r->exists( 'key' ) && print "got key!";
219 $r->del( 'key' ) || warn "key doesn't exist";
223 $r->type( 'key' ); # = string
225 =head1 Commands operating on the key space
229 my @keys = $r->keys( '*glob_pattern*' );
233 my $key = $r->randomkey;
237 my $ok = $r->rename( 'old-key', 'new-key', $new );
241 my $nr_keys = $r->dbsize;
243 =head1 Commands operating on lists
245 See also L<Redis::List> for tie interface.
249 $r->rpush( $key, $value );
253 $r->lpush( $key, $value );
261 my @list = $r->lrange( $key, $start, $end );
265 my $ok = $r->ltrim( $key, $start, $end );
269 $r->lindex( $key, $index );
273 $r->lset( $key, $index, $value );
277 my $modified_count = $r->lrem( $key, $count, $value );
281 my $value = $r->lpop( $key );
285 my $value = $r->rpop( $key );
287 =head1 Commands operating on sets
291 $r->sadd( $key, $member );
295 $r->srem( $key, $member );
299 my $elements = $r->scard( $key );
303 $r->sismember( $key, $member );
307 $r->sinter( $key1, $key2, ... );
311 my $ok = $r->sinterstore( $dstkey, $key1, $key2, ... );
313 =head1 Multiple databases handling commands
317 $r->select( $dbindex ); # 0 for new clients
321 $r->move( $key, $dbindex );
335 $r->sort("key BY pattern LIMIT start end GET pattern ASC|DESC ALPHA');
337 =head1 Persistence control commands
355 =head1 Remote server control commands
359 my $info_hash = $r->info;
363 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
367 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
368 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
369 automatically be notified of progress on your bug as I make changes.
376 You can find documentation for this module with the perldoc command.
383 You can also look for information at:
387 =item * RT: CPAN's request tracker
389 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
391 =item * AnnoCPAN: Annotated CPAN documentation
393 L<http://annocpan.org/dist/Redis>
397 L<http://cpanratings.perl.org/d/Redis>
401 L<http://search.cpan.org/dist/Redis>
406 =head1 ACKNOWLEDGEMENTS
409 =head1 COPYRIGHT & LICENSE
411 Copyright 2009 Dobrica Pavlinusic, all rights reserved.
413 This program is free software; you can redistribute it and/or modify it
414 under the same terms as Perl itself.