]>
git.saurik.com Git - redis.git/blob - client-libraries/perl/lib/Redis.pm
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} ) {
86 $value = '' if ! defined $value;
104 warn ">> $send" if $debug;
107 if ( $command eq 'quit' ) {
108 close( $sock ) || die "can't close socket: $!";
112 my $result = <$sock> || die "can't read socket: $!";
113 warn "<< $result" if $debug;
114 my $type = substr($result,0,1);
115 $result = substr($result,1,-2);
117 if ( $command eq 'info' ) {
119 foreach my $l ( split(/\r\n/, __sock_read_bulk
($result) ) ) {
120 my ($n,$v) = split(/:/, $l, 2);
124 } elsif ( $command eq 'keys' ) {
125 my $keys = __sock_read_bulk
($result);
126 return split(/\s/, $keys) if $keys;
130 if ( $type eq '-' ) {
132 } elsif ( $type eq '+' ) {
134 } elsif ( $type eq '$' ) {
135 return __sock_read_bulk
($result);
136 } elsif ( $type eq '*' ) {
137 return __sock_read_multi_bulk
($result);
138 } elsif ( $type eq ':' ) {
139 return $result; # FIXME check if int?
141 confess
"unknown type: $type", __sock_read_line
();
145 sub __sock_read_bulk
{
147 return undef if $len < 0;
151 read($sock, $v, $len) || die $!;
152 warn "<< ",dump($v),$/ if $debug;
155 read($sock, $crlf, 2); # skip cr/lf
159 sub __sock_read_multi_bulk
{
161 return undef if $size < 0;
165 my @list = ( 0 .. $size );
166 foreach ( 0 .. $size ) {
167 $list[ $_ ] = __sock_read_bulk
( substr(<$sock>,1,-2) );
170 warn "## list = ", dump( @list ) if $debug;
178 =head1 Connection Handling
186 $r->ping || die "no server?";
188 =head1 Commands operating on string values
192 $r->set( foo => 'bar' );
194 $r->setnx( foo => 42 );
198 my $value = $r->get( 'foo' );
202 my @values = $r->mget( 'foo', 'bar', 'baz' );
208 $r->incrby('tripplets', 3);
214 $r->decrby('tripplets', 3);
218 $r->exists( 'key' ) && print "got key!";
222 $r->del( 'key' ) || warn "key doesn't exist";
226 $r->type( 'key' ); # = string
228 =head1 Commands operating on the key space
232 my @keys = $r->keys( '*glob_pattern*' );
236 my $key = $r->randomkey;
240 my $ok = $r->rename( 'old-key', 'new-key', $new );
244 my $nr_keys = $r->dbsize;
246 =head1 Commands operating on lists
248 See also L<Redis::List> for tie interface.
252 $r->rpush( $key, $value );
256 $r->lpush( $key, $value );
264 my @list = $r->lrange( $key, $start, $end );
268 my $ok = $r->ltrim( $key, $start, $end );
272 $r->lindex( $key, $index );
276 $r->lset( $key, $index, $value );
280 my $modified_count = $r->lrem( $key, $count, $value );
284 my $value = $r->lpop( $key );
288 my $value = $r->rpop( $key );
290 =head1 Commands operating on sets
294 $r->sadd( $key, $member );
298 $r->srem( $key, $member );
302 my $elements = $r->scard( $key );
306 $r->sismember( $key, $member );
310 $r->sinter( $key1, $key2, ... );
314 my $ok = $r->sinterstore( $dstkey, $key1, $key2, ... );
316 =head1 Multiple databases handling commands
320 $r->select( $dbindex ); # 0 for new clients
324 $r->move( $key, $dbindex );
338 $r->sort("key BY pattern LIMIT start end GET pattern ASC|DESC ALPHA');
340 =head1 Persistence control commands
358 =head1 Remote server control commands
362 my $info_hash = $r->info;
366 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
370 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
371 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
372 automatically be notified of progress on your bug as I make changes.
379 You can find documentation for this module with the perldoc command.
386 You can also look for information at:
390 =item * RT: CPAN's request tracker
392 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
394 =item * AnnoCPAN: Annotated CPAN documentation
396 L<http://annocpan.org/dist/Redis>
400 L<http://cpanratings.perl.org/d/Redis>
404 L<http://search.cpan.org/dist/Redis>
409 =head1 ACKNOWLEDGEMENTS
412 =head1 COPYRIGHT & LICENSE
414 Copyright 2009 Dobrica Pavlinusic, all rights reserved.
416 This program is free software; you can redistribute it and/or modify it
417 under the same terms as Perl itself.