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

Diff of /lib/Redis.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1 by dpavlin, Sat Mar 21 20:20:45 2009 UTC revision 12 by dpavlin, Sat Mar 21 23:23:37 2009 UTC
# Line 3  package Redis; Line 3  package Redis;
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';
# Line 18  our $VERSION = '0.01'; Line 18  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    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    =head1 Connection Handling
74    
75    =head2 quit
76    
77      $r->quit;
78    
79  =cut  =cut
80    
81  sub function1 {  sub quit {
82            my $self = shift;
83    
84            close( $sock ) || warn $!;
85    }
86    
87    =head2 ping
88    
89      $r->ping || die "no server?";
90    
91    =cut
92    
93    sub ping {
94            print $sock "PING\r\n";
95            my $pong = <$sock>;
96            die "ping failed, got ", dump($pong) unless $pong eq "+PONG\r\n";
97    }
98    
99    =head1 Commands operating on string values
100    
101    =head2 set
102    
103      $r->set( foo => 'bar', $new );
104    
105    =cut
106    
107    sub set {
108            my ( $self, $k, $v, $new ) = @_;
109            print $sock ( $new ? "SETNX" : "SET" ) . " $k " . length($v) . "\r\n$v\r\n";
110            my $ok = <$sock>;
111            confess dump($ok) unless $ok eq "+OK\r\n";
112  }  }
113    
114  =head2 function2  =head2 get
115    
116      my $value = $r->get( 'foo' );
117    
118    =cut
119    
120    sub get {
121            my ( $self, $k ) = @_;
122            print $sock "GET $k\r\n";
123            _sock_result_bulk();
124    }
125    
126    =head2 incr
127    
128      $r->incr('counter');
129      $r->incr('tripplets', 3);
130    
131    =cut
132    
133            
134    
135    sub incr {
136            my ( $self, $key, $value ) = @_;
137            if ( defined $value ) {
138                    print $sock "INCRBY $key $value\r\n";
139            } else {
140                    print $sock "INCR $key\r\n";
141            }
142            _sock_result();
143    }
144    
145    =head2 decr
146    
147      $r->decr('counter');
148      $r->decr('tripplets', 3);
149    
150    =cut
151    
152    sub decr {
153            my ( $self, $key, $value ) = @_;
154            if ( defined $value ) {
155                    print $sock "DECRBY $key $value\r\n";
156            } else {
157                    print $sock "DECR $key\r\n";
158            }
159            _sock_result();
160    }
161    
162    =head2 exists
163    
164      $r->exists( 'key' ) && print "got key!";
165    
166    =cut
167    
168    sub exists {
169            my ( $self, $key ) = @_;
170            print $sock "EXISTS $key\r\n";
171            _sock_result();
172    }
173    
174    =head2 del
175    
176      $r->del( 'key' ) || warn "key doesn't exist";
177    
178    =cut
179    
180    sub del {
181            my ( $self, $key ) = @_;
182            print $sock "DEL $key\r\n";
183            _sock_result();
184    }
185    
186    =head2 type
187    
188      $r->type( 'key' ); # = string
189    
190    =cut
191    
192    sub type {
193            my ( $self, $key ) = @_;
194            print $sock "TYPE $key\r\n";
195            _sock_result();
196    }
197    
198    =head1 Commands operating on the key space
199    
200    =head2 keys
201    
202      my @keys = $r->keys( '*glob_pattern*' );
203    
204  =cut  =cut
205    
206  sub function2 {  sub keys {
207            my ( $self, $glob ) = @_;
208            print $sock "KEYS $glob\r\n";
209            return split(/\s/, _sock_result_bulk());
210  }  }
211    
212  =head1 AUTHOR  =head1 AUTHOR

Legend:
Removed from v.1  
changed lines
  Added in v.12

  ViewVC Help
Powered by ViewVC 1.1.26