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";
79 my ( $self, $command, $key, $value ) = @_;
80 print $sock "$command $key " . length($value) . "\r\n$value\r\n";
85 =head1 Connection Handling
96 close( $sock ) || warn $!;
101 $r->ping || die "no server?";
106 print $sock "PING\r\n";
108 die "ping failed, got ", dump($pong) unless $pong eq "+PONG\r\n";
111 =head1 Commands operating on string values
115 $r->set( foo => 'bar', $new );
120 my ( $self, $key, $value, $new ) = @_;
121 $self->_sock_send_bulk( "SET" . ( $new ? 'NX' : '' ), $key, $value );
126 my $value = $r->get( 'foo' );
131 my ( $self, $k ) = @_;
132 print $sock "GET $k\r\n";
139 $r->incr('tripplets', 3);
146 my ( $self, $key, $value ) = @_;
147 if ( defined $value ) {
148 print $sock "INCRBY $key $value\r\n";
150 print $sock "INCR $key\r\n";
158 $r->decr('tripplets', 3);
163 my ( $self, $key, $value ) = @_;
164 if ( defined $value ) {
165 print $sock "DECRBY $key $value\r\n";
167 print $sock "DECR $key\r\n";
174 $r->exists( 'key' ) && print "got key!";
179 my ( $self, $key ) = @_;
180 print $sock "EXISTS $key\r\n";
186 $r->del( 'key' ) || warn "key doesn't exist";
191 my ( $self, $key ) = @_;
192 print $sock "DEL $key\r\n";
198 $r->type( 'key' ); # = string
203 my ( $self, $key ) = @_;
204 print $sock "TYPE $key\r\n";
208 =head1 Commands operating on the key space
212 my @keys = $r->keys( '*glob_pattern*' );
217 my ( $self, $glob ) = @_;
218 print $sock "KEYS $glob\r\n";
219 return split(/\s/, _sock_result_bulk());
224 my $key = $r->randomkey;
230 print $sock "RANDOMKEY\r\n";
236 my $ok = $r->rename( 'old-key', 'new-key', $new );
241 my ( $self, $old, $new, $nx ) = @_;
242 print $sock "RENAME" . ( $nx ? 'NX' : '' ) . " $old $new\r\n";
248 my $nr_keys = $r->dbsize;
254 print $sock "DBSIZE\r\n";
258 =head1 Commands operating on lists
262 $r->rpush( $key, $value );
267 my ( $self, $key, $value ) = @_;
268 $self->_sock_send_bulk('RPUSH', $key, $value);
273 $r->lpush( $key, $value );
278 my ( $self, $key, $value ) = @_;
279 $self->_sock_send_bulk('LPUSH', $key, $value);
284 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
288 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
289 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
290 automatically be notified of progress on your bug as I make changes.
297 You can find documentation for this module with the perldoc command.
302 You can also look for information at:
306 =item * RT: CPAN's request tracker
308 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
310 =item * AnnoCPAN: Annotated CPAN documentation
312 L<http://annocpan.org/dist/Redis>
316 L<http://cpanratings.perl.org/d/Redis>
320 L<http://search.cpan.org/dist/Redis>
325 =head1 ACKNOWLEDGEMENTS
328 =head1 COPYRIGHT & LICENSE
330 Copyright 2009 Dobrica Pavlinusic, all rights reserved.
332 This program is free software; you can redistribute it and/or modify it
333 under the same terms as Perl itself.