/[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 27 - (show annotations)
Sun Mar 22 13:44:19 2009 UTC (10 years, 3 months ago) by dpavlin
File size: 6750 byte(s)
lpop, rpop
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 read($sock, $v, $len) || die $!;
67 warn "## bulk v: ",dump($v);
68 my $crlf;
69 read($sock, $crlf, 2); # skip cr/lf
70 return $v;
71 }
72
73 sub _sock_result_bulk {
74 my $self = shift;
75 warn "## _sock_result_bulk ",dump( @_ );
76 print $sock join(' ',@_) . "\r\n";
77 _sock_read_bulk();
78 }
79
80 sub __sock_ok {
81 my $ok = <$sock>;
82 confess dump($ok) unless $ok eq "+OK\r\n";
83 }
84
85 sub _sock_send {
86 my $self = shift;
87 warn "## _sock_send ",dump( @_ );
88 print $sock join(' ',@_) . "\r\n";
89 _sock_result();
90 }
91
92 sub _sock_send_ok {
93 my $self = shift;
94 warn "## _sock_send_ok ",dump( @_ );
95 print $sock join(' ',@_) . "\r\n";
96 __sock_ok();
97 }
98
99 sub __sock_send_bulk_raw {
100 my $self = shift;
101 warn "## _sock_send_bulk ",dump( @_ );
102 my $value = pop;
103 print $sock join(' ',@_) . ' ' . length($value) . "\r\n$value\r\n";
104 }
105
106 sub _sock_send_bulk {
107 __sock_send_bulk_raw( @_ );
108 __sock_ok();
109 }
110
111 sub _sock_send_bulk_number {
112 __sock_send_bulk_raw( @_ );
113 my $v = _sock_result();
114 confess $v unless $v =~ m{^\-?\d+$};
115 return $v;
116 }
117
118 =head1 Connection Handling
119
120 =head2 quit
121
122 $r->quit;
123
124 =cut
125
126 sub quit {
127 my $self = shift;
128
129 close( $sock ) || warn $!;
130 }
131
132 =head2 ping
133
134 $r->ping || die "no server?";
135
136 =cut
137
138 sub ping {
139 print $sock "PING\r\n";
140 my $pong = <$sock>;
141 die "ping failed, got ", dump($pong) unless $pong eq "+PONG\r\n";
142 }
143
144 =head1 Commands operating on string values
145
146 =head2 set
147
148 $r->set( foo => 'bar', $new );
149
150 =cut
151
152 sub set {
153 my ( $self, $key, $value, $new ) = @_;
154 $self->_sock_send_bulk( "SET" . ( $new ? 'NX' : '' ), $key, $value );
155 }
156
157 =head2 get
158
159 my $value = $r->get( 'foo' );
160
161 =cut
162
163 sub get {
164 my $self = shift;
165 $self->_sock_result_bulk('GET', @_);
166 }
167
168 =head2 incr
169
170 $r->incr('counter');
171 $r->incr('tripplets', 3);
172
173 =cut
174
175
176
177 sub incr {
178 my $self = shift;
179 $self->_sock_send( 'INCR' . ( $#_ ? 'BY' : '' ), @_ );
180 }
181
182 =head2 decr
183
184 $r->decr('counter');
185 $r->decr('tripplets', 3);
186
187 =cut
188
189 sub decr {
190 my $self = shift;
191 $self->_sock_send( 'DECR' . ( $#_ ? 'BY' : '' ), @_ );
192 }
193
194 =head2 exists
195
196 $r->exists( 'key' ) && print "got key!";
197
198 =cut
199
200 sub exists {
201 my ( $self, $key ) = @_;
202 $self->_sock_send( 'EXISTS', $key );
203 }
204
205 =head2 del
206
207 $r->del( 'key' ) || warn "key doesn't exist";
208
209 =cut
210
211 sub del {
212 my ( $self, $key ) = @_;
213 $self->_sock_send( 'DEL', $key );
214 }
215
216 =head2 type
217
218 $r->type( 'key' ); # = string
219
220 =cut
221
222 sub type {
223 my ( $self, $key ) = @_;
224 $self->_sock_send( 'TYPE', $key );
225 }
226
227 =head1 Commands operating on the key space
228
229 =head2 keys
230
231 my @keys = $r->keys( '*glob_pattern*' );
232
233 =cut
234
235 sub keys {
236 my ( $self, $glob ) = @_;
237 return split(/\s/, $self->_sock_result_bulk( 'KEYS', $glob ));
238 }
239
240 =head2 randomkey
241
242 my $key = $r->randomkey;
243
244 =cut
245
246 sub randomkey {
247 my ( $self ) = @_;
248 $self->_sock_send( 'RANDOMKEY' );
249 }
250
251 =head2 rename
252
253 my $ok = $r->rename( 'old-key', 'new-key', $new );
254
255 =cut
256
257 sub rename {
258 my ( $self, $old, $new, $nx ) = @_;
259 $self->_sock_send_ok( 'RENAME' . ( $nx ? 'NX' : '' ), $old, $new );
260 }
261
262 =head2 dbsize
263
264 my $nr_keys = $r->dbsize;
265
266 =cut
267
268 sub dbsize {
269 my ( $self ) = @_;
270 $self->_sock_send('DBSIZE');
271 }
272
273 =head1 Commands operating on lists
274
275 =head2 rpush
276
277 $r->rpush( $key, $value );
278
279 =cut
280
281 sub rpush {
282 my ( $self, $key, $value ) = @_;
283 $self->_sock_send_bulk('RPUSH', $key, $value);
284 }
285
286 =head2 lpush
287
288 $r->lpush( $key, $value );
289
290 =cut
291
292 sub lpush {
293 my ( $self, $key, $value ) = @_;
294 $self->_sock_send_bulk('LPUSH', $key, $value);
295 }
296
297 =head2 llen
298
299 $r->llen( $key );
300
301 =cut
302
303 sub llen {
304 my ( $self, $key ) = @_;
305 $self->_sock_send( 'LLEN', $key );
306 }
307
308 =head2 lrange
309
310 my @list = $r->lrange( $key, $start, $end );
311
312 =cut
313
314 sub lrange {
315 my ( $self, $key, $start, $end ) = @_;
316 my $size = $self->_sock_send('LRANGE', $key, $start, $end);
317
318 confess $size unless $size > 0;
319 $size--;
320
321 my @list = ( 0 .. $size );
322 foreach ( 0 .. $size ) {
323 $list[ $_ ] = _sock_read_bulk();
324 }
325
326 warn "## lrange $key $start $end = [$size] ", dump( @list );
327 return @list;
328 }
329
330 =head2 ltrim
331
332 my $ok = $r->ltrim( $key, $start, $end );
333
334 =cut
335
336 sub ltrim {
337 my ( $self, $key, $start, $end ) = @_;
338 $self->_sock_send_ok( 'LTRIM', $key, $start, $end );
339 }
340
341 =head2 lindex
342
343 $r->lindex( $key, $index );
344
345 =cut
346
347 sub lindex {
348 my ( $self, $key, $index ) = @_;
349 $self->_sock_result_bulk( 'LINDEX', $key, $index );
350 }
351
352 =head2 lset
353
354 $r->lset( $key, $index, $value );
355
356 =cut
357
358 sub lset {
359 my ( $self, $key, $index, $value ) = @_;
360 $self->_sock_send_bulk( 'LSET', $key, $index, $value );
361 }
362
363 =head2 lrem
364
365 my $modified_count = $r->lrem( $key, $count, $value );
366
367 =cut
368
369 sub lrem {
370 my ( $self, $key, $count, $value ) = @_;
371 $self->_sock_send_bulk_number( 'LREM', $key, $count, $value );
372 }
373
374 =head2 lpop
375
376 my $value = $r->lpop( $key );
377
378 =cut
379
380 sub lpop {
381 my ( $self, $key ) = @_;
382 $self->_sock_result_bulk( 'lpop', $key );
383 }
384
385 =head2 rpop
386
387 my $value = $r->rpop( $key );
388
389 =cut
390
391 sub rpop {
392 my ( $self, $key ) = @_;
393 $self->_sock_result_bulk( 'rpop', $key );
394 }
395
396 =head1 AUTHOR
397
398 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
399
400 =head1 BUGS
401
402 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
403 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
404 automatically be notified of progress on your bug as I make changes.
405
406
407
408
409 =head1 SUPPORT
410
411 You can find documentation for this module with the perldoc command.
412
413 perldoc Redis
414
415
416 You can also look for information at:
417
418 =over 4
419
420 =item * RT: CPAN's request tracker
421
422 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
423
424 =item * AnnoCPAN: Annotated CPAN documentation
425
426 L<http://annocpan.org/dist/Redis>
427
428 =item * CPAN Ratings
429
430 L<http://cpanratings.perl.org/d/Redis>
431
432 =item * Search CPAN
433
434 L<http://search.cpan.org/dist/Redis>
435
436 =back
437
438
439 =head1 ACKNOWLEDGEMENTS
440
441
442 =head1 COPYRIGHT & LICENSE
443
444 Copyright 2009 Dobrica Pavlinusic, all rights reserved.
445
446 This program is free software; you can redistribute it and/or modify it
447 under the same terms as Perl itself.
448
449
450 =cut
451
452 1; # End of Redis

  ViewVC Help
Powered by ViewVC 1.1.26