/usr/share/perl5/Petal/Cache/Disk.pm is in libpetal-perl 2.23-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | # ------------------------------------------------------------------
# Petal::Cache::Disk - Caches generated code on disk.
# ------------------------------------------------------------------
# Author: Jean-Michel Hiver
# Description: A simple cache module to avoid re-generating the Perl
# code from the template file every time
# ------------------------------------------------------------------
package Petal::Cache::Disk;
use strict;
use warnings;
use File::Spec;
use Digest::MD5 qw /md5_hex/;
use Carp;
# kill silly warnings
sub sillyness
{
+ $Petal::INPUT &&
+ $Petal::OUTPUT;
}
# local $Petal::Cache::Disk::TMP_DIR = <some_dir>
# defaults to File::Spec->tmpdir;
our $TMP_DIR = File::Spec->tmpdir;
# local $Petal::Cache::Disk::PREFIX = <some_prefix>
# defaults to 'petal_cache_'
our $PREFIX = 'petal_cache';
# $class->get ($file, $lang);
# --------------------
# Returns the cached data if its last modification time is more
# recent than the last modification time of the template
# Returns the code for template file $file, undef otherwise
sub get
{
my $class = shift;
my $file = shift;
my $lang = shift || '';
my $key = $class->compute_key ($file, $lang);
return $class->cached ($key) if ($class->is_ok ($file, $lang));
return;
}
# $class->set ($file, $code, $lang);
# ---------------------------
# Sets the cached code for $file + $lang
sub set
{
my $class = shift;
my $file = shift;
my $code = shift;
my $lang = shift || '';
my $key = $class->compute_key ($file, $lang);
my $tmp = $class->tmp;
{
if ($] > 5.007)
{
open FP, ">:utf8", "$tmp/$key" or ( Carp::cluck "Cannot write-open $tmp/$key" and return );
}
else
{
open FP, ">$tmp/$key" or ( Carp::cluck "Cannot write-open $tmp/$key" and return );
}
print FP $code;
close FP;
}
}
# $class->is_ok ($file, $lang);
# ----------------------
# Returns TRUE if the cache is still fresh, FALSE otherwise.
sub is_ok
{
my $class = shift;
my $file = shift;
my $lang = shift || '';
my $key = $class->compute_key ($file, $lang);
my $tmp = $class->tmp;
my $tmp_file = "$tmp/$key";
return unless (-e $tmp_file);
my $cached_mtime = $class->cached_mtime ($file, $lang);
my $current_mtime = $class->current_mtime ($file);
return $cached_mtime >= $current_mtime;
}
# $class->compute_key ($file, $lang);
# ----------------------------
# Computes a cache 'key' for $file+$lang, which should be unique.
# (Well, currently an MD5 checksum is used, which is not
# *exactly* unique but which should be good enough)
sub compute_key
{
my $class = shift;
my $file = shift;
my $lang = shift || '';
my $key = md5_hex ($file . ";$lang" . ";INPUT=" . $Petal::INPUT . ";OUTPUT=" . $Petal::OUTPUT);
$key = $PREFIX . "_" . $Petal::VERSION . "_" . $key if (defined $PREFIX);
return $key;
}
# $class->cached_mtime ($file, $lang);
# -----------------------------
# Returns the last modification date of the cached data
# for $file + $lang
sub cached_mtime
{
my $class = shift;
my $file = shift;
my $lang = shift || '';
my $key = $class->compute_key ($file, $lang);
my $tmp = $class->tmp;
my $tmp_file = "$tmp/$key";
my $mtime = (stat($tmp_file))[9];
return $mtime;
}
# $class->current_mtime ($file);
# ------------------------------
# Returns the last modification date for $file
sub current_mtime
{
my $class = shift;
my $file = shift;
$file =~ s/#.*$//;
my $mtime = (stat($file))[9];
return $mtime;
}
# $class->cached ($key);
# ----------------------
# Returns the cached data for $key
sub cached
{
my $class = shift;
my $key = shift;
my $tmp = $class->tmp;
my $cached_filepath = $tmp . '/' . $key;
(-e $cached_filepath) or return;
my $res = undef;
{
if ($] > 5.007)
{
open FP, "<:utf8", "$tmp/$key" or ( Carp::cluck "Cannot read-open $tmp/$key" and return );
}
else
{
open FP, "<$tmp/$key" or ( Carp::cluck "Cannot read-open $tmp/$key" and return );
}
$res = join '', <FP>;
close FP;
}
return $res;
}
# $class->tmp;
# ------------
# Returns the temp directory in which the cached data is kept.
sub tmp
{
my $class = shift;
$TMP_DIR ||= File::Spec->tmpdir;
(-e $TMP_DIR) or confess "\$TMP_DIR '$TMP_DIR' does not exist";
(-d $TMP_DIR) or confess "\$TMP_DIR '$TMP_DIR' is not a directory";
$TMP_DIR =~ s/\/+$//;
return $TMP_DIR;
}
1;
|