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   32971 use strict; use warnings FATAL => 'all';
  98     98   126  
  98         2329  
  98         292  
  98         111  
  98         37069  
3             # This is a prototype class for caching templates
4              
5             sub new
6             {
7 7     7 0 20 my( $proto, %kwargs ) = @_;
8            
9 7         19 @kwargs{'hits','misses'} = (0,0);
10            
11 7         69 return bless{ %kwargs }, $proto;
12             }
13              
14             sub get
15             {
16 92     92 0 123 my( $self, $key ) = @_;
17            
18 92         307 my $template = $self->validate_template(
19             $self->read_data(
20             $key
21             )
22             );
23            
24             defined $template ?
25             $self->{'hits'}++
26 92 100       212 : $self->{'misses'}++;
27            
28 92         358 return $template;
29             }
30              
31             sub put
32             {
33 53     53 0 87 my( $self, $key, $template, %kwargs ) = @_;
34            
35 53 50       88 if( defined $template )
36             {
37 53         103 my @keys = ('cache','url_source');
38 53         57 my @backup = @{$template}{@keys};
  53         108  
39 53         63 delete @{$template}{@keys};
  53         71  
40 53         163 $self->write_data($key, $template, %kwargs);
41 53         59 @{$template}{@keys} = @backup;
  53         143  
42             }
43 53         108 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 108 my( $self, $template ) = @_;
68 92 100       207 return if not defined $template;
69            
70             # here we check if template is still valid
71            
72             # check perl version
73 24 50 33     126 return if not $template->{'perl'} or $template->{'perl'} != $];
74            
75             # check modules version
76 24 50       60 if( my $modules = $template->{'modules'} )
77             {
78 24         66 foreach my $module (keys(%$modules))
79             {
80 82   66     400 my $current_version = $module->VERSION // $DTL::Fast::VERSION;
81 82 50       204 return if $modules->{$module} ne $current_version;
82             }
83             }
84              
85             # check files modification
86 24 50       62 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         38 return $template;
98             }
99              
100             1;