/[Redis.pre-github]/lib/Redis.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /lib/Redis.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 40 - (hide annotations)
Sun Mar 22 18:39:06 2009 UTC (15 years, 1 month ago) by dpavlin
File size: 8641 byte(s)
move
1 dpavlin 1 package Redis;
2    
3     use warnings;
4     use strict;
5    
6 dpavlin 2 use IO::Socket::INET;
7     use Data::Dump qw/dump/;
8 dpavlin 5 use Carp qw/confess/;
9 dpavlin 2
10 dpavlin 1 =head1 NAME
11    
12 dpavlin 36 Redis - perl binding for Redis database
13 dpavlin 1
14     =cut
15    
16     our $VERSION = '0.01';
17    
18    
19     =head1 SYNOPSIS
20    
21 dpavlin 2 Pure perl bindings for L<http://code.google.com/p/redis/>
22 dpavlin 1
23 dpavlin 36 This version support git version of Redis available at
24     L<git://github.com/antirez/redis>
25    
26 dpavlin 1 use Redis;
27    
28 dpavlin 2 my $r = Redis->new();
29 dpavlin 1
30     =head1 FUNCTIONS
31    
32 dpavlin 2 =head2 new
33 dpavlin 1
34     =cut
35    
36 dpavlin 37 our $debug = $ENV{REDIS} || 0;
37    
38 dpavlin 2 our $sock;
39     my $server = '127.0.0.1:6379';
40    
41     sub new {
42     my $class = shift;
43     my $self = {};
44     bless($self, $class);
45    
46     warn "# opening socket to $server";
47    
48     $sock ||= IO::Socket::INET->new(
49     PeerAddr => $server,
50     Proto => 'tcp',
51     ) || die $!;
52    
53     $self;
54 dpavlin 1 }
55    
56 dpavlin 40 sub __sock_result {
57 dpavlin 11 my $result = <$sock>;
58 dpavlin 37 warn "## result: ",dump( $result ) if $debug;
59 dpavlin 11 $result =~ s{\r\n$}{} || warn "can't find cr/lf";
60     return $result;
61     }
62    
63 dpavlin 40 sub __sock_read_bulk {
64 dpavlin 12 my $len = <$sock>;
65 dpavlin 37 warn "## bulk len: ",dump($len) if $debug;
66 dpavlin 12 return undef if $len eq "nil\r\n";
67     my $v;
68 dpavlin 28 if ( $len > 0 ) {
69     read($sock, $v, $len) || die $!;
70 dpavlin 37 warn "## bulk v: ",dump($v) if $debug;
71 dpavlin 28 }
72 dpavlin 12 my $crlf;
73     read($sock, $crlf, 2); # skip cr/lf
74     return $v;
75     }
76    
77 dpavlin 21 sub _sock_result_bulk {
78     my $self = shift;
79 dpavlin 37 warn "## _sock_result_bulk ",dump( @_ ) if $debug;
80 dpavlin 21 print $sock join(' ',@_) . "\r\n";
81 dpavlin 40 __sock_read_bulk();
82 dpavlin 21 }
83    
84 dpavlin 33 sub _sock_result_bulk_list {
85     my $self = shift;
86 dpavlin 37 warn "## _sock_result_bulk_list ",dump( @_ ) if $debug;
87 dpavlin 33
88     my $size = $self->_sock_send( @_ );
89     confess $size unless $size > 0;
90     $size--;
91    
92     my @list = ( 0 .. $size );
93     foreach ( 0 .. $size ) {
94 dpavlin 40 $list[ $_ ] = __sock_read_bulk();
95 dpavlin 33 }
96    
97 dpavlin 37 warn "## list = ", dump( @list ) if $debug;
98 dpavlin 33 return @list;
99     }
100    
101 dpavlin 26 sub __sock_ok {
102 dpavlin 14 my $ok = <$sock>;
103 dpavlin 28 return undef if $ok eq "nil\r\n";
104 dpavlin 14 confess dump($ok) unless $ok eq "+OK\r\n";
105     }
106    
107 dpavlin 20 sub _sock_send {
108     my $self = shift;
109 dpavlin 37 warn "## _sock_send ",dump( @_ ) if $debug;
110 dpavlin 20 print $sock join(' ',@_) . "\r\n";
111 dpavlin 40 __sock_result();
112 dpavlin 20 }
113    
114 dpavlin 21 sub _sock_send_ok {
115     my $self = shift;
116 dpavlin 37 warn "## _sock_send_ok ",dump( @_ ) if $debug;
117 dpavlin 21 print $sock join(' ',@_) . "\r\n";
118 dpavlin 26 __sock_ok();
119 dpavlin 21 }
120    
121 dpavlin 26 sub __sock_send_bulk_raw {
122 dpavlin 37 warn "## _sock_send_bulk ",dump( @_ ) if $debug;
123 dpavlin 24 my $value = pop;
124 dpavlin 28 $value = '' unless defined $value; # FIXME errr? nil?
125     print $sock join(' ',@_) . ' ' . length($value) . "\r\n$value\r\n"
126 dpavlin 18 }
127    
128 dpavlin 26 sub _sock_send_bulk {
129 dpavlin 40 my $self = shift;
130 dpavlin 26 __sock_send_bulk_raw( @_ );
131     __sock_ok();
132     }
133 dpavlin 18
134 dpavlin 26 sub _sock_send_bulk_number {
135 dpavlin 40 my $self = shift;
136 dpavlin 26 __sock_send_bulk_raw( @_ );
137 dpavlin 40 my $v = __sock_result();
138 dpavlin 26 confess $v unless $v =~ m{^\-?\d+$};
139     return $v;
140     }
141    
142 dpavlin 2 =head1 Connection Handling
143 dpavlin 1
144 dpavlin 2 =head2 quit
145    
146     $r->quit;
147    
148 dpavlin 1 =cut
149    
150 dpavlin 2 sub quit {
151     my $self = shift;
152    
153     close( $sock ) || warn $!;
154 dpavlin 1 }
155    
156 dpavlin 2 =head2 ping
157    
158 dpavlin 3 $r->ping || die "no server?";
159 dpavlin 2
160     =cut
161    
162     sub ping {
163     print $sock "PING\r\n";
164     my $pong = <$sock>;
165     die "ping failed, got ", dump($pong) unless $pong eq "+PONG\r\n";
166     }
167    
168 dpavlin 3 =head1 Commands operating on string values
169    
170     =head2 set
171    
172 dpavlin 5 $r->set( foo => 'bar', $new );
173 dpavlin 3
174     =cut
175    
176     sub set {
177 dpavlin 18 my ( $self, $key, $value, $new ) = @_;
178     $self->_sock_send_bulk( "SET" . ( $new ? 'NX' : '' ), $key, $value );
179 dpavlin 3 }
180    
181     =head2 get
182    
183     my $value = $r->get( 'foo' );
184    
185     =cut
186    
187     sub get {
188 dpavlin 21 my $self = shift;
189     $self->_sock_result_bulk('GET', @_);
190 dpavlin 3 }
191    
192 dpavlin 7 =head2 incr
193 dpavlin 4
194 dpavlin 7 $r->incr('counter');
195     $r->incr('tripplets', 3);
196 dpavlin 4
197 dpavlin 7 =cut
198    
199 dpavlin 10
200    
201 dpavlin 7 sub incr {
202 dpavlin 21 my $self = shift;
203     $self->_sock_send( 'INCR' . ( $#_ ? 'BY' : '' ), @_ );
204 dpavlin 7 }
205    
206 dpavlin 8 =head2 decr
207    
208     $r->decr('counter');
209     $r->decr('tripplets', 3);
210    
211     =cut
212    
213     sub decr {
214 dpavlin 21 my $self = shift;
215     $self->_sock_send( 'DECR' . ( $#_ ? 'BY' : '' ), @_ );
216 dpavlin 8 }
217    
218 dpavlin 9 =head2 exists
219    
220     $r->exists( 'key' ) && print "got key!";
221    
222     =cut
223    
224     sub exists {
225     my ( $self, $key ) = @_;
226 dpavlin 21 $self->_sock_send( 'EXISTS', $key );
227 dpavlin 9 }
228    
229 dpavlin 10 =head2 del
230    
231     $r->del( 'key' ) || warn "key doesn't exist";
232    
233     =cut
234    
235     sub del {
236     my ( $self, $key ) = @_;
237 dpavlin 21 $self->_sock_send( 'DEL', $key );
238 dpavlin 10 }
239    
240 dpavlin 11 =head2 type
241    
242     $r->type( 'key' ); # = string
243    
244     =cut
245    
246     sub type {
247     my ( $self, $key ) = @_;
248 dpavlin 21 $self->_sock_send( 'TYPE', $key );
249 dpavlin 11 }
250    
251 dpavlin 12 =head1 Commands operating on the key space
252    
253     =head2 keys
254    
255     my @keys = $r->keys( '*glob_pattern*' );
256    
257     =cut
258    
259     sub keys {
260     my ( $self, $glob ) = @_;
261 dpavlin 21 return split(/\s/, $self->_sock_result_bulk( 'KEYS', $glob ));
262 dpavlin 12 }
263    
264 dpavlin 13 =head2 randomkey
265    
266     my $key = $r->randomkey;
267    
268     =cut
269    
270     sub randomkey {
271 dpavlin 14 my ( $self ) = @_;
272 dpavlin 21 $self->_sock_send( 'RANDOMKEY' );
273 dpavlin 13 }
274    
275 dpavlin 14 =head2 rename
276    
277 dpavlin 15 my $ok = $r->rename( 'old-key', 'new-key', $new );
278 dpavlin 14
279     =cut
280    
281     sub rename {
282     my ( $self, $old, $new, $nx ) = @_;
283 dpavlin 21 $self->_sock_send_ok( 'RENAME' . ( $nx ? 'NX' : '' ), $old, $new );
284 dpavlin 14 }
285    
286 dpavlin 17 =head2 dbsize
287    
288     my $nr_keys = $r->dbsize;
289    
290     =cut
291    
292     sub dbsize {
293     my ( $self ) = @_;
294 dpavlin 21 $self->_sock_send('DBSIZE');
295 dpavlin 17 }
296    
297 dpavlin 18 =head1 Commands operating on lists
298    
299 dpavlin 35 See also L<Redis::List> for tie interface.
300    
301 dpavlin 18 =head2 rpush
302    
303     $r->rpush( $key, $value );
304    
305     =cut
306    
307     sub rpush {
308     my ( $self, $key, $value ) = @_;
309     $self->_sock_send_bulk('RPUSH', $key, $value);
310     }
311    
312 dpavlin 19 =head2 lpush
313    
314     $r->lpush( $key, $value );
315    
316     =cut
317    
318     sub lpush {
319     my ( $self, $key, $value ) = @_;
320     $self->_sock_send_bulk('LPUSH', $key, $value);
321     }
322    
323 dpavlin 20 =head2 llen
324    
325     $r->llen( $key );
326    
327     =cut
328    
329     sub llen {
330     my ( $self, $key ) = @_;
331 dpavlin 21 $self->_sock_send( 'LLEN', $key );
332 dpavlin 20 }
333    
334 dpavlin 21 =head2 lrange
335    
336     my @list = $r->lrange( $key, $start, $end );
337    
338     =cut
339    
340     sub lrange {
341     my ( $self, $key, $start, $end ) = @_;
342 dpavlin 33 $self->_sock_result_bulk_list('LRANGE', $key, $start, $end);
343 dpavlin 21 }
344    
345 dpavlin 22 =head2 ltrim
346    
347     my $ok = $r->ltrim( $key, $start, $end );
348    
349     =cut
350    
351     sub ltrim {
352     my ( $self, $key, $start, $end ) = @_;
353     $self->_sock_send_ok( 'LTRIM', $key, $start, $end );
354     }
355    
356 dpavlin 23 =head2 lindex
357    
358     $r->lindex( $key, $index );
359    
360     =cut
361    
362     sub lindex {
363     my ( $self, $key, $index ) = @_;
364 dpavlin 24 $self->_sock_result_bulk( 'LINDEX', $key, $index );
365 dpavlin 23 }
366    
367 dpavlin 24 =head2 lset
368 dpavlin 23
369 dpavlin 24 $r->lset( $key, $index, $value );
370    
371     =cut
372    
373     sub lset {
374     my ( $self, $key, $index, $value ) = @_;
375     $self->_sock_send_bulk( 'LSET', $key, $index, $value );
376     }
377    
378 dpavlin 26 =head2 lrem
379    
380 dpavlin 27 my $modified_count = $r->lrem( $key, $count, $value );
381 dpavlin 26
382     =cut
383    
384     sub lrem {
385     my ( $self, $key, $count, $value ) = @_;
386     $self->_sock_send_bulk_number( 'LREM', $key, $count, $value );
387     }
388    
389 dpavlin 27 =head2 lpop
390    
391     my $value = $r->lpop( $key );
392    
393     =cut
394    
395     sub lpop {
396     my ( $self, $key ) = @_;
397 dpavlin 30 $self->_sock_result_bulk( 'LPOP', $key );
398 dpavlin 27 }
399    
400     =head2 rpop
401    
402     my $value = $r->rpop( $key );
403    
404     =cut
405    
406     sub rpop {
407     my ( $self, $key ) = @_;
408 dpavlin 30 $self->_sock_result_bulk( 'RPOP', $key );
409 dpavlin 27 }
410    
411 dpavlin 30 =head1 Commands operating on sets
412    
413     =head2 sadd
414    
415     $r->sadd( $key, $member );
416    
417     =cut
418    
419     sub sadd {
420     my ( $self, $key, $member ) = @_;
421     $self->_sock_send_bulk_number( 'SADD', $key, $member );
422     }
423    
424     =head2 srem
425    
426     $r->srem( $key, $member );
427    
428     =cut
429    
430     sub srem {
431     my ( $self, $key, $member ) = @_;
432     $self->_sock_send_bulk_number( 'SREM', $key, $member );
433     }
434    
435 dpavlin 31 =head2 scard
436    
437     my $elements = $r->scard( $key );
438    
439     =cut
440    
441     sub scard {
442     my ( $self, $key ) = @_;
443     $self->_sock_send( 'SCARD', $key );
444     }
445    
446 dpavlin 32 =head2 sismember
447    
448     $r->sismember( $key, $member );
449    
450     =cut
451    
452     sub sismember {
453     my ( $self, $key, $member ) = @_;
454     $self->_sock_send_bulk_number( 'SISMEMBER', $key, $member );
455     }
456    
457 dpavlin 33 =head2 sinter
458    
459     $r->sinter( $key1, $key2, ... );
460    
461     =cut
462    
463     sub sinter {
464     my $self = shift;
465     $self->_sock_result_bulk_list( 'SINTER', @_ );
466     }
467    
468 dpavlin 34 =head2 sinterstore
469    
470     my $ok = $r->sinterstore( $dstkey, $key1, $key2, ... );
471    
472     =cut
473    
474     sub sinterstore {
475     my $self = shift;
476     $self->_sock_send_ok( 'SINTERSTORE', @_ );
477     }
478    
479 dpavlin 38 =head1 Multiple databases handling commands
480    
481     =head2 select
482    
483 dpavlin 40 $r->select( $dbindex ); # 0 for new clients
484 dpavlin 38
485     =cut
486    
487     sub select {
488 dpavlin 40 my ($self,$dbindex) = @_;
489     confess dump($dbindex) . 'not number' unless $dbindex =~ m{^\d+$};
490     $self->_sock_send_ok( 'SELECT', $dbindex );
491 dpavlin 38 }
492    
493 dpavlin 40 =head2 move
494    
495     $r->move( $key, $dbindex );
496    
497     =cut
498    
499     sub move {
500     my ( $self, $key, $dbindex ) = @_;
501     $self->_sock_send( 'MOVE', $key, $dbindex );
502     }
503    
504 dpavlin 1 =head1 AUTHOR
505    
506     Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
507    
508     =head1 BUGS
509    
510     Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
511     the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
512     automatically be notified of progress on your bug as I make changes.
513    
514    
515    
516    
517     =head1 SUPPORT
518    
519     You can find documentation for this module with the perldoc command.
520    
521     perldoc Redis
522    
523    
524     You can also look for information at:
525    
526     =over 4
527    
528     =item * RT: CPAN's request tracker
529    
530     L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
531    
532     =item * AnnoCPAN: Annotated CPAN documentation
533    
534     L<http://annocpan.org/dist/Redis>
535    
536     =item * CPAN Ratings
537    
538     L<http://cpanratings.perl.org/d/Redis>
539    
540     =item * Search CPAN
541    
542     L<http://search.cpan.org/dist/Redis>
543    
544     =back
545    
546    
547     =head1 ACKNOWLEDGEMENTS
548    
549    
550     =head1 COPYRIGHT & LICENSE
551    
552     Copyright 2009 Dobrica Pavlinusic, all rights reserved.
553    
554     This program is free software; you can redistribute it and/or modify it
555     under the same terms as Perl itself.
556    
557    
558     =cut
559    
560     1; # End of Redis

  ViewVC Help
Powered by ViewVC 1.1.26