/[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

Contents of /lib/Redis.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26