File Coverage

blib/lib/DTL/Fast/Cache.pm
Criterion Covered Total %
statement 33 42 78.5
branch 9 18 50.0
condition 3 9 33.3
subroutine 6 9 66.6
pod 0 7 0.0
total 51 85 60.0


line stmt bran cond sub pod time code
1             package DTL::Fast::Cache;
2 98     98   52160 use strict; use warnings FATAL => 'all';
  98     98   195  
  98         2708  
  98         485  
  98         170  
  98         55128  
3             # This is a prototype class for caching templates
4              
5             sub new
6             {
7 7     7 0 23 my( $proto, %kwargs ) = @_;
8            
9 7         27 @kwargs{'hits','misses'} = (0,0);
10            
11 7         85 return bless{ %kwargs }, $proto;
12             }
13              
14             sub get
15             {
16 92     92 0 150 my( $self, $key ) = @_;
17            
18 92         272 my $template = $self->validate_template(
19             $self->read_data(
20             $key
21             )
22             );
23            
24             defined $template ?
25             $self->{'hits'}++
26 92 100       217 : $self->{'misses'}++;
27            
28 92         459 return $template;
29             }
30              
31             sub put
32             {
33 53     53 0 123 my( $self, $key, $template, %kwargs ) = @_;
34            
35 53 50       127 if( defined $template )
36             {
37 53         111 my @keys = ('cache','url_source');
38 53         78 my @backup = @{$template}{@keys};
  53         141  
39 53         77 delete @{$template}{@keys};
  53         113  
40 53         301 $self->write_data($key, $template, %kwargs);
41 53         73 @{$template}{@keys} = @backup;
  53         175  
42             }
43 53         150 return $self;
44             }
45              
46             sub read_data
47             {
48 0     0 0 0 my( $self, $key ) = @_;
49 0         0 die "read_data method was not defined in ".(ref $self);
50             }
51              
52             sub clear
53             {
54 0     0 0 0 my( $self ) = @_;
55 0         0 die "clear method was not defined in ".(ref $self);
56             }
57              
58             sub write_data
59             {
60 0     0 0 0 my( $self, $key, $value ) = @_;
61            
62 0         0 die "write_data method was not defined in ".(ref $self);
63             }
64              
65             sub validate_template
66             {
67 92     92 0 145 my( $self, $template ) = @_;
68 92 100       247 return if not defined $template;
69            
70             # here we check if template is still valid
71            
72             # check perl version
73 24 50 33     149 return if not $template->{'perl'} or $template->{'perl'} != $];
74            
75             # check modules version
76 24 50       69 if( my $modules = $template->{'modules'} )
77             {
78 24         64 foreach my $module (keys(%$modules))
79             {
80 82   66     467 my $current_version = $module->VERSION // $DTL::Fast::VERSION;
81 82 50       272 return if $modules->{$module} ne $current_version;
82             }
83             }
84              
85             # check files modification
86 24 50       74 if( my $files = $template->{'inherits'} )
87             {
88 0         0 foreach my $file (keys( %$files ))
89             {
90 0 0       0 next if $file eq 'inline';
91             return if
92             not -e $file
93 0 0 0     0 or $files->{$file} != (stat($file))[9]
94             }
95             }
96            
97 24         50 return $template;
98             }
99              
100             1;