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.20260302'; # 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 ▶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 ▶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 ▶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: $!" 0 2 2
36 }
37  
38 ▶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__
46  
47 =pod
48  
49 =encoding UTF-8
50  
51 =head1 NAME
52  
53 Mail::BIMI::CacheBackend::File - Cache handling
54  
55 =head1 VERSION
56  
57 version 3.20260302
58  
59 =head1 DESCRIPTION
60  
61 Cache worker role for File storage
62  
63 =head1 ATTRIBUTES
64  
65 These values are derived from lookups and verifications made based upon the input values, it is however possible to override these with other values should you wish to, for example, validate a record before it is published in DNS, or validate an Indicator which is only available locally
66  
67 =head2 parent
68  
69 is=ro required
70  
71 Parent class for cacheing
72  
73 =head1 CONSUMES
74  
75 =over 4
76  
77 =item * L<Mail::BIMI::Role::CacheBackend>
78  
79 =back
80  
81 =head1 EXTENDS
82  
83 =over 4
84  
85 =item * L<Moose::Object>
86  
87 =back
88  
89 =head1 METHODS
90  
91 =head2 I<get_from_cache()>
92  
93 Retrieve this class data from cache
94  
95 =head2 I<put_to_cache($data)>
96  
97 Put this classes data into the cache
98  
99 =head2 I<delete_cache>
100  
101 Delete the cache entry for this class
102  
103 =head1 REQUIRES
104  
105 =over 4
106  
107 =item * L<File::Slurp|File::Slurp>
108  
109 =item * L<Mail::BIMI::Prelude|Mail::BIMI::Prelude>
110  
111 =item * L<Moose|Moose>
112  
113 =item * L<Sereal|Sereal>
114  
115 =back
116  
117 =head1 AUTHOR
118  
119 Marc Bradshaw <marc@marcbradshaw.net>
120  
121 =head1 COPYRIGHT AND LICENSE
122  
123 This software is copyright (c) 2020 by Marc Bradshaw.
124  
125 This is free software; you can redistribute it and/or modify it under
126 the same terms as the Perl 5 programming language system itself.
127  
128 =cut
« BIMI/Role/HasError.pm ↑ Summary BIMI/Prelude.pm »