XML/Template/Cache/File.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 107 SCAR 50.5
CC
12
Cov
0%
load
110.0
_init
6.0
put
6.0
CRAP 156.0
1 ###############################################################################
2 # XML::Template::Cache::File
3 #
4 # Copyright (c) 2002-2003 Jonathan A. Waxman <jowaxman@bbl.med.upenn.edu>
5 # All rights reserved.
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the same terms as Perl itself.
9 ###############################################################################
10 package XML::Template::Cache::File;
11 use base qw(XML::Template::Base);
12  
13 use strict;
14 use XML::Template::Document;
15 use File::Spec;
16  
17  
18 =pod
19  
20 =head1 NAME
21  
22 XML::Template::Cache::File - Document caching to files module.
23  
24 =head1 SYNOPSIS
25  
26 use XML::Template::Cache::File;
27  
28 my $cache = XML::Template::Cache::File->new (%config)
29 || die XML::Template::Cache::File->error;
30 my $document = XML::Template::Document->new ();
31 $cache->put ($docname, $document);
32 $document = $cache->get ($docname);
33  
34 =head1 DESCRIPTION
35  
36 This modules provides XML template caching to files. The files store the
37 code generated from the template. When the template is requested, the
38 code from the file can be quickly loaded and evaluated.
39  
40 If file caching is turned on, in the initialization of
41 L<XML::Template::Process>, a file cache object is placed in the load and
42 put chain of responsiblity lists after the normal cache object. Hence,
43 every load and put operation on a document will result in the file cache
44 being queried right after the normal cache.
45  
46 =head1 CONSTRUCTOR
47  
48 A constructor method C<new> is provided by L<XML::Template::Base>. A list
49 of named configuration parameters may be passed to the constructor. The
50 constructor returns a reference to a new cache object or undef if an error
51 occurred. If undef is returned, you can use the method C<error> to
52 retrieve the error. For instance:
53  
54 my $cache = XML::Template::Cache::File->new (%config)
55 || die XML::Template::Cache->error;
56  
57 The following named configuration parameters are supported by this module:
58  
59 =over 4
60  
61 =item CacheDir
62  
63 The directory to store parsed XML templated in. This value will override
64 the default value C<$CACHE_DIR> in L<XML::Template::Config>. The default
65 file cache directory is C</tmp/-xml-template>.
66  
67 =back
68  
69 =head1 PRIVATE METHODS
70  
71 =head2 _init
72  
73 This method is the internal initialization function called from
74 L<XML::Template::Base> when a new cache object is created.
75  
76 =cut
77  
78 sub _init {
79 my $self = shift;
80 my %params = @_;
81  
82 print "XML::Template::Cache::_init\n" if $self->{_debug};
83  
84 $self->{_cache_dir} = $params{CacheDir} || XML::Template::Config->cache_dir
85 || return $self->error (XML::Template::Config->error);
86  
87 $self->{_enabled} = 1;
88  
89 return 1;
90 }
91  
92 =pod
93  
94 =head1 PUBLIC METHODS
95  
96 =head2 load
97  
98 my $document = $cache->load ($docname);
99  
100 The C<load> method, returns a document stored in the cache named by the
101 parameter. If no document is found, undef is returned.
102  
103 =cut
104  
105 sub load {
106 my $self = shift;
107 my $name = shift;
108  
109 my $cachefile = File::Spec->catdir ($self->{_cache_dir}, "$self->{_hostname}/$name");
110 my $code;
111  
112 print "XML::Template::Cache::File::load : Loading $cachefile\n" if $self->{_debug};
113  
114 # Find document in cache directory.
115 open (FILE, $cachefile)
116 || return undef;
117 my $sourceinfo = <FILE>;
118 chomp ($sourceinfo);
119 my ($source, $sourcename, @sourceinfo) = split (':', $sourceinfo);
120  
121 # Remove cached file if original template modified.
122 my $cache_mtime = (stat ($cachefile))[9];
123 my $unlink = 0;
124 if ($source eq 'file') {
125 if (-e $sourcename) {
126 print "XML::Template::Cache::File::load : Checking $sourcename\n" if $self->{_debug};
127 my $mtime = (stat ($sourcename))[9];
128 $unlink = $mtime > $cache_mtime;
129 }
130  
131 } elsif ($source eq 'source') {
132 print "XML::Template::Cache::File::load : Checking $sourceinfo\n" if $self->{_debug};
133 my $source = $self->get_source ($sourcename);
134 $unlink = $source->is_modified ($cache_mtime, @sourceinfo);
135 }
136 if ($unlink) {
137 print "XML::Template::Cache::File::load : $cachefile is old - unlinking\n" if $self->{_debug};
138 unlink $cachefile;
139 return undef;
140 }
141  
142 # Slurp in file.
143 while (<FILE>) { $code .= $_; }
144 close (FILE);
145  
146 print "XML::Template::Cache::File::load : Loaded $cachefile\n" if $self->{_debug};
147  
148 return XML::Template::Document->new (Code => $code);
149 }
150  
151 =pod
152  
153 =head2 put
154  
155 my $document = XML::Template::Document->new (Code => $code);
156 $cache->put ($docname, $document);
157  
158 The C<put> method stores a document in the cache. If the cache is full,
159 the oldest accessed document is replaced. The first parameter is the name
160 of the document. The second parameter is a blessed reference to the
161 L<XML::Template::Document> object to store.
162  
163 =cut
164  
165 sub put {
166 my $self = shift;
167 my $name = shift;
168 my $document = shift;
169  
170 my $file = "$self->{_cache_dir}/$self->{_hostname}/$name";
171  
172 # File exists - update access and modification time.
173 if (-e $file) {
174 # utime (time, time, $file);
175  
176 # File does not exist - create.
177 } else {
178 require File::Basename;
179 require File::Path;
180  
181 my ($filename, $filepath) = File::Basename::fileparse ($file);
182 File::Path::mkpath ($filepath);
183  
184 open (FILE, "> $file")
185 || return $self->error ('Cache/File', "Could not open $file: $!");
186 print FILE $document->source . "\n";
187 print FILE $document->code;
188 close (FILE);
189 }
190
191 return 1;
192 }
193  
194 =pod
195  
196 =head1 AUTHOR
197  
198 Jonathan Waxman
199 <jowaxman@bbl.med.upenn.edu>
200  
201 =head1 COPYRIGHT
202  
203 Copyright (c) 2002-2003 Jonathan A. Waxman
204 All rights reserved.
205  
206 This program is free software; you can redistribute it and/or
207 modify it under the same terms as Perl itself.
208  
209 =cut
210  
211  
212 1;