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
|
|
237
|
use strict; |
|
77
|
|
|
|
|
1492
|
|
|
77
|
|
|
|
|
3054
|
|
10
|
77
|
|
|
77
|
|
858
|
use warnings; |
|
77
|
|
|
|
|
75
|
|
|
77
|
|
|
|
|
3508
|
|
11
|
77
|
|
|
77
|
|
863
|
use Carp; |
|
77
|
|
|
|
|
716
|
|
|
77
|
|
|
|
|
30775
|
|
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
|
9
|
my $class = shift; |
32
|
8
|
|
|
|
|
9
|
my $file = shift; |
33
|
8
|
|
100
|
|
|
23
|
my $lang = shift || ''; |
34
|
8
|
|
|
|
|
17
|
my $key = $class->compute_key ($file, $lang); |
35
|
8
|
50
|
|
|
|
19
|
return $FILE_TO_SUBS->{$key} if ($class->is_ok ($file, $lang)); |
36
|
8
|
|
|
|
|
14
|
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
|
9
|
my $class = shift; |
46
|
7
|
|
|
|
|
8
|
my $file = shift; |
47
|
7
|
|
|
|
|
8
|
my $code = shift; |
48
|
7
|
|
100
|
|
|
19
|
my $lang = shift || ''; |
49
|
7
|
|
|
|
|
20
|
my $key = $class->compute_key ($file, $lang); |
50
|
7
|
|
|
|
|
22
|
$FILE_TO_SUBS->{$key} = $code; |
51
|
7
|
|
|
|
|
16
|
$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
|
9
|
my $class = shift; |
61
|
8
|
|
|
|
|
6
|
my $file = shift; |
62
|
8
|
|
100
|
|
|
25
|
my $lang = shift || ''; |
63
|
8
|
|
|
|
|
13
|
my $key = $class->compute_key ($file, $lang); |
64
|
8
|
50
|
|
|
|
28
|
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
|
7
|
my $class = shift; |
92
|
7
|
|
|
|
|
7
|
my $file = shift; |
93
|
7
|
|
|
|
|
11
|
$file =~ s/#.*$//; |
94
|
7
|
|
|
|
|
91
|
my $mtime = (stat($file))[9]; |
95
|
7
|
|
|
|
|
24
|
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
|
18
|
my $class = shift; |
107
|
23
|
|
|
|
|
20
|
my $file = shift; |
108
|
23
|
|
100
|
|
|
52
|
my $lang = shift || ''; |
109
|
|
|
|
|
|
|
|
110
|
23
|
|
|
|
|
50
|
my $key = $file . ";$lang" . ";INPUT=" . $Petal::INPUT . ";OUTPUT=" . $Petal::OUTPUT; |
111
|
23
|
|
|
|
|
29
|
return $key; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |