File Coverage

blib/lib/Pod/Cpandoc/Cache.pm
Criterion Covered Total %
statement 41 77 53.2
branch 0 14 0.0
condition 0 6 0.0
subroutine 14 20 70.0
pod 0 6 0.0
total 55 123 44.7


line stmt bran cond sub pod time code
1             package Pod::Cpandoc::Cache;
2 1     1   589 use 5.008005;
  1         4  
3 1     1   5 use strict;
  1         2  
  1         20  
4 1     1   10 use warnings;
  1         2  
  1         31  
5 1     1   5 use Carp;
  1         2  
  1         62  
6 1     1   508 use Data::Dumper;
  1         4475  
  1         60  
7 1     1   466 use parent 'Pod::Cpandoc';
  1         252  
  1         6  
8 1     1   69127 use File::Spec::Functions qw(catfile catdir);
  1         6  
  1         73  
9 1     1   10 use File::Basename qw(dirname);
  1         3  
  1         57  
10 1     1   7 use File::Path qw(make_path);
  1         3  
  1         31  
11 1     1   480 use File::Copy;
  1         2243  
  1         62  
12 1     1   579 use Class::Method::Modifiers;
  1         1393  
  1         68  
13 1     1   587 use Time::Piece 1.16;
  1         10344  
  1         7  
14              
15             our $VERSION = "0.04";
16 1     1   100 use constant DEBUG => $ENV{POD_CPANDOC_CACHE_DEBUG};
  1         2  
  1         73  
17 1     1   7 use constant TTL => 3600*24;
  1         2  
  1         567  
18              
19             sub live_cpan_url {
20 0     0 0   my $self = shift;
21 0           my $module = shift;
22 0 0         if ($self->opt_c) {
23 0           return $self->SUPER::live_cpan_url($module);
24             }
25 0           "https://fastapi.metacpan.org/v1/source/$module";
26             }
27              
28             around 'grand_search_init' => sub {
29             my $orig = shift;
30             my ($self, $module_names) = @_;
31             my $module_name = $module_names->[0];
32              
33             if ($self->opt_c) {
34             if ( my $found = $self->search_from_cache($module_name)) {
35             return $found;
36             }
37             my $found = $orig->(@_);
38             $self->put_cache_file($found,$module_name);
39             return $found;
40             }
41              
42             $orig->(@_);
43             };
44              
45             around 'searchfor' => sub {
46             my $orig = shift;
47             my ($self, undef, $module_name) = @_;
48              
49             if ( my $found = $self->search_from_cache($module_name)) {
50             return ($found);
51             }
52              
53             my @found = $orig->(@_) or return;
54              
55             warn "found number: ", scalar @found if DEBUG;
56             warn "found file: ", $found[0] if DEBUG;
57              
58             $self->put_cache_file($found[0],$module_name);
59              
60             return @found;
61             };
62              
63             sub search_from_cache {
64 0     0 0   my $self = shift;
65 0           my $module_name = shift;
66 0           my $path = $self->module_name_to_path($module_name);
67 0 0         return unless (-f $path);
68              
69 0           my $mtime = (stat($path))[9];
70              
71 0 0         if ( (localtime->epoch - localtime($mtime)->epoch) > TTL() ) {
72 0           warn 'expire cache' if DEBUG;
73 0           return;
74             }else{
75 0           warn 'search from cache' if DEBUG;
76 0           return $path;
77             }
78             }
79              
80             sub is_tempfile {
81 0     0 0   my $self = shift;
82 0           my $file_name = shift;
83 0           my $module_name = shift;
84              
85 0           my $hyphenated_module_name = join '-' => split('::',$module_name);
86 0           $file_name =~ /${hyphenated_module_name}-[a-zA-Z0-9_]{4}\.(pm|txt)\z/;
87             }
88              
89             sub cache_root_dir {
90 0     0 0   my $self = shift;
91             $self->{cache_root_dir} ||=
92 0   0       $ENV{POD_CPANDOC_CACHE_ROOT} || catdir($ENV{HOME}, '.pod_cpandoc_cache');
      0        
93             }
94              
95             sub module_name_to_path {
96 0     0 0   my $self = shift;
97 0           my $module_name = shift;
98 0 0         my $cache_file_path = catfile($self->cache_root_dir,split('::',$module_name)) . ($self->opt_c ? '.txt' : '.pm');
99 0           return $cache_file_path;
100             }
101              
102             sub put_cache_file {
103 0     0 0   my $self = shift;
104 0           my $tempfile_name = shift;
105 0           my $module_name = shift;
106              
107 0 0         if ($self->is_tempfile($tempfile_name,$module_name)) {
108 0           my $path = $self->module_name_to_path($module_name);
109 0           warn "put cache file: $path" if DEBUG;
110              
111 0           my $errors = [];
112 0           make_path(dirname($path),{ error => \$errors });
113 0 0         croak Dumper $errors if @$errors;
114              
115 0 0         copy($tempfile_name,$path) or die "Copy failed: $!";
116             }
117             }
118              
119              
120             1;
121              
122             __END__