BIMI/CacheBackend/File.pm

filter: Statementstmt 100% 34 / 34 Branchbran 70.0% 7 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 94.6% 53 / 56 CC 6 SCAR 7.1
CC
6
Cov
93%
put_to_cache
3.1
get_from_cache
3.0
delete_cache
2.1
CRAP 2.0
« BIMI/Role/HasError.pm ↑ Summary BIMI/Prelude.pm »
1 package Mail::BIMI::CacheBackend::File;
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;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 29 ▶use Mail::BIMI::Prelude;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 ▶use File::Slurp qw{ read_file write_file };
Subroutine BEGIN: called CC=1 SCAR=0.0
8 29 ▶use Sereal qw{encode_sereal decode_sereal};
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 29 with 'Mail::BIMI::Role::CacheBackend';
11 29 has _cache_filename => ( is => 'ro', lazy => 1, builder => '_build_cache_filename' );
12  
13  
14  
15 19 ▶sub get_from_cache($self) {
Subroutine get_from_cache: called CC=3 SCAR=11.0
Pod documented
16 19 my $cache_file = $self->_cache_filename;
17 19 ▶ return if !-e $cache_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $cache_file 10 9 19
18 9 my $raw = scalar read_file($self->_cache_filename);
19 9 my $value = eval{ decode_sereal($raw) };
20 9 ▶ warn "Error reading from cache: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 8 9
21 9 return $value;
22 }
23  
24  
25 6 ▶sub put_to_cache($self,$data) {
Subroutine put_to_cache: called CC=3 SCAR=11.2
Pod documented
26 6 $self->parent->log_verbose('Writing '.(ref $self->parent).' to cache file '.$self->_cache_filename);
27 6 my $sereal_data = eval{ encode_sereal($data) };
28 6 ▶ warn "Error writing to cache: $@" if $@; # uncoverable branch
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
29 6 ▶ return unless $sereal_data; # uncoverable branch
Branch: 1 branch
BranchTrueFalseTotal
unless $sereal_data 0 6 6
30 6 write_file($self->_cache_filename,{atomic=>1},$sereal_data);
31 }
32  
33  
34 2 ▶sub delete_cache($self) {
Subroutine delete_cache: called CC=2 SCAR=7.6
Pod documented
35 2 ▶ unlink $self->_cache_filename or warn "Unable to unlink cache file: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $self->_cache_filename or warn "Unable to unlink cache file: $!" 2 0 2
36 }
37  
38 19 ▶sub _build_cache_filename($self) {
Subroutine _build_cache_filename: called CC=1 SCAR=0.0
39 19 my $cache_dir = $self->parent->bimi_object->options->cache_file_directory;
40 19 return $cache_dir.'mail-bimi-cache-'.$self->_cache_hash.'.cache';
41 }
42  
43 29 1;
44  
45 __END__
« BIMI/Role/HasError.pm ↑ Summary BIMI/Prelude.pm »