File Coverage

blib/lib/AnyPAN/SourceCache.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 AnyPAN::SourceCache;
2 2     2   14 use strict;
  2         4  
  2         60  
3 2     2   10 use warnings;
  2         3  
  2         91  
4              
5 2     2   13 use Class::Accessor::Lite ro => [qw/cache_dir index_cache_timeout agent logger/];
  2         3  
  2         14  
6              
7 2     2   224 use Path::Tiny ();
  2         4  
  2         50  
8 2     2   833 use AnyPAN::Index;
  2         8  
  2         82  
9 2     2   15 use AnyPAN::Logger::Null;
  2         36  
  2         937  
10              
11             sub new {
12 1     1 0 74 my ($class, %args) = @_;
13 1   33     5 $args{logger} ||= AnyPAN::Logger::Null->instance();
14              
15 1         7 my $cache_dir = Path::Tiny->new(delete $args{cache_dir});
16 1         60 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 47 my ($self, $source) = @_;
25 6 100       20 if ($self->{index_cache}->{$source->name}) {
26 3         23 $self->logger->debug("memory cache hit source: @{[ $source->name ]}");
  3         17  
27 3         24 return $self->{index_cache}->{$source->name};
28             }
29              
30 3         32 my $cache_dir = $self->cache_dir->child($source->name);
31 3         168 $cache_dir->mkpath();
32              
33 3         646 my $index_url = $source->index_url();
34 3         9 my $index_path = $cache_dir->child($index_url->path);
35              
36 3         151 my $timeout_at = time - $self->index_cache_timeout;
37 3 50 33     87 if (!$index_path->exists || $index_path->stat->mtime < $timeout_at) {
38 3         98 $index_path->parent->mkpath();
39 3         3190 $self->logger->info("download source @{[ $source->name ]} index");
  3         27  
40 3         7598 $self->agent->download($index_url, $index_path);
41             }
42              
43 3         247 my $index = AnyPAN::Index->parse($index_path, $source);
44 3         273 $self->{index_cache}->{$source->name} = $index;
45 3         52 return $index;
46             }
47              
48             sub get_or_fetch_package {
49 3     3 0 26 my ($self, $source, $package_info) = @_;
50              
51 3         8 my $cache_dir = $self->cache_dir->child($source->name);
52 3         139 $cache_dir->mkpath();
53              
54 3         183 my $package_url = $source->package_url($package_info->canonicalized_path);
55 3         355 my $package_path = $cache_dir->child($package_url->path);
56              
57 3 50       151 unless ($package_path->exists) {
58 3         109 $package_path->parent->mkpath();
59 3         1929 $self->logger->info("download package @{[ $package_info->path ]} from @{[ $source->name ]}");
  3         25  
  3         28  
60 3         1395 $self->agent->download($package_url, $package_path);
61             }
62              
63 3         276 return $package_path;
64             }
65              
66             1;
67             __END__