blib/lib/PEF/Front/Cache.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 102 SCAR 40.3
CC
7
Cov
0%
make_request_cache_key
20.0
get_cache
12.0
BEGIN
6.0
CRAP 56.0
1 package PEF::Front::Cache;
2 use strict;
3 use warnings;
4 use Cache::FastMmap;
5 use PEF::Front::Config;
6 use Time::Duration::Parse;
7 use Time::HiRes 'time';
8 use Data::Dumper;
9  
10 use base 'Exporter';
11 our @EXPORT = qw{
12 get_cache
13 make_request_cache_key
14 remove_cache_key
15 set_cache
16 };
17  
18 my $cache;
19 my $dumper;
20  
21 BEGIN {
22 # empty grep means there's no config loaded - some statical
23 # analyzing tools can break
24 if (grep { /AppFrontConfig\.pm$/ } keys %INC) {
25 $cache = Cache::FastMmap->new(
26 share_file => cfg_cache_file,
27 cache_size => cfg_cache_size,
28 empty_on_exit => 0,
29 unlink_on_exit => 0,
30 expire_time => 0,
31 init_file => 1
32 ) or die "Can't create cache: $!";
33 }
34 $dumper = Data::Dumper->new([]);
35 $dumper->Indent(0);
36 $dumper->Pair(":");
37 $dumper->Useqq(1);
38 $dumper->Terse(1);
39 $dumper->Deepcopy(1);
40 $dumper->Sortkeys(1);
41 }
42  
43 sub get_cache {
44 my $key = $_[0];
45 my $res = $cache->get($key);
46 if ($res) {
47 if ($res->[0] < time) {
48 $cache->remove($key);
49 return;
50 }
51 return $res->[1];
52 } else {
53 return;
54 }
55 }
56  
57 sub set_cache {
58 my ($key, $obj, $expires) = @_;
59 my $seconds = parse_duration($expires) || 60;
60 $cache->set($key, [$seconds + time, $obj]);
61 }
62  
63 sub remove_cache_key {
64 my $key = $_[0];
65 $cache->remove($key);
66 }
67  
68 sub make_request_cache_key {
69 my ($vreq, $cache_attr) = @_;
70 $cache_attr = {key => 'method', expires => $cache_attr} if not ref $cache_attr;
71 my @keys;
72 if (ref ($cache_attr->{key}) eq 'ARRAY') {
73 @keys = grep { exists $vreq->{$_} } @{$cache_attr->{key}};
74 } elsif (not exists $cache_attr->{key}) {
75 @keys = ('method');
76 } else {
77 @keys = ($cache_attr->{key});
78 }
79 $dumper->Values([{map { $_ => $vreq->{$_} } @keys}]);
80 return $dumper->Dump;
81 }
82  
83 1;
84  
85 __END__