7 use Data::Dump qw/dump/;
12 Redis - The great new Redis!
16 our $VERSION = '0.01';
21 Pure perl bindings for L<http://code.google.com/p/redis/>
37 my $server = '127.0.0.1:6379';
44 warn "# opening socket to $server";
46 $sock ||= IO::Socket::INET->new(
56 warn "# result: ",dump( $result );
57 $result =~ s{\r\n$}{} || warn "can't find cr/lf";
63 warn "## bulk len: ",dump($len);
64 return undef if $len eq "nil\r\n";
66 read($sock, $v, $len) || die $!;
67 warn "## bulk v: ",dump($v);
69 read($sock, $crlf, 2); # skip cr/lf
73 sub _sock_result_bulk {
75 warn "## _sock_result_bulk ",dump( @_ );
76 print $sock join(' ',@_) . "\r\n";
82 confess dump($ok) unless $ok eq "+OK\r\n";
87 warn "## _sock_send ",dump( @_ );
88 print $sock join(' ',@_) . "\r\n";
94 warn "## _sock_send_ok ",dump( @_ );
95 print $sock join(' ',@_) . "\r\n";
102 print $sock join(' ',@_) . ' ' . length($value) . "\r\n$value\r\n";
107 =head1 Connection Handling
118 close( $sock ) || warn $!;
123 $r->ping || die "no server?";
128 print $sock "PING\r\n";
130 die "ping failed, got ", dump($pong) unless $pong eq "+PONG\r\n";
133 =head1 Commands operating on string values
137 $r->set( foo => 'bar', $new );
142 my ( $self, $key, $value, $new ) = @_;
143 $self->_sock_send_bulk( "SET" . ( $new ? 'NX' : '' ), $key, $value );
148 my $value = $r->get( 'foo' );
154 $self->_sock_result_bulk('GET', @_);
160 $r->incr('tripplets', 3);
168 $self->_sock_send( 'INCR' . ( $#_ ? 'BY' : '' ), @_ );
174 $r->decr('tripplets', 3);
180 $self->_sock_send( 'DECR' . ( $#_ ? 'BY' : '' ), @_ );
185 $r->exists( 'key' ) && print "got key!";
190 my ( $self, $key ) = @_;
191 $self->_sock_send( 'EXISTS', $key );
196 $r->del( 'key' ) || warn "key doesn't exist";
201 my ( $self, $key ) = @_;
202 $self->_sock_send( 'DEL', $key );
207 $r->type( 'key' ); # = string
212 my ( $self, $key ) = @_;
213 $self->_sock_send( 'TYPE', $key );
216 =head1 Commands operating on the key space
220 my @keys = $r->keys( '*glob_pattern*' );
225 my ( $self, $glob ) = @_;
226 return split(/\s/, $self->_sock_result_bulk( 'KEYS', $glob ));
231 my $key = $r->randomkey;
237 $self->_sock_send( 'RANDOMKEY' );
242 my $ok = $r->rename( 'old-key', 'new-key', $new );
247 my ( $self, $old, $new, $nx ) = @_;
248 $self->_sock_send_ok( 'RENAME' . ( $nx ? 'NX' : '' ), $old, $new );
253 my $nr_keys = $r->dbsize;
259 $self->_sock_send('DBSIZE');
262 =head1 Commands operating on lists
266 $r->rpush( $key, $value );
271 my ( $self, $key, $value ) = @_;
272 $self->_sock_send_bulk('RPUSH', $key, $value);
277 $r->lpush( $key, $value );
282 my ( $self, $key, $value ) = @_;
283 $self->_sock_send_bulk('LPUSH', $key, $value);
293 my ( $self, $key ) = @_;
294 $self->_sock_send( 'LLEN', $key );
299 my @list = $r->lrange( $key, $start, $end );
304 my ( $self, $key, $start, $end ) = @_;
305 my $size = $self->_sock_send('LRANGE', $key, $start, $end);
307 confess $size unless $size > 0;
310 my @list = ( 0 .. $size );
311 foreach ( 0 .. $size ) {
312 $list[ $_ ] = _sock_read_bulk();
315 warn "## lrange $key $start $end = [$size] ", dump( @list );
321 my $ok = $r->ltrim( $key, $start, $end );
326 my ( $self, $key, $start, $end ) = @_;
327 $self->_sock_send_ok( 'LTRIM', $key, $start, $end );
332 $r->lindex( $key, $index );
337 my ( $self, $key, $index ) = @_;
338 $self->_sock_result_bulk( 'LINDEX', $key, $index );
343 $r->lset( $key, $index, $value );
348 my ( $self, $key, $index, $value ) = @_;
349 $self->_sock_send_bulk( 'LSET', $key, $index, $value );
354 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
358 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
359 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
360 automatically be notified of progress on your bug as I make changes.
367 You can find documentation for this module with the perldoc command.
372 You can also look for information at:
376 =item * RT: CPAN's request tracker
378 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
380 =item * AnnoCPAN: Annotated CPAN documentation
382 L<http://annocpan.org/dist/Redis>
386 L<http://cpanratings.perl.org/d/Redis>
390 L<http://search.cpan.org/dist/Redis>
395 =head1 ACKNOWLEDGEMENTS
398 =head1 COPYRIGHT & LICENSE
400 Copyright 2009 Dobrica Pavlinusic, all rights reserved.
402 This program is free software; you can redistribute it and/or modify it
403 under the same terms as Perl itself.