Cache/Driver/File.pm

filter: Statementstmt 96.6% 29 / 30 Branchbran 62.5% 5 / 8 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 83.9% 47 / 56 SCAR 18.2
CC
6
Cov
84%
remove
3.4
__ANON__
2.1
set
2.0
CRAP 6.2
« Cache.pm ↑ Summary
1 package Mojo::UserAgent::Role::Cache::Driver::File;
2 3 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 ▶use Mojo::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Mojo::Util 'md5_sum';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶has root_dir => sub { $ENV{MOJO_USERAGENT_CACHE_DIR} || Mojo::File::tempdir('mojo-useragent-cache-XXXXX') };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"MOJO_USERAGENT_CACHE_DIR"} or Mojo::File::tempdir("mojo-useragent-cache-XXXXX") 1 0 1
8  
9 sub get {
10 3 ▶ my $self = shift;
Subroutine get: called CC=2 SCAR=6.9
Pod documented
11 3 my $file = $self->_path(shift);
12 3 ▶ return -e $file ? $file->slurp : undef;
Branch: 1 branch
BranchTrueFalseTotal
-e $file ? : 1 2 3
13 }
14  
15 sub remove {
16 1 ▶ my $self = shift;
Subroutine remove: called CC=3 SCAR=12.3
Pod documented
17 1 my $file = $self->_path(shift);
18 1 ▶ unlink $file or die "unlink $file: $!" if -e $file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 1 0 1
Condition40%2/5
-e $file && (unlink($file) || die("unlink ${file}: $!"))
!ll&&!rl&&r
010
unlink $file or die "unlink ${file}: $!"
l!l
10
MC/DC0%0/2
-e $file && (unlink($file) || die("unlink ${file}: $!"))
-e $file unlink $file
Truth table0%0/3
A && B
A= -e $fileB= unlink $file
ABresult
0X0
100
111
19 1 return $self;
20 }
21  
22 sub set {
23 2 ▶ my $self = shift;
Subroutine set: called CC=2 SCAR=7.0
Pod documented
24 2 my $file = $self->_path(shift);
25 2 my $dir = Mojo::File->new($file->dirname);
26 2 ▶ $dir->make_path({mode => 0755}) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 2 0 2
27 2 $file->spurt(shift);
28 2 return $self;
29 }
30  
31 sub _path {
32 6 ▶ my ($self, $key) = @_;
Subroutine _path: called CC=1 SCAR=0.1
33 6 my $method = shift @$key;
34 6 my $last = substr md5_sum(pop @$key), 0, 12;
35  
36 6 return Mojo::File->new($self->root_dir, $method, (map { substr md5_sum($_), 0, 12 } @$key), "$last.http");
37 }
38  
39 3 1;
40  
41 =encoding utf8
42  
43 =head1 NAME
44  
45 Mojo::UserAgent::Role::Cache::Driver::File - Default cache driver for Mojo::UserAgent::Role::Cache
46  
47 =head1 SYNOPSIS
48  
49 my $driver = Mojo::UserAgent::Role::Cache::Driver::File->new;
50  
51 $driver->set($key, $data);
52 $data = $driver->get($key);
53 $driver->remove($key);
54  
55 =head1 DESCRIPTION
56  
57 L<Mojo::UserAgent::Role::Cache::Driver::File> is the default cache driver for
58 L<Mojo::UserAgent::Role::Cache>. It should provide the same interface as
59 L<CHI>.
60  
61 =head1 ATTRIBUTES
62  
63 =head2 root_dir
64  
65 $str = $self->root_dir;
66 $self = $self->root_dir("/path/to/mojo-useragent-cache");
67  
68 Where to store the cached files. Defaults to the C<MOJO_USERAGENT_CACHE_DIR>
69 environment variable or a L<tempdir|Mojo::File/tempdir>.
70  
71 =head1 METHODS
72  
73 =head2 get
74  
75 $data = $self->get($key);
76  
77 Retrive data from the cache. Returns C<undef()> if the C<$key> is not L</set>.
78  
79 =head2 remove
80  
81 $self = $self->remove($key);
82  
83 Removes data from the cache, by C<$key>.
84  
85 =head2 set
86  
87 $self = $self->set($key => $data);
88  
89 Stores new C<$data> in the cache.
90  
91 =head1 SEE ALSO
92  
93 L<Mojo::UserAgent::Role::Cache>.
94  
95 =cut
« Cache.pm ↑ Summary