3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
|
|
6 |
|
use IO::Socket::INET; |
7 |
|
use Data::Dump qw/dump/; |
8 |
|
use Carp qw/confess/; |
9 |
|
|
10 |
=head1 NAME |
=head1 NAME |
11 |
|
|
12 |
Redis - The great new Redis! |
Redis - The great new Redis! |
13 |
|
|
|
=head1 VERSION |
|
|
|
|
|
Version 0.01 |
|
|
|
|
14 |
=cut |
=cut |
15 |
|
|
16 |
our $VERSION = '0.01'; |
our $VERSION = '0.01'; |
18 |
|
|
19 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
20 |
|
|
21 |
Quick summary of what the module does. |
Pure perl bindings for L<http://code.google.com/p/redis/> |
|
|
|
|
Perhaps a little code snippet. |
|
22 |
|
|
23 |
use Redis; |
use Redis; |
24 |
|
|
25 |
my $foo = Redis->new(); |
my $r = Redis->new(); |
26 |
... |
|
27 |
|
|
|
=head1 EXPORT |
|
28 |
|
|
|
A list of functions that can be exported. You can delete this section |
|
|
if you don't export anything, such as for a purely object-oriented module. |
|
29 |
|
|
30 |
=head1 FUNCTIONS |
=head1 FUNCTIONS |
31 |
|
|
32 |
=head2 function1 |
=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 |
|
=head1 Connection Handling |
55 |
|
|
56 |
|
=head2 quit |
57 |
|
|
58 |
|
$r->quit; |
59 |
|
|
60 |
|
=cut |
61 |
|
|
62 |
|
sub quit { |
63 |
|
my $self = shift; |
64 |
|
|
65 |
|
close( $sock ) || warn $!; |
66 |
|
} |
67 |
|
|
68 |
|
=head2 ping |
69 |
|
|
70 |
|
$r->ping || die "no server?"; |
71 |
|
|
72 |
=cut |
=cut |
73 |
|
|
74 |
sub function1 { |
sub ping { |
75 |
|
print $sock "PING\r\n"; |
76 |
|
my $pong = <$sock>; |
77 |
|
die "ping failed, got ", dump($pong) unless $pong eq "+PONG\r\n"; |
78 |
} |
} |
79 |
|
|
80 |
=head2 function2 |
=head1 Commands operating on string values |
81 |
|
|
82 |
|
=head2 set |
83 |
|
|
84 |
|
$r->set( foo => 'bar', $new ); |
85 |
|
|
86 |
|
=cut |
87 |
|
|
88 |
|
sub set { |
89 |
|
my ( $self, $k, $v, $new ) = @_; |
90 |
|
print $sock ( $new ? "SETNX" : "SET" ) . " $k " . length($v) . "\r\n$v\r\n"; |
91 |
|
my $ok = <$sock>; |
92 |
|
confess dump($ok) unless $ok eq "+OK\r\n"; |
93 |
|
} |
94 |
|
|
95 |
|
=head2 get |
96 |
|
|
97 |
|
my $value = $r->get( 'foo' ); |
98 |
|
|
99 |
|
=cut |
100 |
|
|
101 |
|
sub get { |
102 |
|
my ( $self, $k ) = @_; |
103 |
|
print $sock "GET $k\r\n"; |
104 |
|
my $len = <$sock>; |
105 |
|
# warn "# len: ",dump($len); |
106 |
|
return undef if $len eq "nil\r\n"; |
107 |
|
my $v; |
108 |
|
read($sock, $v, $len) || die $!; |
109 |
|
# warn "# v: ",dump($v); |
110 |
|
my $crlf; |
111 |
|
read($sock, $crlf, 2); # skip cr/lf |
112 |
|
return $v; |
113 |
|
} |
114 |
|
|
115 |
|
=head2 incr |
116 |
|
|
117 |
|
$r->incr('counter'); |
118 |
|
$r->incr('tripplets', 3); |
119 |
|
|
120 |
|
=cut |
121 |
|
|
122 |
|
sub incr { |
123 |
|
my ( $self, $key, $value ) = @_; |
124 |
|
if ( defined $value ) { |
125 |
|
print $sock "INCRBY $key $value\r\n"; |
126 |
|
} else { |
127 |
|
print $sock "INCR $key\r\n"; |
128 |
|
} |
129 |
|
my $count = <$sock>; |
130 |
|
warn "# $key = $count"; |
131 |
|
return $count; |
132 |
|
} |
133 |
|
|
134 |
|
=head2 decr |
135 |
|
|
136 |
|
$r->decr('counter'); |
137 |
|
$r->decr('tripplets', 3); |
138 |
|
|
139 |
|
=cut |
140 |
|
|
141 |
|
sub decr { |
142 |
|
my ( $self, $key, $value ) = @_; |
143 |
|
if ( defined $value ) { |
144 |
|
print $sock "DECRBY $key $value\r\n"; |
145 |
|
} else { |
146 |
|
print $sock "DECR $key\r\n"; |
147 |
|
} |
148 |
|
my $count = <$sock>; |
149 |
|
warn "# $key = $count"; |
150 |
|
return $count; |
151 |
|
} |
152 |
|
|
153 |
|
=head2 exists |
154 |
|
|
155 |
|
$r->exists( 'key' ) && print "got key!"; |
156 |
|
|
157 |
=cut |
=cut |
158 |
|
|
159 |
sub function2 { |
sub exists { |
160 |
|
my ( $self, $key ) = @_; |
161 |
|
print $sock "EXISTS $key\r\n"; |
162 |
|
my $found = <$sock>; |
163 |
|
$found =~ s{\r\n$}{}; |
164 |
|
warn "# exists $key = $found"; |
165 |
|
return $found; |
166 |
} |
} |
167 |
|
|
168 |
=head1 AUTHOR |
=head1 AUTHOR |