| 1 | package AnyPAN::SourceCache; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Class::Accessor::Lite ro => [qw/cache_dir index_cache_timeout agent logger/]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Path::Tiny (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use AnyPAN::Index; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use AnyPAN::Logger::Null; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 1 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 13 | 1 | ▶ | $args{logger} ||= AnyPAN::Logger::Null->instance(); | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{"logger"} ||= "AnyPAN::Logger::Null"->instance
MC/DC0%0/2
$args{"logger"} ||= "AnyPAN::Logger::Null"->instance
$args{"logger"}
"AnyPAN::Logger::Null"->instance
Truth table33%1/3
A ||= B
A= $args{"logger"}B= "AnyPAN::Logger::Null"->instance
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | my $cache_dir = Path::Tiny->new(delete $args{cache_dir}); | |||||||||||||||||||||||||||
| 16 | 1 | bless { | |||||||||||||||||||||||||||
| 17 | %args, | ||||||||||||||||||||||||||||
| 18 | cache_dir => $cache_dir, | ||||||||||||||||||||||||||||
| 19 | index_cache => {}, | ||||||||||||||||||||||||||||
| 20 | } => $class; | ||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 6 | ▶ | sub get_or_fetch_index { | ||||||||||||||||||||||||||
|
Subroutine
get_or_fetch_index: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 6 | my ($self, $source) = @_; | |||||||||||||||||||||||||||
| 25 | 6 | ▶ | if ($self->{index_cache}->{$source->name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 3 | $self->logger->debug("memory cache hit source: @{[ $source->name ]}"); | |||||||||||||||||||||||||||
| 27 | 3 | return $self->{index_cache}->{$source->name}; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 3 | my $cache_dir = $self->cache_dir->child($source->name); | |||||||||||||||||||||||||||
| 31 | 3 | $cache_dir->mkpath(); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 3 | my $index_url = $source->index_url(); | |||||||||||||||||||||||||||
| 34 | 3 | my $index_path = $cache_dir->child($index_url->path); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 3 | my $timeout_at = time - $self->index_cache_timeout; | |||||||||||||||||||||||||||
| 37 | 3 | ▶ | if (!$index_path->exists || $index_path->stat->mtime < $timeout_at) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $index_path->exists or $index_path->stat->mtime < $timeout_at
MC/DC0%0/2
not $index_path->exists or $index_path->stat->mtime < $timeout_at
not $index_path->exists
$index_path->stat->mtime < $timeout_at
Truth table33%1/3
not(A) or B
A= not $index_path->existsB= $index_path->stat->mtime < $timeout_at
| |||||||||||||||||||||||||||||
| 38 | 3 | $index_path->parent->mkpath(); | |||||||||||||||||||||||||||
| 39 | 3 | $self->logger->info("download source @{[ $source->name ]} index"); | |||||||||||||||||||||||||||
| 40 | 3 | $self->agent->download($index_url, $index_path); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 3 | my $index = AnyPAN::Index->parse($index_path, $source); | |||||||||||||||||||||||||||
| 44 | 3 | $self->{index_cache}->{$source->name} = $index; | |||||||||||||||||||||||||||
| 45 | 3 | return $index; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 3 | ▶ | sub get_or_fetch_package { | ||||||||||||||||||||||||||
|
Subroutine
get_or_fetch_package: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 49 | 3 | my ($self, $source, $package_info) = @_; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 3 | my $cache_dir = $self->cache_dir->child($source->name); | |||||||||||||||||||||||||||
| 52 | 3 | $cache_dir->mkpath(); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 3 | my $package_url = $source->package_url($package_info->canonicalized_path); | |||||||||||||||||||||||||||
| 55 | 3 | my $package_path = $cache_dir->child($package_url->path); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 3 | ▶ | unless ($package_path->exists) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 3 | $package_path->parent->mkpath(); | |||||||||||||||||||||||||||
| 59 | 3 | $self->logger->info("download package @{[ $package_info->path ]} from @{[ $source->name ]}"); | |||||||||||||||||||||||||||
| 60 | 3 | $self->agent->download($package_url, $package_path); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 3 | return $package_path; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 2 | 1; | |||||||||||||||||||||||||||
| 67 | __END__ | ||||||||||||||||||||||||||||