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";
61 sub _sock_result_bulk {
63 warn "# len: ",dump($len);
64 return undef if $len eq "nil\r\n";
66 read($sock, $v, $len) || die $!;
67 warn "# v: ",dump($v);
69 read($sock, $crlf, 2); # skip cr/lf
75 confess dump($ok) unless $ok eq "+OK\r\n";
80 print $sock join(' ',@_) . "\r\n";
85 my ( $self, $command, $key, $value ) = @_;
86 print $sock "$command $key " . length($value) . "\r\n$value\r\n";
91 =head1 Connection Handling
102 close( $sock ) || warn $!;
107 $r->ping || die "no server?";
112 print $sock "PING\r\n";
114 die "ping failed, got ", dump($pong) unless $pong eq "+PONG\r\n";
117 =head1 Commands operating on string values
121 $r->set( foo => 'bar', $new );
126 my ( $self, $key, $value, $new ) = @_;
127 $self->_sock_send_bulk( "SET" . ( $new ? 'NX' : '' ), $key, $value );
132 my $value = $r->get( 'foo' );
137 my ( $self, $k ) = @_;
138 print $sock "GET $k\r\n";
145 $r->incr('tripplets', 3);
152 my ( $self, $key, $value ) = @_;
153 if ( defined $value ) {
154 print $sock "INCRBY $key $value\r\n";
156 print $sock "INCR $key\r\n";
164 $r->decr('tripplets', 3);
169 my ( $self, $key, $value ) = @_;
170 if ( defined $value ) {
171 print $sock "DECRBY $key $value\r\n";
173 print $sock "DECR $key\r\n";
180 $r->exists( 'key' ) && print "got key!";
185 my ( $self, $key ) = @_;
186 print $sock "EXISTS $key\r\n";
192 $r->del( 'key' ) || warn "key doesn't exist";
197 my ( $self, $key ) = @_;
198 print $sock "DEL $key\r\n";
204 $r->type( 'key' ); # = string
209 my ( $self, $key ) = @_;
210 print $sock "TYPE $key\r\n";
214 =head1 Commands operating on the key space
218 my @keys = $r->keys( '*glob_pattern*' );
223 my ( $self, $glob ) = @_;
224 print $sock "KEYS $glob\r\n";
225 return split(/\s/, _sock_result_bulk());
230 my $key = $r->randomkey;
236 print $sock "RANDOMKEY\r\n";
242 my $ok = $r->rename( 'old-key', 'new-key', $new );
247 my ( $self, $old, $new, $nx ) = @_;
248 print $sock "RENAME" . ( $nx ? 'NX' : '' ) . " $old $new\r\n";
254 my $nr_keys = $r->dbsize;
260 print $sock "DBSIZE\r\n";
264 =head1 Commands operating on lists
268 $r->rpush( $key, $value );
273 my ( $self, $key, $value ) = @_;
274 $self->_sock_send_bulk('RPUSH', $key, $value);
279 $r->lpush( $key, $value );
284 my ( $self, $key, $value ) = @_;
285 $self->_sock_send_bulk('LPUSH', $key, $value);
295 my ( $self, $key ) = @_;
296 $self->_sock_send( 'llen', $key );
301 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
305 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
306 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
307 automatically be notified of progress on your bug as I make changes.
314 You can find documentation for this module with the perldoc command.
319 You can also look for information at:
323 =item * RT: CPAN's request tracker
325 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
327 =item * AnnoCPAN: Annotated CPAN documentation
329 L<http://annocpan.org/dist/Redis>
333 L<http://cpanratings.perl.org/d/Redis>
337 L<http://search.cpan.org/dist/Redis>
342 =head1 ACKNOWLEDGEMENTS
345 =head1 COPYRIGHT & LICENSE
347 Copyright 2009 Dobrica Pavlinusic, all rights reserved.
349 This program is free software; you can redistribute it and/or modify it
350 under the same terms as Perl itself.