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