File Coverage

blib/lib/Dancer/Plugin/MemcachedFast.pm
Criterion Covered Total %
statement 12 60 20.0
branch 0 62 0.0
condition 0 31 0.0
subroutine 4 21 19.0
pod n/a
total 16 174 9.2


line stmt bran cond sub pod time code
1             package Dancer::Plugin::MemcachedFast;
2             $Dancer::Plugin::MemcachedFast::VERSION = '0.161360';
3 3     3   399731 use strict;
  3         4  
  3         66  
4 3     3   9 use warnings;
  3         4  
  3         58  
5              
6             # ABSTRACT: Dancer::Plugin::MemcachedFast - Cache things using Cache::Memcached::Fast
7              
8 3     3   1146 use Dancer::Plugin;
  3         51046  
  3         170  
9              
10 3     3   1500 use Cache::Memcached::Fast;
  3         6247  
  3         2607  
11              
12             my $default_timeout = 3600;
13             my $ok_to_compress = 0;
14             my $cache = do {
15             my $config = plugin_setting;
16             $config->{servers} = [$ENV{HAVE_TEST_MEMCACHED_SERVER_LOCALHOST}]
17             if exists $ENV{HAVE_TEST_MEMCACHED_SERVER_LOCALHOST};
18             $default_timeout = delete $config->{default_timeout}
19             if exists $config->{default_timeout};
20             $ok_to_compress =
21             ( defined $config->{compress_threshold}
22             and int($config->{compress_threshold}) > 1
23             and defined $config->{compress_method});
24             Cache::Memcached::Fast->new($config);
25             };
26              
27             register memcached_compress => sub {
28 0 0   0     unless ($ok_to_compress) {
29 0           warn
30             "Cannot compress. Check the 'compress_threshold' and 'compress_method' configuration options";
31 0           return;
32             }
33 0           $cache->compress(!!$_[0]);
34             };
35              
36             register memcached_get_or_set => sub {
37 0 0 0 0     $cache->get($_[0]) or do {
38 0           my $ret;
39 0 0         $cache->set(
    0          
40             $_[0],
41             $ret = ref $_[1] eq 'CODE' ? $_[1]->() : $_[1],
42             defined $_[2] ? $_[2] : $default_timeout
43             );
44 0           $ret;
45             }
46             or $_[1];
47             };
48              
49             register memcached_get => sub {
50 0 0 0 0     return $cache->get($_[0]) if $#_ == 0 and !ref $_[0];
51 0 0         $cache->get_multi(map { ref $_ and ref $_ eq 'ARRAY' ? @$_ : $_ } @_);
  0 0          
52             };
53              
54             register memcached_gets => sub {
55 0 0 0 0     return $cache->gets($_[0]) if $#_ == 0 and !ref $_[0];
56 0 0         $cache->gets_multi(map { ref $_ and ref $_ eq 'ARRAY' ? @$_ : $_ } @_);
  0 0          
57             };
58              
59             register memcached_set => sub {
60 0 0   0     return $cache->set(
    0          
    0          
61             $_[0],
62             ref $_[1] eq 'CODE' ? $_[1]->() : $_[1],
63             defined $_[2] ? $_[2] : $default_timeout
64             ) if ref $_[0] ne 'ARRAY';
65             $cache->set_multi(
66             map { [
67 0 0         $_->[0],
    0          
68             ref $_->[1] eq 'CODE' ? $_->[1]->() : $_->[1],
69             defined $_->[2] ? $_->[2] : $default_timeout
70             ]
71 0           } grep { ref $_ eq 'ARRAY' } @_
  0            
72             );
73             };
74              
75             register memcached_add => sub {
76 0 0   0     return $cache->add(
    0          
    0          
77             $_[0],
78             ref $_[1] eq 'CODE' ? $_[1]->() : $_[1],
79             defined $_[2] ? $_[2] : $default_timeout
80             ) if ref $_[0] ne 'ARRAY';
81             $cache->add_multi(
82             map { [
83 0 0         $_->[0],
    0          
84             ref $_->[1] eq 'CODE' ? $_->[1]->() : $_->[1],
85             defined $_->[2] ? $_->[2] : $default_timeout
86             ]
87 0           } grep { ref $_ eq 'ARRAY' } @_
  0            
88             );
89             };
90              
91             register memcached_replace => sub {
92 0 0   0     return $cache->replace(
    0          
    0          
93             $_[0],
94             ref $_[1] eq 'CODE' ? $_[1]->() : $_[1],
95             defined $_[2] ? $_[2] : $default_timeout
96             ) if ref $_[0] ne 'ARRAY';
97             $cache->replace_multi(
98             map { [
99 0 0         $_->[0],
    0          
100             ref $_->[1] eq 'CODE' ? $_->[1]->() : $_->[1],
101             defined $_->[2] ? $_->[2] : $default_timeout
102             ]
103 0           } grep { ref $_ eq 'ARRAY' } @_
  0            
104             );
105             };
106              
107             register memcached_delete => sub {
108 0 0 0 0     return $cache->delete($_[0]) if $#_ == 0 and !ref $_[0];
109 0 0 0       $cache->delete_multi(map { (ref $_ and ref $_ eq 'ARRAY') ? @$_ : $_ } @_);
  0            
110             };
111              
112             register memcached_append => sub {
113 0 0 0 0     return $cache->append(@_) if ($#_ == 1 and !ref $_[0] and !ref $_[1]);
      0        
114 0           $cache->append_multi(grep { ref $_ eq 'ARRAY' } @_);
  0            
115             };
116              
117             register memcached_prepend => sub {
118 0 0 0 0     return $cache->prepend(@_) if ($#_ == 1 and !ref $_[0] and !ref $_[1]);
      0        
119 0           $cache->prepend_multi(grep { ref $_ eq 'ARRAY' } @_);
  0            
120             };
121              
122             register memcached_incr => sub {
123 0 0 0 0     return $cache->incr(@_) if ($#_ <= 2 and scalar grep { !ref $_ } @_);
  0            
124 0           $cache->incr_multi(grep { ref $_ eq 'ARRAY' } @_);
  0            
125             };
126              
127             register memcached_decr => sub {
128 0 0 0 0     return $cache->decr(@_) if ($#_ <= 2 and scalar grep { !ref $_ } @_);
  0            
129 0           $cache->decr_multi(grep { ref $_ eq 'ARRAY' } @_);
  0            
130             };
131              
132             register memcached_flush_all => sub {
133 0     0     $cache->flush_all(@_);
134             };
135              
136             register memcached_nowait_push => sub {
137 0     0     $cache->nowait_push;
138             };
139              
140             register memcached_server_versions => sub {
141 0     0     $cache->server_versions;
142             };
143              
144             register memcached_disconnect_all => sub {
145 0     0     $cache->disconnect_all;
146             };
147              
148             register memcached_namespace => sub {
149 0     0     $cache->namespace($_[0]);
150             };
151              
152             register_plugin;
153              
154             1;
155              
156             __END__