/[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 14 by dpavlin, Sat Mar 21 23:36:26 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    sub _sock_ok {
74            my $ok = <$sock>;
75            confess dump($ok) unless $ok eq "+OK\r\n";
76    }
77    
78    =head1 Connection Handling
79    
80    =head2 quit
81    
82      $r->quit;
83    
84  =cut  =cut
85    
86  sub function1 {  sub quit {
87            my $self = shift;
88    
89            close( $sock ) || warn $!;
90  }  }
91    
92  =head2 function2  =head2 ping
93    
94      $r->ping || die "no server?";
95    
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    =head1 Commands operating on string values
105    
106    =head2 set
107    
108      $r->set( foo => 'bar', $new );
109    
110    =cut
111    
112    sub set {
113            my ( $self, $k, $v, $new ) = @_;
114            print $sock ( $new ? "SETNX" : "SET" ) . " $k " . length($v) . "\r\n$v\r\n";
115            _sock_ok();
116    }
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            _sock_result_bulk();
128    }
129    
130    =head2 incr
131    
132      $r->incr('counter');
133      $r->incr('tripplets', 3);
134    
135    =cut
136    
137            
138    
139    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            _sock_result();
147    }
148    
149    =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            _sock_result();
164    }
165    
166    =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            _sock_result();
176    }
177    
178    =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            _sock_result();
188    }
189    
190    =head2 type
191    
192      $r->type( 'key' ); # = string
193    
194    =cut
195    
196    sub type {
197            my ( $self, $key ) = @_;
198            print $sock "TYPE $key\r\n";
199            _sock_result();
200    }
201    
202    =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    =head2 randomkey
217    
218      my $key = $r->randomkey;
219    
220    =cut
221    
222    sub randomkey {
223            my ( $self ) = @_;
224            print $sock "RANDOMKEY\r\n";
225            _sock_result();
226    }
227    
228    =head2 rename
229    
230      my $ok = $r->rename( 'old-key', 'new-key', $only_if_new );
231    
232  =cut  =cut
233    
234  sub function2 {  sub rename {
235            my ( $self, $old, $new, $nx ) = @_;
236            print $sock "RENAME" . ( $nx ? 'NX' : '' ) . " $old $new\r\n";
237            _sock_ok();
238  }  }
239    
240  =head1 AUTHOR  =head1 AUTHOR

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

  ViewVC Help
Powered by ViewVC 1.1.26