3026ee79c5ec8cc2bbd39dd8f974a7f5d112124d
[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
9 =head1 NAME
10
11 Redis - The great new Redis!
12
13 =cut
14
15 our $VERSION = '0.01';
16
17
18 =head1 SYNOPSIS
19
20 Pure perl bindings for L<http://code.google.com/p/redis/>
21
22     use Redis;
23
24     my $r = Redis->new();
25
26
27
28
29 =head1 FUNCTIONS
30
31 =head2 new
32
33 =cut
34
35 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 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
99
100 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
110
111 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
112
113 =head1 BUGS
114
115 Please report any bugs or feature requests to C<bug-redis at rt.cpan.org>, or through
116 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis>.  I will be notified, and then you'll
117 automatically be notified of progress on your bug as I make changes.
118
119
120
121
122 =head1 SUPPORT
123
124 You can find documentation for this module with the perldoc command.
125
126     perldoc Redis
127
128
129 You can also look for information at:
130
131 =over 4
132
133 =item * RT: CPAN's request tracker
134
135 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis>
136
137 =item * AnnoCPAN: Annotated CPAN documentation
138
139 L<http://annocpan.org/dist/Redis>
140
141 =item * CPAN Ratings
142
143 L<http://cpanratings.perl.org/d/Redis>
144
145 =item * Search CPAN
146
147 L<http://search.cpan.org/dist/Redis>
148
149 =back
150
151
152 =head1 ACKNOWLEDGEMENTS
153
154
155 =head1 COPYRIGHT & LICENSE
156
157 Copyright 2009 Dobrica Pavlinusic, all rights reserved.
158
159 This program is free software; you can redistribute it and/or modify it
160 under the same terms as Perl itself.
161
162
163 =cut
164
165 1; # End of Redis