| 1 | package Beam::Make::Cache; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 7 | our $VERSION = '0.003'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Store information about recipes performed | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 6 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 7 | #pod my $cache = Beam::Make::Cache->new; | ||||||||||||||||||||||||||||||||||||||||||
| 8 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 9 | #pod # Update the cache and track what the content should be | ||||||||||||||||||||||||||||||||||||||||||
| 10 | #pod $cache->set( 'recipe', 'content hash' ); | ||||||||||||||||||||||||||||||||||||||||||
| 11 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 12 | #pod # Set the last modified time to a specific time by passing | ||||||||||||||||||||||||||||||||||||||||||
| 13 | #pod # a Time::Piece object | ||||||||||||||||||||||||||||||||||||||||||
| 14 | #pod $cache->set( 'recipe', 'content hash', $timestamp ); | ||||||||||||||||||||||||||||||||||||||||||
| 15 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 16 | #pod # Get a Time::Piece object if the content hashes match | ||||||||||||||||||||||||||||||||||||||||||
| 17 | #pod # Otherwise returns 0 | ||||||||||||||||||||||||||||||||||||||||||
| 18 | #pod my $time = $cache->last_modified( 'recipe', 'content hash' ); | ||||||||||||||||||||||||||||||||||||||||||
| 19 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 20 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 21 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 22 | #pod This class provides an API to access timestamps and content hashes to validate | ||||||||||||||||||||||||||||||||||||||||||
| 23 | #pod recipes and determine which recipes are out-of-date and should be re-run. | ||||||||||||||||||||||||||||||||||||||||||
| 24 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 25 | #pod =head2 Limitations | ||||||||||||||||||||||||||||||||||||||||||
| 26 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 27 | #pod The cache file cannot be accessed by more than one process. This limitation may | ||||||||||||||||||||||||||||||||||||||||||
| 28 | #pod be fixed in the future. Other cache modules that use distributed databases may | ||||||||||||||||||||||||||||||||||||||||||
| 29 | #pod also be created in the future. | ||||||||||||||||||||||||||||||||||||||||||
| 30 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 31 | #pod =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 32 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 33 | #pod L<Beam::Make> | ||||||||||||||||||||||||||||||||||||||||||
| 34 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 35 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 7 | ▶ | use v5.20; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 7 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 7 | ▶ | use experimental qw( signatures postderef ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 7 | ▶ | use File::stat; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 7 | ▶ | use Time::Piece; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 7 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 7 | ▶ | use YAML (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | #pod =attr file | ||||||||||||||||||||||||||||||||||||||||||
| 47 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 48 | #pod The path to a file to use for the cache. Defaults to C<.Beamfile.cache> in | ||||||||||||||||||||||||||||||||||||||||||
| 49 | #pod the current directory. | ||||||||||||||||||||||||||||||||||||||||||
| 50 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 51 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 9 | ▶ | has file => ( is => 'ro', default => sub { '.Beamfile.cache' } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 7 | has _last_read => ( is => 'rw', default => 0 ); | |||||||||||||||||||||||||||||||||||||||||
| 55 | 10 | ▶ | has _cache => ( is => 'rw', default => sub { {} } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | #pod =method set | ||||||||||||||||||||||||||||||||||||||||||
| 58 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 59 | #pod $cache->set( $name, $hash, $time ); | ||||||||||||||||||||||||||||||||||||||||||
| 60 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 61 | #pod # Update modified time to now | ||||||||||||||||||||||||||||||||||||||||||
| 62 | #pod $cache->set( $name, $hash ); | ||||||||||||||||||||||||||||||||||||||||||
| 63 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 64 | #pod Set an entry in the cache. C<$name> is the recipe name. C<$hash> is an identifier | ||||||||||||||||||||||||||||||||||||||||||
| 65 | #pod for the content (usually a base64 SHA-1 hash from L<Digest::SHA>). C<$time> is a | ||||||||||||||||||||||||||||||||||||||||||
| 66 | #pod L<Time::Piece> object to save as the last modified time. If C<$time> is not provided, | ||||||||||||||||||||||||||||||||||||||||||
| 67 | #pod defaults to now. | ||||||||||||||||||||||||||||||||||||||||||
| 68 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 69 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 10 | ▶ | sub set( $self, $name, $hash, $time=Time::Piece->new ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 10 | my $cache = $self->_fetch_cache; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 10 | ▶ | $cache->{ $name } = { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | hash => $hash, | ||||||||||||||||||||||||||||||||||||||||||
| 75 | time => blessed $time eq 'Time::Piece' ? $time->epoch : $time, | ||||||||||||||||||||||||||||||||||||||||||
| 76 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 10 | $self->_write_cache( $cache ); | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | #pod =method last_modified | ||||||||||||||||||||||||||||||||||||||||||
| 81 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 82 | #pod my $time = $cache->last_modified( $name, $hash ); | ||||||||||||||||||||||||||||||||||||||||||
| 83 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 84 | #pod Get the last modified timestamp (as a L<Time::Piece> object) for the | ||||||||||||||||||||||||||||||||||||||||||
| 85 | #pod given recipe C<$name>. If the C<$hash> does not match what was given to | ||||||||||||||||||||||||||||||||||||||||||
| 86 | #pod L</set>, or if the recipe has never been made, returns C<0>. | ||||||||||||||||||||||||||||||||||||||||||
| 87 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 88 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 40 | ▶ | sub last_modified( $self, $name, $hash ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
last_modified: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 40 | my $cache = $self->_fetch_cache; | |||||||||||||||||||||||||||||||||||||||||
| 92 | return Time::Piece->new( $cache->{ $name }{ time } ) | ||||||||||||||||||||||||||||||||||||||||||
| 93 | if $cache->{ $name } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 40 | ▶ | && $cache->{ $name }{ hash } eq $hash | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cache->{$name} and $cache->{$name}{"hash"} eq $hash
MC/DC100%2/2
$cache->{$name} and $cache->{$name}{"hash"} eq $hash
$cache->{$name}
$cache->{$name}{"hash"} eq $hash
Truth table100%3/3
A and B
A= $cache->{$name}B= $cache->{$name}{"hash"} eq $hash
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | ; | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 7 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 60 | ▶ | sub _fetch_cache( $self ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fetch_cache: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 60 | my $last_read = $self->_last_read; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 60 | ▶ | if ( -e $self->file && ( !$last_read || stat( $self->file )->mtime > $last_read ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
-e $self->file and !$last_read || &Beam::Make::Cache::stat(scalar $self->file)->mtime > $last_read
not $last_read or &Beam::Make::Cache::stat(scalar $self->file)->mtime > $last_read
MC/DC66%2/3
-e $self->file and !$last_read || &Beam::Make::Cache::stat(scalar $self->file)->mtime > $last_read
-e $self->file
not $last_read
&Beam::Make::Cache::stat(scalar $self->file)->mtime > $last_read
Truth table75%3/4
A and not(B) or C
A= -e $self->fileB= not $last_readC= &Beam::Make::Cache::stat(scalar $self->file)->mtime > $last_read
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 5 | $self->_last_read( stat( $self->file )->mtime ); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 5 | $self->_cache( YAML::LoadFile( $self->file ) ); | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 60 | return $self->_cache; | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 10 | ▶ | sub _write_cache( $self, $cache ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_write_cache: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 10 | my $old_cache = $self->_fetch_cache; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 10 | $cache = { %$old_cache, %$cache }; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 10 | YAML::DumpFile( $self->file, $cache ); | |||||||||||||||||||||||||||||||||||||||||
| 112 | 10 | $self->_cache( $cache ); | |||||||||||||||||||||||||||||||||||||||||
| 113 | 10 | $self->_last_read( stat( $self->file )->mtime ); | |||||||||||||||||||||||||||||||||||||||||
| 114 | 10 | return; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | __END__ | ||||||||||||||||||||||||||||||||||||||||||