| 1 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # ABSTRACT: load YAML from cache or disk, whichever seems better | ||||||||||||||||||||||||||||
| 5 | package YAML::CacheLoader; | ||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.018'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use base qw( Exporter ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | our @EXPORT_OK = qw( LoadFile DumpFile FlushCache FreshenCache); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use constant CACHE_SECONDS => 3607; # Relatively nice prime number just over 1 hour. | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use constant CACHE_NAMESPACE => 'YAML-CACHELOADER'; # Make clear who dirtied up the memory | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Cache::RedisDB 0.07; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Path::Tiny 0.061; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use YAML::XS 0.59; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =over | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =item LoadFile | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | my $structure = LoadFile('/path/to/yml'[, $force_reload]); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | Loads the structure from '/path/to/yml' into $structure, preferring the cached version if available, | ||||||||||||||||||||||||||||
| 27 | otherwise reading the file and caching the result for 593 seconds (about 10 minutes). | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | If $force_reload is set to a true value, the file will be loaded from disk without regard | ||||||||||||||||||||||||||||
| 30 | to the current cache status. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub LoadFile { | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | my ($path, $force_reload) = @_; | ||||||||||||||||||||||||||
|
Subroutine
LoadFile: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | my $file_loc = path($path)->canonpath; # realpath would be more accurate, but slower. | |||||||||||||||||||||||||||
| 38 | 0 | my $structure; | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | if ($force_reload) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | $structure = _load_and_cache($file_loc); | |||||||||||||||||||||||||||
| 41 | } else { | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $structure = Cache::RedisDB->get(CACHE_NAMESPACE, $file_loc) // _load_and_cache($file_loc); | ||||||||||||||||||||||||||
|
Condition0%0/3
"Cache::RedisDB"->get("YAML-CACHELOADER", $file_loc) // _load_and_cache($file_loc)
MC/DC0%0/2
"Cache::RedisDB"->get("YAML-CACHELOADER", $file_loc) // _load_and_cache($file_loc)
"Cache::RedisDB"->get("YAML-CACHELOADER", $file_loc)
_load_and_cache($file_loc)
Truth table0%0/3
A // B
A= "Cache::RedisDB"->get("YAML-CACHELOADER", $file_loc)B= _load_and_cache($file_loc)
| |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | return $structure; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub _load_and_cache { | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | my $loc = shift; | ||||||||||||||||||||||||||
|
Subroutine
_load_and_cache: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | my $structure = YAML::XS::LoadFile($loc); # Let this fail in whatever ways it might. | |||||||||||||||||||||||||||
| 52 | 0 | ▶ | Cache::RedisDB->set(CACHE_NAMESPACE, $loc, $structure, CACHE_SECONDS) if ($structure); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | return $structure; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =item DumpFile | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | DumpFile('/path/to/yml', $structure); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | Dump the structure from $structure into '/path/to/yml', filling the cache along the way. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | sub DumpFile { | ||||||||||||||||||||||||||||
| 66 | 0 | ▶ | my ($path, $structure) = @_; | ||||||||||||||||||||||||||
|
Subroutine
DumpFile: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | my $file_loc = path($path)->canonpath; # realpath would be more accurate, but slower. | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | if ($structure) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | YAML::XS::DumpFile($file_loc, $structure); | |||||||||||||||||||||||||||
| 72 | 0 | Cache::RedisDB->set(CACHE_NAMESPACE, $file_loc, $structure, CACHE_SECONDS); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | return $structure; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =item FlushCache | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | FlushCache(); | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Remove all currently cached YAML documents from the cache server. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =cut | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | sub FlushCache { | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my @cached_files = _cached_files_list(); | ||||||||||||||||||||||||||
|
Subroutine
FlushCache: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | return (@cached_files) ? Cache::RedisDB->del(CACHE_NAMESPACE, @cached_files) : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =item FreshenCache | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | FreshenCache(); | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Freshen currently cached files which may be out of date, either by deleting the cache (for now deleted files) or reloading from the disk (for changed ones) | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | May optionally provide a list of files to check, otherwise all known cached files are checked. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Returns a stats hash-ref. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =back | ||||||||||||||||||||||||||||
| 103 | =cut | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | sub FreshenCache { | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | my (@file_list) = @_; | ||||||||||||||||||||||||||
|
Subroutine
FreshenCache: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | @file_list = _cached_files_list() unless @file_list; # By default check all currently cached. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | my @to_check = map { path($_) } @file_list; | |||||||||||||||||||||||||||
| 111 | # A good rough cut is to see if something _might_ have changed in the meantime | ||||||||||||||||||||||||||||
| 112 | 0 | my $cutoff = time - CACHE_SECONDS; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | my $stats = { | |||||||||||||||||||||||||||
| 115 | examined => scalar @to_check, | ||||||||||||||||||||||||||||
| 116 | cleared => 0, | ||||||||||||||||||||||||||||
| 117 | freshened => 0, | ||||||||||||||||||||||||||||
| 118 | }; | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | foreach my $file (@to_check) { | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | if (!$file->exists) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | $stats->{cleared}++ if (Cache::RedisDB->del(CACHE_NAMESPACE, $file->canonpath)); # Let's not cache things which don't exist. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | ▶ | } elsif ((my $mtime = $file->stat->mtime) > $cutoff | |||||||||||||||||||||||||||
|
Condition0%0/3
(my $mtime = $file->stat->mtime) > $cutoff and my $reloaded_ago = 3607 - "Cache::RedisDB"->ttl("YAML-CACHELOADER", $file->canonpath)
MC/DC0%0/2
(my $mtime = $file->stat->mtime) > $cutoff and my $reloaded_ago = 3607 - "Cache::RedisDB"->ttl("YAML-CACHELOADER", $file->canonpath)
(my $mtime = $file->stat->mtime) > $cutoff
my $reloaded_ago = 3607 - "Cache::RedisDB"->ttl("YAML-CACHELOADER", $file->canonpath)
Truth table0%0/3
A and B
A= (my $mtime = $file->stat->mtime) > $cutoffB= my $reloaded_ago = 3607 - "Cache::RedisDB"->ttl("YAML-CACHELOADER", $file->canonpath)
| |||||||||||||||||||||||||||||
| 124 | && (my $reloaded_ago = CACHE_SECONDS - Cache::RedisDB->ttl(CACHE_NAMESPACE, $file->canonpath))) | ||||||||||||||||||||||||||||
| 125 | { | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | $stats->{freshened}++ if (time - $reloaded_ago < $mtime && LoadFile($file, 1)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
time - $reloaded_ago < $mtime and LoadFile($file, 1)
MC/DC0%0/2
time - $reloaded_ago < $mtime and LoadFile($file, 1)
time - $reloaded_ago < $mtime
LoadFile($file, 1)
Truth table0%0/3
A and B
A= time - $reloaded_ago < $mtimeB= LoadFile($file, 1)
| |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | return $stats; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | sub _cached_files_list { | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | return @{Cache::RedisDB->keys(CACHE_NAMESPACE)}; | ||||||||||||||||||||||||||
|
Subroutine
_cached_files_list: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 1 | 1; | |||||||||||||||||||||||||||