File Coverage

lib/Petal/Cache/Memory.pm
Criterion Covered Total %
statement 37 46 80.4
branch 2 6 33.3
condition 8 10 80.0
subroutine 8 10 80.0
pod 0 7 0.0
total 55 79 69.6


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   599 use strict;
  77         132  
  77         2267  
10 77     77   400 use warnings;
  77         128  
  77         1895  
11 77     77   362 use Carp;
  77         130  
  77         39899  
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 14 my $class = shift;
32 8         23 my $file = shift;
33 8   100     27 my $lang = shift || '';
34 8         74 my $key = $class->compute_key ($file, $lang);
35 8 50       26 return $FILE_TO_SUBS->{$key} if ($class->is_ok ($file, $lang));
36 8         20 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 18 my $class = shift;
46 7         12 my $file = shift;
47 7         10 my $code = shift;
48 7   100     23 my $lang = shift || '';
49 7         22 my $key = $class->compute_key ($file, $lang);
50 7         29 $FILE_TO_SUBS->{$key} = $code;
51 7         25 $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 13 my $class = shift;
61 8         12 my $file = shift;
62 8   100     35 my $lang = shift || '';
63 8         20 my $key = $class->compute_key ($file, $lang);
64 8 50       36 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 15 my $class = shift;
92 7         10 my $file = shift;
93 7         12 $file =~ s/#.*$//;
94 7         120 my $mtime = (stat($file))[9];
95 7         43 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 60 my $class = shift;
107 23         33 my $file = shift;
108 23   100     55 my $lang = shift || '';
109              
110 23         72 my $key = $file . ";$lang" . ";INPUT=" . $Petal::INPUT . ";OUTPUT=" . $Petal::OUTPUT;
111 23         44 return $key;
112             }
113              
114              
115             1;