| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# ------------------------------------------------------------------ |
|
2
|
|
|
|
|
|
|
# Petal::Cache::Memory - Caches generated subroutines in memory. |
|
3
|
|
|
|
|
|
|
# ------------------------------------------------------------------ |
|
4
|
|
|
|
|
|
|
# Author: Jean-Michel Hiver |
|
5
|
|
|
|
|
|
|
# Description: A simple cache module to avoid re-compiling the Perl |
|
6
|
|
|
|
|
|
|
# code from the Perl data at each request. |
|
7
|
|
|
|
|
|
|
# ------------------------------------------------------------------ |
|
8
|
|
|
|
|
|
|
package Petal::Cache::Memory; |
|
9
|
77
|
|
|
77
|
|
476
|
use strict; |
|
|
77
|
|
|
|
|
124
|
|
|
|
77
|
|
|
|
|
2452
|
|
|
10
|
77
|
|
|
77
|
|
361
|
use warnings; |
|
|
77
|
|
|
|
|
124
|
|
|
|
77
|
|
|
|
|
1632
|
|
|
11
|
77
|
|
|
77
|
|
324
|
use Carp; |
|
|
77
|
|
|
|
|
122
|
|
|
|
77
|
|
|
|
|
37195
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $FILE_TO_SUBS = {}; |
|
15
|
|
|
|
|
|
|
our $FILE_TO_MTIME = {}; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub sillyness |
|
19
|
|
|
|
|
|
|
{ |
|
20
|
0
|
0
|
|
0
|
0
|
0
|
+ $Petal::INPUT && $Petal::OUTPUT; |
|
21
|
|
|
|
|
|
|
} |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# $class->get ($file, $lang); |
|
25
|
|
|
|
|
|
|
# -------------------- |
|
26
|
|
|
|
|
|
|
# Returns the cached subroutine if its last modification time |
|
27
|
|
|
|
|
|
|
# is more recent than the last modification time of the template, |
|
28
|
|
|
|
|
|
|
# returns undef otherwise |
|
29
|
|
|
|
|
|
|
sub get |
|
30
|
|
|
|
|
|
|
{ |
|
31
|
8
|
|
|
8
|
0
|
23
|
my $class = shift; |
|
32
|
8
|
|
|
|
|
11
|
my $file = shift; |
|
33
|
8
|
|
100
|
|
|
38
|
my $lang = shift || ''; |
|
34
|
8
|
|
|
|
|
23
|
my $key = $class->compute_key ($file, $lang); |
|
35
|
8
|
50
|
|
|
|
24
|
return $FILE_TO_SUBS->{$key} if ($class->is_ok ($file, $lang)); |
|
36
|
8
|
|
|
|
|
21
|
return; |
|
37
|
|
|
|
|
|
|
} |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# $class->set ($file, $code, $lang); |
|
41
|
|
|
|
|
|
|
# --------------------------- |
|
42
|
|
|
|
|
|
|
# Sets the cached code for $file. |
|
43
|
|
|
|
|
|
|
sub set |
|
44
|
|
|
|
|
|
|
{ |
|
45
|
7
|
|
|
7
|
0
|
16
|
my $class = shift; |
|
46
|
7
|
|
|
|
|
9
|
my $file = shift; |
|
47
|
7
|
|
|
|
|
12
|
my $code = shift; |
|
48
|
7
|
|
100
|
|
|
20
|
my $lang = shift || ''; |
|
49
|
7
|
|
|
|
|
20
|
my $key = $class->compute_key ($file, $lang); |
|
50
|
7
|
|
|
|
|
22
|
$FILE_TO_SUBS->{$key} = $code; |
|
51
|
7
|
|
|
|
|
19
|
$FILE_TO_MTIME->{$key} = $class->current_mtime ($file); |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# $class->is_ok ($file, $lang); |
|
56
|
|
|
|
|
|
|
# ---------------------- |
|
57
|
|
|
|
|
|
|
# Returns TRUE if the cache is still fresh, FALSE otherwise. |
|
58
|
|
|
|
|
|
|
sub is_ok |
|
59
|
|
|
|
|
|
|
{ |
|
60
|
8
|
|
|
8
|
0
|
12
|
my $class = shift; |
|
61
|
8
|
|
|
|
|
11
|
my $file = shift; |
|
62
|
8
|
|
100
|
|
|
26
|
my $lang = shift || ''; |
|
63
|
8
|
|
|
|
|
16
|
my $key = $class->compute_key ($file, $lang); |
|
64
|
8
|
50
|
|
|
|
41
|
return unless (defined $FILE_TO_SUBS->{$key}); |
|
65
|
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
0
|
my $cached_mtime = $class->cached_mtime ($file, $lang); |
|
67
|
0
|
|
|
|
|
0
|
my $current_mtime = $class->current_mtime ($file); |
|
68
|
0
|
|
|
|
|
0
|
return $cached_mtime >= $current_mtime; |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# $class->cached_mtime ($file, $lang); |
|
73
|
|
|
|
|
|
|
# ----------------------------- |
|
74
|
|
|
|
|
|
|
# Returns the last modification date of the cached data |
|
75
|
|
|
|
|
|
|
# for $file & $lang |
|
76
|
|
|
|
|
|
|
sub cached_mtime |
|
77
|
|
|
|
|
|
|
{ |
|
78
|
0
|
|
|
0
|
0
|
0
|
my $class = shift; |
|
79
|
0
|
|
|
|
|
0
|
my $file = shift; |
|
80
|
0
|
|
0
|
|
|
0
|
my $lang = shift || ''; |
|
81
|
0
|
|
|
|
|
0
|
my $key = $class->compute_key ($file, $lang); |
|
82
|
0
|
|
|
|
|
0
|
return $FILE_TO_MTIME->{$key}; |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# $class->current_mtime ($file); |
|
87
|
|
|
|
|
|
|
# ------------------------------ |
|
88
|
|
|
|
|
|
|
# Returns the last modification date for $file |
|
89
|
|
|
|
|
|
|
sub current_mtime |
|
90
|
|
|
|
|
|
|
{ |
|
91
|
7
|
|
|
7
|
0
|
12
|
my $class = shift; |
|
92
|
7
|
|
|
|
|
9
|
my $file = shift; |
|
93
|
7
|
|
|
|
|
13
|
$file =~ s/#.*$//; |
|
94
|
7
|
|
|
|
|
104
|
my $mtime = (stat($file))[9]; |
|
95
|
7
|
|
|
|
|
39
|
return $mtime; |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# $class->compute_key ($file); |
|
100
|
|
|
|
|
|
|
# ---------------------------- |
|
101
|
|
|
|
|
|
|
# Computes a cache 'key' for $file, which should be unique. |
|
102
|
|
|
|
|
|
|
# (Well, currently an MD5 checksum is used, which is not |
|
103
|
|
|
|
|
|
|
# *exactly* unique but which should be good enough) |
|
104
|
|
|
|
|
|
|
sub compute_key |
|
105
|
|
|
|
|
|
|
{ |
|
106
|
23
|
|
|
23
|
0
|
30
|
my $class = shift; |
|
107
|
23
|
|
|
|
|
27
|
my $file = shift; |
|
108
|
23
|
|
100
|
|
|
99
|
my $lang = shift || ''; |
|
109
|
|
|
|
|
|
|
|
|
110
|
23
|
|
|
|
|
97
|
my $key = $file . ";$lang" . ";INPUT=" . $Petal::INPUT . ";OUTPUT=" . $Petal::OUTPUT; |
|
111
|
23
|
|
|
|
|
42
|
return $key; |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |