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

  ViewVC Help
Powered by ViewVC 1.1.26