File Coverage

blib/lib/CPAN/MirrorMerger/MirrorCache.pm
Criterion Covered Total %
statement 52 52 100.0
branch 4 6 66.6
condition 2 6 33.3
subroutine 9 9 100.0
pod 0 3 0.0
total 67 76 88.1


line stmt bran cond sub pod time code
1             package CPAN::MirrorMerger::MirrorCache;
2 2     2   13 use strict;
  2         5  
  2         50  
3 2     2   10 use warnings;
  2         2  
  2         58  
4              
5 2     2   9 use Class::Accessor::Lite ro => [qw/cache_dir index_cache_timeout agent logger/];
  2         4  
  2         12  
6              
7 2     2   205 use Path::Tiny ();
  2         6  
  2         35  
8 2     2   740 use CPAN::MirrorMerger::Index;
  2         7  
  2         63  
9 2     2   14 use CPAN::MirrorMerger::Logger::Null;
  2         4  
  2         756  
10              
11             sub new {
12 1     1 0 59 my ($class, %args) = @_;
13 1   33     5 $args{logger} ||= CPAN::MirrorMerger::Logger::Null->instance();
14              
15 1         5 my $cache_dir = Path::Tiny->new(delete $args{cache_dir});
16 1         54 bless {
17             %args,
18             cache_dir => $cache_dir,
19             index_cache => {},
20             } => $class;
21             }
22              
23             sub get_or_fetch_index {
24 6     6 0 41 my ($self, $mirror) = @_;
25 6 100       17 if ($self->{index_cache}->{$mirror->name}) {
26 3         20 $self->logger->debug("memory cache hit mirror: @{[ $mirror->name ]}");
  3         16  
27 3         22 return $self->{index_cache}->{$mirror->name};
28             }
29              
30 3         28 my $cache_dir = $self->cache_dir->child($mirror->name);
31 3         137 $cache_dir->mkpath();
32              
33 3         536 my $index_url = $mirror->index_url();
34 3         8 my $index_path = $cache_dir->child($index_url->path);
35              
36 3         138 my $timeout_at = time - $self->index_cache_timeout;
37 3 50 33     23 if (!$index_path->exists || $index_path->stat->mtime < $timeout_at) {
38 3         79 $index_path->parent->mkpath();
39 3         2493 $self->logger->info("download mirror @{[ $mirror->name ]} index");
  3         22  
40 3         6122 $self->agent->download($index_url, $index_path);
41             }
42              
43 3         194 my $index = CPAN::MirrorMerger::Index->parse($index_path, $mirror);
44 3         230 $self->{index_cache}->{$mirror->name} = $index;
45 3         43 return $index;
46             }
47              
48             sub get_or_fetch_package {
49 3     3 0 21 my ($self, $mirror, $package_info) = @_;
50              
51 3         6 my $cache_dir = $self->cache_dir->child($mirror->name);
52 3         120 $cache_dir->mkpath();
53              
54 3         145 my $package_url = $mirror->package_url($package_info->canonicalized_path);
55 3         7 my $package_path = $cache_dir->child($package_url->path);
56              
57 3 50       114 unless ($package_path->exists) {
58 3         108 $package_path->parent->mkpath();
59 3         1682 $self->logger->info("download package @{[ $package_info->path ]} from @{[ $mirror->name ]}");
  3         23  
  3         24  
60 3         1142 $self->agent->download($package_url, $package_path);
61             }
62              
63 3         187 return $package_path;
64             }
65              
66             1;
67             __END__