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

Annotation of /lib/Redis.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 17 - (hide annotations)
Sun Mar 22 09:22:39 2009 UTC (15 years ago) by dpavlin
File size: 4424 byte(s)
dbsize
1 dpavlin 1 package Redis;
2    
3     use warnings;
4     use strict;
5    
6 dpavlin 2 use IO::Socket::INET;
7     use Data::Dump qw/dump/;
8 dpavlin 5 use Carp qw/confess/;
9 dpavlin 2
10 dpavlin 1 =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 dpavlin 2 Pure perl bindings for L<http://code.google.com/p/redis/>
22 dpavlin 1
23     use Redis;
24    
25 dpavlin 2 my $r = Redis->new();
26 dpavlin 1
27    
28    
29 dpavlin 2
30 dpavlin 1 =head1 FUNCTIONS
31    
32 dpavlin 2 =head2 new
33 dpavlin 1
34     =cut
35    
36 dpavlin 2 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 dpavlin 1 }
53    
54 dpavlin 11 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 dpavlin 12 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 dpavlin 14 sub _sock_ok {
74     my $ok = <$sock>;
75     confess dump($ok) unless $ok eq "+OK\r\n";
76     }
77    
78 dpavlin 2 =head1 Connection Handling
79 dpavlin 1
80 dpavlin 2 =head2 quit
81    
82     $r->quit;
83    
84 dpavlin 1 =cut
85    
86 dpavlin 2 sub quit {
87     my $self = shift;
88    
89     close( $sock ) || warn $!;
90 dpavlin 1 }
91    
92 dpavlin 2 =head2 ping
93    
94 dpavlin 3 $r->ping || die "no server?";
95 dpavlin 2
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 dpavlin 3 =head1 Commands operating on string values
105    
106     =head2 set
107    
108 dpavlin 5 $r->set( foo => 'bar', $new );
109 dpavlin 3
110     =cut
111    
112     sub set {
113 dpavlin 5 my ( $self, $k, $v, $new ) = @_;
114 dpavlin 15 print $sock "SET" . ( $new ? 'NX' : '' ) . " $k " . length($v) . "\r\n$v\r\n";
115 dpavlin 14 _sock_ok();
116 dpavlin 3 }
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 dpavlin 12 _sock_result_bulk();
128 dpavlin 3 }
129    
130 dpavlin 7 =head2 incr
131 dpavlin 4
132 dpavlin 7 $r->incr('counter');
133     $r->incr('tripplets', 3);
134 dpavlin 4
135 dpavlin 7 =cut
136    
137 dpavlin 10
138    
139 dpavlin 7 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 dpavlin 11 _sock_result();
147 dpavlin 7 }
148    
149 dpavlin 8 =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 dpavlin 11 _sock_result();
164 dpavlin 8 }
165    
166 dpavlin 9 =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 dpavlin 11 _sock_result();
176 dpavlin 9 }
177    
178 dpavlin 10 =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 dpavlin 11 _sock_result();
188 dpavlin 10 }
189    
190 dpavlin 11 =head2 type
191    
192     $r->type( 'key' ); # = string
193    
194     =cut
195    
196     sub type {
197     my ( $self, $key ) = @_;
198 dpavlin 12 print $sock "TYPE $key\r\n";
199 dpavlin 11 _sock_result();
200     }
201    
202 dpavlin 12 =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 dpavlin 13 =head2 randomkey
217    
218     my $key = $r->randomkey;
219    
220     =cut
221    
222     sub randomkey {
223 dpavlin 14 my ( $self ) = @_;
224 dpavlin 13 print $sock "RANDOMKEY\r\n";
225     _sock_result();
226     }
227    
228 dpavlin 14 =head2 rename
229    
230 dpavlin 15 my $ok = $r->rename( 'old-key', 'new-key', $new );
231 dpavlin 14
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 dpavlin 17 =head2 dbsize
241    
242     my $nr_keys = $r->dbsize;
243    
244     =cut
245    
246     sub dbsize {
247     my ( $self ) = @_;
248     print $sock "DBSIZE\r\n";
249     _sock_result();
250     }
251    
252 dpavlin 1 =head1 AUTHOR
253    
254     Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
255    
256     =head1 BUGS
257    
258     Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
259     the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>. I will be notified, and then you'll
260     automatically be notified of progress on your bug as I make changes.
261    
262    
263    
264    
265     =head1 SUPPORT
266    
267     You can find documentation for this module with the perldoc command.
268    
269     perldoc Redis
270    
271    
272     You can also look for information at:
273    
274     =over 4
275    
276     =item * RT: CPAN's request tracker
277    
278     L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
279    
280     =item * AnnoCPAN: Annotated CPAN documentation
281    
282     L<http://annocpan.org/dist/Redis>
283    
284     =item * CPAN Ratings
285    
286     L<http://cpanratings.perl.org/d/Redis>
287    
288     =item * Search CPAN
289    
290     L<http://search.cpan.org/dist/Redis>
291    
292     =back
293    
294    
295     =head1 ACKNOWLEDGEMENTS
296    
297    
298     =head1 COPYRIGHT & LICENSE
299    
300     Copyright 2009 Dobrica Pavlinusic, all rights reserved.
301    
302     This program is free software; you can redistribute it and/or modify it
303     under the same terms as Perl itself.
304    
305    
306     =cut
307    
308     1; # End of Redis

  ViewVC Help
Powered by ViewVC 1.1.26