File Coverage

blib/lib/KiokuDB/Backend/Redis.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package KiokuDB::Backend::Redis;
2 2     2   137509 use Moose;
  0            
  0            
3              
4             use Carp qw(croak);
5             use Redis;
6              
7             our $VERSION = '0.02';
8              
9             has '_redis' => (
10             is => 'rw',
11             isa => 'Redis'
12             );
13              
14             with qw(
15             KiokuDB::Backend
16             KiokuDB::Backend::Serialize::Delegate
17             );
18              
19             sub new_from_dsn_params {
20             my ( $self, %args ) = @_;
21              
22             $args{debug} = 1;
23              
24             $self->new(_redis => Redis->new(%args));
25             }
26              
27             sub delete {
28             my ($self, @ids_or_entries) = @_;
29              
30             my $redis = $self->_redis;
31              
32             my @uids = map { ref($_) ? $_->id : $_ } @ids_or_entries;
33              
34             foreach my $id ( @uids ) {
35              
36             $redis->del($id);
37             # TODO Error checking
38             }
39              
40             return;
41             }
42              
43             sub exists {
44             my ($self, @ids) = @_;
45              
46             my @exists;
47              
48             my $redis = $self->_redis;
49             foreach my $id (@ids) {
50              
51             if($redis->exists($id)) {
52             push(@exists, 1);
53             } else {
54             push(@exists, 0);
55             }
56             }
57             # TODO Error checking
58             return @exists;
59             }
60              
61             sub insert {
62             my ($self, @entries) = @_;
63              
64             my $redis = $self->_redis;
65              
66             my @exists = $self->exists(@entries);
67              
68             foreach my $entry ( @entries ) {
69              
70             if($entry->has_prev) {
71             my $ret = $redis->set(
72             $entry->id => $self->serialize($entry),
73             );
74             # TODO Error checking
75             } else {
76             my $ret = $redis->setnx(
77             $entry->id => $self->serialize($entry),
78             );
79             # TODO Error checking
80             }
81             }
82             }
83              
84             sub get {
85             my ($self, @ids) = @_;
86              
87             my ( $var, @ret );
88              
89             my $redis = $self->_redis;
90              
91             foreach my $id ( @ids ) {
92             my $val = $redis->get($id);
93             # TODO Error checking
94             if(defined($val)) {
95             push @ret, $val;
96             } else {
97             return;
98             }
99             }
100              
101             return map { $self->deserialize($_) } @ret;
102             }
103              
104             1;
105              
106             __END__
107              
108             =head1 NAME
109              
110             KiokuDB::Backend::Redis - Redis backend for KiokuDB
111              
112             =head1 SYNOPSIS
113              
114              
115             use KiokuDB::Backend::Redis;
116              
117             my $kiokudb = KiokuDB->connect('Redis:server=127.0.0.1;debug=1);
118             ...
119              
120             =head1 DESCRPTION
121              
122             This is a KiokuDB backend for Redis, a self proclaimed data structures server.
123             It is rather embryonic, but passes the tests. I expect to expand it as I
124             explore Redis and KiokuDB.
125              
126             =head1 SEE ALSO
127              
128             L<http://code.google.com/p/redis/>
129              
130             =head1 AUTHOR
131              
132             Cory G Watson, C<< <gphat at cpan.org> >>
133              
134             =head1 COPYRIGHT & LICENSE
135              
136             Copyright 2009 Cory G Watson.
137              
138             This program is free software; you can redistribute it and/or modify it
139             under the terms of either: the GNU General Public License as published
140             by the Free Software Foundation; or the Artistic License.
141              
142             See http://dev.perl.org/licenses/ for more information.
143              
144             =cut