| 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
| |||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$attribute->does("Mail::BIMI::Trait::CacheKey") and $attribute->does("Mail::BIMI::Trait::Cacheable")
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")
| |||||||||||||||||||||||||||||||||||||
| 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} // ""
MC/DC100%1/1
$self->{$attribute_name} // ""
$self->{$attribute_name}
Truth table100%2/2
A
A= $self->{$attribute_name}
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 64 | 91 | ▶ | croak "No cacheable fields defined" if ! @cache_fields; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 74 | 11 | $self->log_verbose('Build '.(ref $self).' from cache'); | |||||||||||||||||||||||||||||||||||
| 75 | 11 | ▶ | if ($data->{cache_key} ne $self->_cache_key){ | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$version //= "dev"
$version
Truth table50%1/2
A
A= $version
| |||||||||||||||||||||||||||||||||||||
| 81 | 10 | ▶ | if ($data->{cache_version} ne $version){ | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$version //= "dev"
$version
Truth table50%1/2
A
A= $version
| |||||||||||||||||||||||||||||||||||||
| 115 | 10 | ▶ | my $data = { | ||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->_cache_read_timestamp // $time
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
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||