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";
78 =head1 Connection Handling
89 close( $sock ) || warn $!;
94 $r->ping || die "no server?";
99 print $sock "PING\r\n";
101 die "ping failed, got ", dump($pong) unless $pong eq "+PONG\r\n";
104 =head1 Commands operating on string values
108 $r->set( foo => 'bar', $new );
113 my ( $self, $k, $v, $new ) = @_;
114 print $sock ( $new ? "SETNX" : "SET" ) . " $k " . length($v) . "\r\n$v\r\n";
120 my $value = $r->get( 'foo' );
125 my ( $self, $k ) = @_;
126 print $sock "GET $k\r\n";
133 $r->incr('tripplets', 3);
140 my ( $self, $key, $value ) = @_;
141 if ( defined $value ) {
142 print $sock "INCRBY $key $value\r\n";
144 print $sock "INCR $key\r\n";
152 $r->decr('tripplets', 3);
157 my ( $self, $key, $value ) = @_;
158 if ( defined $value ) {
159 print $sock "DECRBY $key $value\r\n";
161 print $sock "DECR $key\r\n";
168 $r->exists( 'key' ) && print "got key!";
173 my ( $self, $key ) = @_;
174 print $sock "EXISTS $key\r\n";
180 $r->del( 'key' ) || warn "key doesn't exist";
185 my ( $self, $key ) = @_;
186 print $sock "DEL $key\r\n";
192 $r->type( 'key' ); # = string
197 my ( $self, $key ) = @_;
198 print $sock "TYPE $key\r\n";
202 =head1 Commands operating on the key space
206 my @keys = $r->keys( '*glob_pattern*' );
211 my ( $self, $glob ) = @_;
212 print $sock "KEYS $glob\r\n";
213 return split(/\s/, _sock_result_bulk());
218 my $key = $r->randomkey;
224 print $sock "RANDOMKEY\r\n";
230 my $ok = $r->rename( 'old-key', 'new-key', $only_if_new );
235 my ( $self, $old, $new, $nx ) = @_;
236 print $sock "RENAME" . ( $nx ? 'NX' : '' ) . " $old $new\r\n";
242 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
246 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
247 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
248 automatically be notified of progress on your bug as I make changes.
255 You can find documentation for this module with the perldoc command.
260 You can also look for information at:
264 =item * RT: CPAN's request tracker
266 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
268 =item * AnnoCPAN: Annotated CPAN documentation
270 L<http://annocpan.org/dist/Redis>
274 L<http://cpanratings.perl.org/d/Redis>
278 L<http://search.cpan.org/dist/Redis>
283 =head1 ACKNOWLEDGEMENTS
286 =head1 COPYRIGHT & LICENSE
288 Copyright 2009 Dobrica Pavlinusic, all rights reserved.
290 This program is free software; you can redistribute it and/or modify it
291 under the same terms as Perl itself.