File Coverage

blib/lib/Katsubushi/Client.pm
Criterion Covered Total %
statement 17 57 29.8
branch 0 12 0.0
condition n/a
subroutine 6 10 60.0
pod 0 4 0.0
total 23 83 27.7


line stmt bran cond sub pod time code
1             package Katsubushi::Client;
2              
3 4     4   307457 use 5.008001;
  4         19  
4 4     4   16 use strict;
  4         5  
  4         57  
5 4     4   13 use warnings;
  4         5  
  4         129  
6              
7             our $VERSION = "0.2";
8              
9 4     4   1684 use Cache::Memcached::Fast;
  4         7220  
  4         99  
10              
11 4     4   20 use Carp qw(croak);
  4         6  
  4         352  
12             use Class::Tiny +{
13 0           servers => sub { ["127.0.0.1:11212"] },
14             _models => sub {
15 0           my $self = shift;
16             return [ map {
17 0           Cache::Memcached::Fast->new({ servers => [ $_ ] });
18 0           } @{$self->servers} ];
  0            
19             },
20 0           pid => sub { $$ },
21 4     4   1425 };
  4         5055  
  4         27  
22              
23             sub BUILD {
24 0     0 0   my $self = shift;
25              
26 0 0         if (scalar @{$self->servers} < 1) {
  0            
27 0           die 'there are no servers.';
28             }
29             }
30              
31             sub fetch {
32 0     0 0   my $self = shift;
33              
34 0           $self->ensure_fork_safe;
35 0           my $id = 0;
36              
37             # retry at once for only main (the first of list) server
38 0           for my $model ($self->_models->[0], @{$self->_models}) {
  0            
39 0           $id = $model->get('id');
40 0 0         last if $id;
41             }
42              
43 0 0         unless ($id) {
44 0           croak 'Failed to fetch new id';
45             }
46              
47 0           return $id;
48             }
49              
50             sub fetch_multi {
51 0     0 0   my $self = shift;
52 0           my $n = shift;
53              
54 0           $self->ensure_fork_safe;
55              
56 0           my @keys = ( 1 .. $n );
57 0           my $ids;
58             # retry at once for only main (the first of list) server
59 0           for my $model ($self->_models->[0], @{$self->_models}) {
  0            
60 0           $ids = $model->get_multi(@keys);
61 0 0         last if scalar(values %$ids) == $n;
62             }
63              
64 0 0         if (scalar values %$ids != $n) {
65 0           croak "Failed to fetch new $n ids";
66             }
67              
68 0           return map { $ids->{$_} } @keys;
  0            
69             }
70              
71             sub ensure_fork_safe {
72 0     0 0   my $self = shift;
73              
74 0 0         if ($self->pid != $$) {
75             # detect forked
76 0           $self->pid($$);
77 0           for my $memd (@{$self->_models}) {
  0            
78 0           $memd->disconnect_all;
79             }
80             }
81 0           1;
82             }
83              
84             1;
85             __END__