BIMI/Role/Cacheable.pm

filter: Statementstmt 95.8% 92 / 96 Branchbran 82.3% 28 / 34 Conditioncond 58.3% 7 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 92.3% 12 / 13 Podpod 100% 1 / 1 totaltotal 86.5% 141 / 163 CC 26 SCAR 23.0
CC
26
Cov
89%
__ANON__
17.7
_write_cache
7.2
_build_cache_backend
4.0
CRAP 9.9
« BIMI/App/Command/checksvg.pm ↑ Summary BIMI/Result.pm »
1 package Mail::BIMI::Role::Cacheable;
2 # ABSTRACT: Cache handling
3 29 our $VERSION = '3.20261116'; # VERSION
4 29 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 29 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 29 ▶use Mail::BIMI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 ▶use Mail::BIMI::Prelude;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 29 ▶use Mail::BIMI::Trait::Cacheable;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 29 ▶use Mail::BIMI::Trait::CacheKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 29 ▶use Mail::BIMI::CacheBackend::FastMmap;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 29 ▶use Mail::BIMI::CacheBackend::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 29 ▶use Mail::BIMI::CacheBackend::Null;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 29 has _do_not_cache => ( is => 'rw', isa => 'Bool', required => 0 );
15 29 has _cache_read_timestamp => ( is => 'rw', required => 0 );
16 29 has _cache_key => ( is => 'rw' );
17 29 has _cache_fields => ( is => 'rw' );
18 29 has cache_backend => ( is => 'ro', lazy => 1, builder => '_build_cache_backend' );
19 29 requires 'cache_valid_for';
20  
21  
22  
23 0 ▶sub do_not_cache($self) {
Subroutine do_not_cache: not called CC=1 SCAR=6.9
Pod documented
24 0 $self->_do_not_cache(1);
25 }
26  
27 91 ▶sub _build_cache_backend($self) {
Subroutine _build_cache_backend: called CC=4 SCAR=13.9
28 91 my %opts = (
29 bimi_object => $self->bimi_object,
30 parent => $self,
31 );
32 91 my $backend_type = $self->bimi_object->options->cache_backend;
33 91 ▶ my $backend
Branch: 3 branches
BranchTrueFalseTotal
$backend_type eq 'FastMmap' ? : 9 82 91
$backend_type eq 'File' ? : 19 63 82
$backend_type eq 'Null' ? : 63 0 63
34 = $backend_type eq 'FastMmap' ? Mail::BIMI::CacheBackend::FastMmap->new( %opts )
35 : $backend_type eq 'File' ? Mail::BIMI::CacheBackend::File->new( %opts )
36 : $backend_type eq 'Null' ? Mail::BIMI::CacheBackend::Null->new( %opts )
37 : croak 'Unknown Cache Backend';
38 91 $self->log_verbose('Using cache backend '.$backend_type);
39 91 return $backend;
40 }
41  
42 91 ▶around new => sub{
Subroutine __ANON__: called CC=17 SCAR=28.7
43 91 my $original = shift;
44 91 my $class = shift;
45 91 my $self = $class->$original(@_);
46 91 my @cache_key;
47 my @cache_fields;
48  
49 91 my $meta = $self->meta;
50 91 foreach my $attribute_name ( sort $meta->get_attribute_list ) {
51 1561 my $attribute = $meta->get_attribute($attribute_name);
52 1561 ▶ if ( $attribute->does('Mail::BIMI::Trait::CacheKey') && $attribute->does('Mail::BIMI::Trait::Cacheable') ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($attribute->does('Mail::BIMI::Trait::CacheKey') and $attribute->does('Mail::BIMI::Trait::Cacheable')) { } 0 1561 1561
elsif ($attribute->does('Mail::BIMI::Trait::CacheKey')) { } 154 1407 1561
elsif ($attribute->does('Mail::BIMI::Trait::Cacheable')) { } 721 686 1407
Condition66%2/3
$attribute->does("Mail::BIMI::Trait::CacheKey") and $attribute->does("Mail::BIMI::Trait::Cacheable")
!ll&&!rl&&r
14071540
MC/DC0%0/2
$attribute->does("Mail::BIMI::Trait::CacheKey") and $attribute->does("Mail::BIMI::Trait::Cacheable")
$attribute->does("Mail::BIMI::Trait::CacheKey") $attribute->does("Mail::BIMI::Trait::Cacheable")
Truth table66%2/3
A and B
A= $attribute->does("Mail::BIMI::Trait::CacheKey")B= $attribute->does("Mail::BIMI::Trait::Cacheable")
ABresult
0X0
100
111
53 0 croak "Attribute $attribute_name cannot be BOTH is_cacheable AND is_cache_key";
54 }
55 elsif ( $attribute->does('Mail::BIMI::Trait::CacheKey') ) {
56 154 ▶ push @cache_key, "$attribute_name=".($self->{$attribute_name}//'');
Condition100%2/2
$self->{$attribute_name} // ""
l!l
1459
MC/DC100%1/1
$self->{$attribute_name} // ""
$self->{$attribute_name}
Truth table100%2/2
A
A= $self->{$attribute_name}
Aresult
11
00
57 }
58 elsif ( $attribute->does('Mail::BIMI::Trait::Cacheable') ) {
59 721 push @cache_fields, $attribute_name;
60 }
61 }
62  
63 91 ▶ croak "No cache key defined" if ! @cache_key;
Branch: 1 branch
BranchTrueFalseTotal
unless @cache_key 0 91 91
64 91 ▶ croak "No cacheable fields defined" if ! @cache_fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @cache_fields 0 91 91
65  
66 91 $self->_cache_key( join("\n",
67 ref $self,
68 @cache_key,
69 ));
70 91 $self->_cache_fields( \@cache_fields );
71  
72 91 my $data = $self->cache_backend->get_from_cache;
73 91 ▶ return $self if !$data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 80 11 91
74 11 $self->log_verbose('Build '.(ref $self).' from cache');
75 11 ▶ if ($data->{cache_key} ne $self->_cache_key){
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"cache_key"} ne $self->_cache_key) 1 10 11
76 1 warn 'Cache is invalid';
77 1 return $self;
78 }
79 10 my $version = $Mail::BIMI::VERSION;
80 10 ▶ $version //= 'dev';
Condition50%1/2
$version //= "dev"
l!l
100
MC/DC0%0/1
$version //= "dev"
$version
Truth table50%1/2
A
A= $version
Aresult
11
00
81 10 ▶ if ($data->{cache_version} ne $version){
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"cache_version"} ne $version) 0 10 10
82 0 warn 'Cache is invalid';
83 0 return $self;
84 }
85 10 ▶ if ($data->{timestamp}+$self->cache_valid_for < $self->bimi_object->time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"timestamp"} + $self->cache_valid_for < $self->bimi_object->time) 4 6 10
86 4 $self->cache_backend->delete_cache;
87 4 return $self;
88 }
89  
90 6 $self->_cache_read_timestamp($data->{timestamp});
91 6 foreach my $cache_field ( $self->_cache_fields->@* ) {
92 48 ▶ if ( exists ( $data->{data}->{$cache_field} )) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"data"}{$cache_field}) 39 9 48
93 39 my $value = $data->{data}->{$cache_field};
94 39 my $attribute = $meta->get_attribute($cache_field);
95 39 ▶ if ( $attribute->does('Mail::BIMI::Trait::CacheSerial') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->does('Mail::BIMI::Trait::CacheSerial')) { } 6 33 39
96 6 my $method_name = 'deserialize_'.$cache_field;
97 6 $self->$method_name($value);
98 }
99 else {
100 33 $self->{$cache_field} = $value;
101 }
102 }
103 }
104  
105 6 return $self;
106 29 };
107  
108 10 ▶sub _write_cache($self) {
Subroutine _write_cache: called CC=7 SCAR=19.7
109 10 ▶ return if $self->_do_not_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_do_not_cache 0 10 10
110 10 $self->_do_not_cache(1);
111 10 my $meta = $self->meta;
112 10 my $time = $self->bimi_object->time;
113 10 my $version = $Mail::BIMI::VERSION;
114 10 ▶ $version //= 'dev';
Condition50%1/2
$version //= "dev"
l!l
100
MC/DC0%0/1
$version //= "dev"
$version
Truth table50%1/2
A
A= $version
Aresult
11
00
115 10 ▶ my $data = {
Condition33%1/3
$self->_cache_read_timestamp // $time
l!l&&r!l&&!r
0100
MC/DC0%0/2
$self->_cache_read_timestamp // $time
$self->_cache_read_timestamp $time
Truth table33%1/3
A // B
A= $self->_cache_read_timestampB= $time
ABresult
1X1
011
000
116 cache_key => $self->_cache_key,
117 cache_version => $version,
118 timestamp => $self->_cache_read_timestamp // $time,
119 data => {},
120 };
121 10 foreach my $cache_field ( $self->_cache_fields->@* ) {
122 80 ▶ if ( defined ( $self->{$cache_field} )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$cache_field}) 62 18 80
123  
124 62 my $value = $self->{$cache_field};
125 62 my $attribute = $meta->get_attribute($cache_field);
126 62 ▶ if ( $attribute->does('Mail::BIMI::Trait::CacheSerial') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->does("Mail::BIMI::Trait::CacheSerial")) 10 52 62
127 10 my $method_name = 'serialize_'.$cache_field;
128 10 $value = $self->$method_name;
129 }
130  
131 62 $data->{data}->{$cache_field} = $value;
132 }
133 }
134  
135 10 $self->cache_backend->put_to_cache($data);
136 }
137  
138 29 1;
139  
140 __END__
« BIMI/App/Command/checksvg.pm ↑ Summary BIMI/Result.pm »