| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Cache::File::Simple; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | our @EXPORT = qw(cache); | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use v5.16; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use JSON::PP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Tie::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use File::Path; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Digest::SHA qw(sha256_hex); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 3 | our $CACHE_ROOT = "/tmp/cacheroot/"; | |||||||||||||||||||||||||||
| 19 | 3 | our $DEFAULT_EXPIRE = 3600; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # https://pause.perl.org/pause/query?ACTION=pause_operating_model#3_5_factors_considering_in_the_indexing_phase | ||||||||||||||||||||||||||||
| 22 | 3 | our $VERSION = '0.1'; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | ############################################################################### | ||||||||||||||||||||||||||||
| 25 | ############################################################################### | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Cache get: cache($key); | ||||||||||||||||||||||||||||
| 28 | # Cache set: cache($key, $val, $expires = 3600); | ||||||||||||||||||||||||||||
| 29 | 15 | ▶ | sub cache { | ||||||||||||||||||||||||||
|
Subroutine
cache: called CC=10 SCAR=23.6
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 15 | my ($key, $val, $expire, $ret, @data) = @_; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 15 | ▶ | my $hash = sha256_hex($key || ""); | ||||||||||||||||||||||||||
|
Condition50%1/2
$key || ''
MC/DC0%0/1
$key || ''
$key
Truth table50%1/2
A
A= $key
| |||||||||||||||||||||||||||||
| 33 | 15 | my $dir = "$CACHE_ROOT/perl-cache/" . substr($hash, 0, 3); | |||||||||||||||||||||||||||
| 34 | 15 | my $file = "$dir/$hash.json"; | |||||||||||||||||||||||||||
| 35 | 15 | mkpath($dir); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 15 | ▶ | tie @data, 'Tie::File', $file or die("Unable to write $file"); # to r/w file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 15 | ▶ | if (@_ > 1) { # Set | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 40 | 7 | ▶ | my $expires = int($expire || 0) || time() + $DEFAULT_EXPIRE; | ||||||||||||||||||||||||||
|
Condition80%4/5
int($expire || 0) || time + $Cache::File::Simple::DEFAULT_EXPIRE
$expire || 0
MC/DC0%0/2
int($expire || 0) || time + $Cache::File::Simple::DEFAULT_EXPIRE
$expire
time + $Cache::File::Simple::DEFAULT_EXPIRE
Truth table66%2/3
A || B
A= $expireB= time + $Cache::File::Simple::DEFAULT_EXPIRE
| |||||||||||||||||||||||||||||
| 41 | 7 | $data[0] = encode_json({ expires => $expires, data => $val, key => $key }); | |||||||||||||||||||||||||||
| 42 | 7 | $ret = 1; | |||||||||||||||||||||||||||
| 43 | ▶ | } elsif ($key && -r $file) { # Get | |||||||||||||||||||||||||||
|
Condition33%1/3
$key and -r $file
MC/DC0%0/2
$key and -r $file
$key
-r $file
Truth table33%1/3
A and B
A= $keyB= -r $file
| |||||||||||||||||||||||||||||
| 44 | 8 | eval { $ret = decode_json($data[0]); }; | |||||||||||||||||||||||||||
| 45 | 8 | ▶ | if ($ret->{expires} && $ret->{expires} > time()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ret->{"expires"} and $ret->{"expires"} > time
MC/DC50%1/2
$ret->{"expires"} and $ret->{"expires"} > time
$ret->{"expires"}
$ret->{"expires"} > time
Truth table66%2/3
A and B
A= $ret->{"expires"}B= $ret->{"expires"} > time
| |||||||||||||||||||||||||||||
| 46 | 5 | $ret = $ret->{data}; | |||||||||||||||||||||||||||
| 47 | } else { | ||||||||||||||||||||||||||||
| 48 | 3 | unlink($file); | |||||||||||||||||||||||||||
| 49 | 3 | $ret = undef; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 15 | return $ret; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # $num = cache_clean() | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub cache_clean { | ||||||||||||||||||||||||||
|
Subroutine
cache_clean: not called CC=10 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 0 | my ($verbose) = @_; | |||||||||||||||||||||||||||
| 59 | 0 | my $ret = 0; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # https://www.perturb.org/display/1306_Perl_Nested_subroutines.html | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | local *dir_is_empty = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | opendir(my $dh, $_[0]) or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | return scalar(grep { $_ ne "." && $_ ne ".." } readdir($dh)) == 0; | ||||||||||||||||||||||||||
|
Condition0%0/3
$_ ne '.' && $_ ne '..'
MC/DC0%0/2
$_ ne '.' && $_ ne '..'
$_ ne '.'
$_ ne '..'
Truth table0%0/3
A && B
A= $_ ne '.'B= $_ ne '..'
| |||||||||||||||||||||||||||||
| 65 | 0 | }; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | foreach my $file (glob("$CACHE_ROOT/*/*.json")) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | tie my @data, 'Tie::File', $file or die("Unable to write $file"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | my $x = decode_json($data[0] // {}); | ||||||||||||||||||||||||||
|
Condition0%0/2
$data[0] // {}
MC/DC0%0/1
$data[0] // {}
$data[0]
Truth table0%0/2
A
A= $data[0]
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ($x->{expires} < time()) { # File is expired | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ($verbose) { print "$file is expired\n"; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | $ret += int(unlink($file)); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | foreach my $dir (glob("$CACHE_ROOT/*")) { # Directory is empty | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | if (-d $dir && dir_is_empty($dir)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-d $dir and dir_is_empty($dir)
MC/DC0%0/2
-d $dir and dir_is_empty($dir)
-d $dir
dir_is_empty($dir)
Truth table0%0/3
A and B
A= -d $dirB= dir_is_empty($dir)
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | if ($verbose) { print "$dir is empty\n"; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | $ret += int(rmdir($dir)); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | return int($ret); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =pod | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head1 NAME | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | Cache::File::Simple - Dead simple file based caching meachanism | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | use Cache::File::Simple; | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | my $ckey = "cust:1234"; | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # Get data from the cache | ||||||||||||||||||||||||||||
| 100 | my $data = cache($ckey); | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # Store a scalar | ||||||||||||||||||||||||||||
| 103 | cache($ckey, "Jason Doolis"); | ||||||||||||||||||||||||||||
| 104 | cache($ckey, "Jason Doolis", time() + 7200); | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # Store an arrayref | ||||||||||||||||||||||||||||
| 107 | cache($ckey, [1, 2, 3]); | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # Store a hashref | ||||||||||||||||||||||||||||
| 110 | cache($ckey, {'one' => 1, 'two' => 2}); | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # Delete an item from the cache | ||||||||||||||||||||||||||||
| 113 | cache($ckey, undef); | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | C<Cache::File::Simple> exports a single C<cache()> function automatically. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | Store Perl data structures in an on-disk file cache. Cache entries can be given | ||||||||||||||||||||||||||||
| 120 | an expiration time to allow for easy clean up. | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head1 METHODS | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =over 4 | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =item B<cache($key)> | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | Get cache data for C<$key> from the cache | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =item B<cache($key, $obj)> | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | Store data in the cache for C<$key>. C<$obj> can be a scalar, listref, or hashref. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =item B<cache($key, $obj, $expires)> | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | Store data in the cache for C<$key> with an expiration time. C<$expires> is a | ||||||||||||||||||||||||||||
| 137 | unixtime after which the cache entry will be removed. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =item B<cache($key, undef)> | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Delete an entry from the cache. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =item B<Cache::File::Simple::cache_clean()> | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | Manually remove expired entries from the cache. Returns the number of items | ||||||||||||||||||||||||||||
| 146 | expired from the cache; | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =item B<$Cache::File::Simple::CACHE_ROOT> | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Change where the cache files are stored. Default C</tmp/cacheroot> | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =item B<$Cache::File::Simple::DEFAULT_EXPIRES> | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | Change the default time entries are cached for. Default 3600 seconds | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =back | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =cut | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 3 | 1; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # vim: tabstop=4 shiftwidth=4 autoindent softtabstop=4 | ||||||||||||||||||||||||||||