| 1 | package Mojo::UserAgent::Role::Cache::Driver::File; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use Mojo::File; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Mojo::Util qw(md5_sum url_unescape); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use constant DEBUG => $ENV{MOJO_CLIENT_DEBUG} || $ENV{MOJO_UA_CACHE_DEBUG} || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=12.5
Condition40%2/5
$ENV{'MOJO_CLIENT_DEBUG'} || $ENV{'MOJO_UA_CACHE_DEBUG'} || 0
$ENV{"MOJO_CLIENT_DEBUG"} or $ENV{"MOJO_UA_CACHE_DEBUG"}
MC/DC0%0/2
$ENV{'MOJO_CLIENT_DEBUG'} || $ENV{'MOJO_UA_CACHE_DEBUG'} || 0
$ENV{"MOJO_CLIENT_DEBUG"}
$ENV{"MOJO_UA_CACHE_DEBUG"}
Truth table33%1/3
A or B
A= $ENV{"MOJO_CLIENT_DEBUG"}B= $ENV{"MOJO_UA_CACHE_DEBUG"}
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use constant RENAME => $ENV{MOJO_UA_CACHE_RENAME} || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{'MOJO_UA_CACHE_RENAME'} || 0
MC/DC100%1/1
$ENV{'MOJO_UA_CACHE_RENAME'} || 0
$ENV{'MOJO_UA_CACHE_RENAME'}
Truth table100%2/2
A
A= $ENV{'MOJO_UA_CACHE_RENAME'}
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 43 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 43 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 14 | 43 | my $file = $self->_path($key); | |||||||||||||||||||||||||||||||||||||||||
| 15 | 43 | $self->_try_to_rename($file, @$key) if RENAME and !-e $file; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 43 | my $exists = -e $file; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 43 | warn qq(-- Reading Mojo::UserAgent cache file $file\n) if DEBUG and $exists; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 43 | ▶ | return $exists ? $file->slurp : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | sub remove { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | my $file = $self->_path(shift); | |||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | unlink $file or die "unlink $file: $!" if -e $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
-e $file && (unlink($file) || die("unlink ${file}: $!"))
unlink $file or die "unlink ${file}: $!"
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
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 18 | ▶ | sub set { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 18 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 18 | my $file = $self->_path(shift); | |||||||||||||||||||||||||||||||||||||||||
| 31 | 18 | my $dir = Mojo::File->new($file->dirname); | |||||||||||||||||||||||||||||||||||||||||
| 32 | 18 | warn qq(-- Writing Mojo::UserAgent cache file $file\n) if DEBUG; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 18 | ▶ | $dir->make_path({mode => 0755}) unless -d $dir; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 18 | $file->spurt(shift); | |||||||||||||||||||||||||||||||||||||||||
| 35 | 18 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 62 | ▶ | sub _path { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 62 | my ($self, @key) = ($_[0], @{$_[1]}); | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 242 | ▶ | my $safe = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 242 | my $len = length; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 242 | ▶ | ($len < 100 && $len != 32 && m!^[\w+\.-]+$!) ? $_ : md5_sum($_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$len < 100 and $len != 32 and /^[\w+\.-]+$/u
$len < 100 and $len != 32
MC/DC100%3/3
$len < 100 and $len != 32 and /^[\w+\.-]+$/u
$len < 100
$len != 32
/^[\w+\.-]+$/u
Truth table100%4/4
A and B and C
A= $len < 100B= $len != 32C= /^[\w+\.-]+$/u
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 62 | }; | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 62 | my $last = $safe->(local $_ = pop @key); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 62 | return Mojo::File->new($self->root_dir, (map { $safe->() } @key), "$last.http"); | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | # Will be removed in the future | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | ▶ | sub _try_to_rename { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_try_to_rename: called CC=7 SCAR=20.6
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | my ($self, $to, @key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | my @old = (shift @key, shift @key); # method and host | |||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | my $body = $key[-1] =~ s!^\?b=!! ? pop @key : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | my $url = Mojo::URL->new('/'); | |||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | ▶ | $url->query->parse($1) if $key[-1] =~ m!^\?q=(.*)!; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | pop @key; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | $url->path->parts([map { url_unescape $_ } @key]); | |||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | push @old, $url->path_query; | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | ▶ | push @old, $body if defined $body; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | my $last = substr md5_sum(pop @old), 0, 12; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | my $from = Mojo::File->new($self->root_dir, shift @old, (map { substr md5_sum($_), 0, 12 } @old), "$last.http"); | |||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | my $to_dir = Mojo::File->new($to->dirname); | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | ▶ | $to_dir->make_path({mode => 0755}) unless -d $to_dir; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | rename $from, $to or die "Rename $from $to: $!" if -e $from; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
-e $from && (rename($from, $to) || die("Rename $from ${to}: $!"))
rename $from, $to or die "Rename $from ${to}: $!"
MC/DC0%0/2
-e $from && (rename($from, $to) || die("Rename $from ${to}: $!"))
-e $from
rename $from, $to
Truth table0%0/3
A && B
A= -e $fromB= rename $from, $to
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | Mojo::UserAgent::Role::Cache::Driver::File - Default cache driver for Mojo::UserAgent::Role::Cache | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | my $driver = Mojo::UserAgent::Role::Cache::Driver::File->new; | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | $driver->set(\@key, $data); | ||||||||||||||||||||||||||||||||||||||||||
| 85 | $data = $driver->get(\@key); | ||||||||||||||||||||||||||||||||||||||||||
| 86 | $driver->remove(\@key); | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | L<Mojo::UserAgent::Role::Cache::Driver::File> is the default cache driver for | ||||||||||||||||||||||||||||||||||||||||||
| 91 | L<Mojo::UserAgent::Role::Cache>. It should provide the same interface as | ||||||||||||||||||||||||||||||||||||||||||
| 92 | L<CHI>. | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | =head2 root_dir | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | $str = $self->root_dir; | ||||||||||||||||||||||||||||||||||||||||||
| 99 | $self = $self->root_dir("/path/to/mojo-useragent-cache"); | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | Where to store the cached files. Defaults to the C<MOJO_USERAGENT_CACHE_DIR> | ||||||||||||||||||||||||||||||||||||||||||
| 102 | environment variable or a L<tempdir|Mojo::File/tempdir>. | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | =head2 get | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | $data = $self->get(\@key); | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | Retrive data from the cache. Returns C<undef()> if the C<@key> is not L</set>. | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | =head2 remove | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | $self = $self->remove(\@key); | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | Removes data from the cache, by C<@key>. | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | =head2 set | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | $self = $self->set(\@key => $data); | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | Stores new C<$data> in the cache. | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | L<Mojo::UserAgent::Role::Cache>. | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||||||||||||||||||||||||