keys, _sock_result_bulk
[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_result_bulk {
62         my $len = <$sock>;
63         warn "# len: ",dump($len);
64         return undef if $len eq "nil\r\n";
65         my $v;
66         read($sock, $v, $len) || die $!;
67         warn "# v: ",dump($v);
68         my $crlf;
69         read($sock, $crlf, 2); # skip cr/lf
70         return $v;
71 }
72
73 =head1 Connection Handling
74
75 =head2 quit
76
77   $r->quit;
78
79 =cut
80
81 sub quit {
82         my $self = shift;
83
84         close( $sock ) || warn $!;
85 }
86
87 =head2 ping
88
89   $r->ping || die "no server?";
90
91 =cut
92
93 sub ping {
94         print $sock "PING\r\n";
95         my $pong = <$sock>;
96         die "ping failed, got ", dump($pong) unless $pong eq "+PONG\r\n";
97 }
98
99 =head1 Commands operating on string values
100
101 =head2 set
102
103   $r->set( foo => 'bar', $new );
104
105 =cut
106
107 sub set {
108         my ( $self, $k, $v, $new ) = @_;
109         print $sock ( $new ? "SETNX" : "SET" ) . " $k " . length($v) . "\r\n$v\r\n";
110         my $ok = <$sock>;
111         confess dump($ok) unless $ok eq "+OK\r\n";
112 }
113
114 =head2 get
115
116   my $value = $r->get( 'foo' );
117
118 =cut
119
120 sub get {
121         my ( $self, $k ) = @_;
122         print $sock "GET $k\r\n";
123         _sock_result_bulk();
124 }
125
126 =head2 incr
127
128   $r->incr('counter');
129   $r->incr('tripplets', 3);
130
131 =cut
132
133         
134
135 sub incr {
136         my ( $self, $key, $value ) = @_;
137         if ( defined $value ) {
138                 print $sock "INCRBY $key $value\r\n";
139         } else {
140                 print $sock "INCR $key\r\n";
141         }
142         _sock_result();
143 }
144
145 =head2 decr
146
147   $r->decr('counter');
148   $r->decr('tripplets', 3);
149
150 =cut
151
152 sub decr {
153         my ( $self, $key, $value ) = @_;
154         if ( defined $value ) {
155                 print $sock "DECRBY $key $value\r\n";
156         } else {
157                 print $sock "DECR $key\r\n";
158         }
159         _sock_result();
160 }
161
162 =head2 exists
163
164   $r->exists( 'key' ) && print "got key!";
165
166 =cut
167
168 sub exists {
169         my ( $self, $key ) = @_;
170         print $sock "EXISTS $key\r\n";
171         _sock_result();
172 }
173
174 =head2 del
175
176   $r->del( 'key' ) || warn "key doesn't exist";
177
178 =cut
179
180 sub del {
181         my ( $self, $key ) = @_;
182         print $sock "DEL $key\r\n";
183         _sock_result();
184 }
185
186 =head2 type
187
188   $r->type( 'key' ); # = string
189
190 =cut
191
192 sub type {
193         my ( $self, $key ) = @_;
194         print $sock "TYPE $key\r\n";
195         _sock_result();
196 }
197
198 =head1 Commands operating on the key space
199
200 =head2 keys
201
202   my @keys = $r->keys( '*glob_pattern*' );
203
204 =cut
205
206 sub keys {
207         my ( $self, $glob ) = @_;
208         print $sock "KEYS $glob\r\n";
209         return split(/\s/, _sock_result_bulk());
210 }
211
212 =head1 AUTHOR
213
214 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
215
216 =head1 BUGS
217
218 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
219 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>.  I will be notified, and then you'll
220 automatically be notified of progress on your bug as I make changes.
221
222
223
224
225 =head1 SUPPORT
226
227 You can find documentation for this module with the perldoc command.
228
229     perldoc Redis
230
231
232 You can also look for information at:
233
234 =over 4
235
236 =item * RT: CPAN's request tracker
237
238 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
239
240 =item * AnnoCPAN: Annotated CPAN documentation
241
242 L<http://annocpan.org/dist/Redis>
243
244 =item * CPAN Ratings
245
246 L<http://cpanratings.perl.org/d/Redis>
247
248 =item * Search CPAN
249
250 L<http://search.cpan.org/dist/Redis>
251
252 =back
253
254
255 =head1 ACKNOWLEDGEMENTS
256
257
258 =head1 COPYRIGHT & LICENSE
259
260 Copyright 2009 Dobrica Pavlinusic, all rights reserved.
261
262 This program is free software; you can redistribute it and/or modify it
263 under the same terms as Perl itself.
264
265
266 =cut
267
268 1; # End of Redis