3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
|
|
6 |
|
use IO::Socket::INET; |
7 |
|
use Data::Dump qw/dump/; |
8 |
|
|
9 |
=head1 NAME |
=head1 NAME |
10 |
|
|
11 |
Redis - The great new Redis! |
Redis - The great new Redis! |
12 |
|
|
|
=head1 VERSION |
|
|
|
|
|
Version 0.01 |
|
|
|
|
13 |
=cut |
=cut |
14 |
|
|
15 |
our $VERSION = '0.01'; |
our $VERSION = '0.01'; |
17 |
|
|
18 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
19 |
|
|
20 |
Quick summary of what the module does. |
Pure perl bindings for L<http://code.google.com/p/redis/> |
|
|
|
|
Perhaps a little code snippet. |
|
21 |
|
|
22 |
use Redis; |
use Redis; |
23 |
|
|
24 |
my $foo = Redis->new(); |
my $r = Redis->new(); |
25 |
... |
|
26 |
|
|
|
=head1 EXPORT |
|
27 |
|
|
|
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. |
|
28 |
|
|
29 |
=head1 FUNCTIONS |
=head1 FUNCTIONS |
30 |
|
|
31 |
=head2 function1 |
=head2 new |
32 |
|
|
33 |
=cut |
=cut |
34 |
|
|
35 |
sub function1 { |
our $sock; |
36 |
|
my $server = '127.0.0.1:6379'; |
37 |
|
|
38 |
|
sub new { |
39 |
|
my $class = shift; |
40 |
|
my $self = {}; |
41 |
|
bless($self, $class); |
42 |
|
|
43 |
|
warn "# opening socket to $server"; |
44 |
|
|
45 |
|
$sock ||= IO::Socket::INET->new( |
46 |
|
PeerAddr => $server, |
47 |
|
Proto => 'tcp', |
48 |
|
) || die $!; |
49 |
|
|
50 |
|
$self; |
51 |
|
} |
52 |
|
|
53 |
|
=head1 Connection Handling |
54 |
|
|
55 |
|
=head2 quit |
56 |
|
|
57 |
|
$r->quit; |
58 |
|
|
59 |
|
=cut |
60 |
|
|
61 |
|
sub quit { |
62 |
|
my $self = shift; |
63 |
|
|
64 |
|
close( $sock ) || warn $!; |
65 |
} |
} |
66 |
|
|
67 |
=head2 function2 |
=head2 ping |
68 |
|
|
69 |
|
$r->ping || die "no server?"; |
70 |
|
|
71 |
|
=cut |
72 |
|
|
73 |
|
sub ping { |
74 |
|
print $sock "PING\r\n"; |
75 |
|
my $pong = <$sock>; |
76 |
|
die "ping failed, got ", dump($pong) unless $pong eq "+PONG\r\n"; |
77 |
|
} |
78 |
|
|
79 |
|
=head1 Commands operating on string values |
80 |
|
|
81 |
|
=head2 set |
82 |
|
|
83 |
|
$r->set( foo => 'bar' ); |
84 |
|
|
85 |
|
=cut |
86 |
|
|
87 |
|
sub set { |
88 |
|
my ( $self, $k, $v ) = @_; |
89 |
|
print $sock "SET $k " . length($v) . "\r\n$v\r\n"; |
90 |
|
my $ok = <$sock>; |
91 |
|
die dump($ok) unless $ok eq "+OK\r\n"; |
92 |
|
} |
93 |
|
|
94 |
|
=head2 get |
95 |
|
|
96 |
|
my $value = $r->get( 'foo' ); |
97 |
|
|
98 |
=cut |
=cut |
99 |
|
|
100 |
sub function2 { |
sub get { |
101 |
|
my ( $self, $k ) = @_; |
102 |
|
print $sock "GET $k\r\n"; |
103 |
|
my $len = <$sock>; |
104 |
|
my $v; |
105 |
|
read($sock, $v, $len) || die $!; |
106 |
|
return $v; |
107 |
} |
} |
108 |
|
|
109 |
=head1 AUTHOR |
=head1 AUTHOR |