V2/Cache.pm untested

filter: Statementstmt 0.0% 0 / 218 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 44 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 358 CC 26 SCAR 58.7
CC
26
Cov
0%
setPageCache
552.0
cacheValue
12.0
flushCache
6.0
CRAP 354.4
1 package FWS::V2::Cache;
2  
3 use 5.006;
4 use strict;
5 use warnings;
6 no warnings 'uninitialized';
7  
8 =head1 NAME
9  
10 FWS::V2::Cache - Framework Sites version 2 data caching
11  
12 =head1 VERSION
13  
14 Version 1.13091122
15  
16 =cut
17  
18 our $VERSION = '1.13091122';
19  
20  
21 =head1 SYNOPSIS
22  
23 use FWS::V2;
24
25 my $fws = FWS::V2->new();
26  
27  
28 my $cacheData = $fws->cacheValue( 'someData' ) || &{ sub {
29 my $value = 'This is a testineeg!';
30 #
31 # do something...
32 #
33 return $fws->saveCache( key => 'someData', expire => 1, value => $value );
34 } };
35  
36 print $cacheData;
37
38 $fws->deleteCache( key => 'someData' );
39
40 $fws->flushCache();
41  
42  
43 =head1 DESCRIPTION
44  
45 FWS version 2 cache methods are used as a portable non-environment specific cache library to the FWS data model. For maximum compatibility it uses the file FWSCache directory under the sites fileSecurePath directory to store its data and is compatible with NFS or other distributed file systems.
46  
47 =head1 METHODS
48  
49 =head2 saveCache
50  
51 Save data to cache passing they key, value and expire (in minutes). The return will be the string passed as value.
52  
53 #
54 # Save my data to cache and hang on to it for 5 minutes before getting a new a new one
55 # but return the current one if my 5 minutes isn't up
56 #
57 $someValue = $fws->saveCache( key => 'someData', value => $someValue, expire => 5 );
58  
59 =cut
60  
61 sub saveCache {
62 my ( $self, %paramHash ) = @_;
63  
64 #
65 # set default expire if not passed
66 #
67 $paramHash{expire} ||= 60;
68  
69 #
70 # make the dir for the cache
71 #
72 my $cacheDir = $self->{fileSecurePath} . "/cache/" . $self->safeFile( $paramHash{key} ) ;
73 $self->makeDir( $cacheDir );
74  
75 #
76 # set cache file based on expire date
77 #
78 my $newFile = $cacheDir . '/' . ( time() + ( $paramHash{expire} * 60 ) );
79
80 #
81 # set the file name as the exp epoch with its content
82 #
83 my $expireEpoch = ( time() + ( $paramHash{expire} * 60 ) );
84 open ( my $CFILE, ">", $newFile );
85 print $CFILE $expireEpoch."\n";
86 print $CFILE $paramHash{value};
87 close $CFILE;
88
89 #
90 # Move it to the live value, or if we have something nasty from file locking happen
91 # what ever is there will be good in those rare cases
92 #
93 rename $newFile, $cacheDir.'/value';
94  
95 #
96 # return what we were givin
97 #
98 return $paramHash{value};
99 }
100  
101  
102 =head2 deleteCache
103  
104 Remove a key from cache. For optmization, this does not look up the key, only blindly remove it.
105  
106 #
107 # We no longer need somData lets get rid of it
108 #
109 $fws->deleteCache( key => 'someData' );
110  
111 =cut
112  
113 sub deleteCache {
114 my ( $self, %paramHash ) = @_;
115
116 my $cacheDir = $self->{fileSecurePath} . "/cache/" . $self->safeFile( $paramHash{key} );
117  
118 my @fileArray = @{$self->fileArray( directory => $cacheDir ) };
119 for my $i (0 .. $#fileArray) { unlink $fileArray[$i]{fullFile} }
120  
121 rmdir $cacheDir;
122  
123 return;
124 }
125  
126  
127 =head2 flushCache
128  
129 If anything is in cache currently, after this call it won't be!
130  
131 #
132 # Remove all cache keys
133 #
134 $fws->flushCache();
135  
136 =cut
137  
138 sub flushCache {
139 my ( $self ) = @_;
140  
141 my $cacheDir = $self->{fileSecurePath} . "/cache" ;
142  
143 #
144 # pull the directory into an array
145 #
146 opendir( DIR, $cacheDir );
147 my @getDir = grep(!/^\.\.?$/,readdir( DIR ));
148 closedir( DIR );
149  
150 #
151 # eat each cache in the dir
152 #
153 foreach my $dirFile (@getDir) { if (-d $cacheDir.'/'.$dirFile) { $self->deleteCache( key => $dirFile ) } }
154  
155 $self->runScript( 'postFlushCache' );
156
157 $self->FWSLog( 'Cache Flushed' );
158
159 return;
160 }
161  
162  
163 =head2 flushWebCache
164  
165 Removes all files located in your web cache. These are the files that are your combined js, and css files created on the fly when pages are rendered. When pages are loaded after this is ran, it will start to repopulate the cache with newly created web cache files.
166  
167 #
168 # Remove all web cache files
169 #
170 $fws->flushWebCache();
171  
172 =cut
173  
174 sub flushWebCache {
175 my ( $self ) = @_;
176  
177 my @fileArray = @{$self->fileArray( directory => $self->{filePath} . '/fws/cache' )};
178 for my $i ( 0 .. $#fileArray ) { unlink $fileArray[$i]{fullFile} }
179  
180 $self->runScript( 'postFlushWebCache' );
181
182 $self->FWSLog( 'Web Cache Flushed' );
183  
184 return;
185 }
186  
187  
188 =head2 cacheValue
189  
190 Return the cache value. If the cache value does not exist, it will return blank in same way that formValue, siteValue and userValue.
191  
192 #
193 # will return the cache value. This or a blank string if it is not set, or blank
194 #
195 print $fws->caheValue( 'someData' );
196  
197 =cut
198  
199 sub cacheValue {
200 my ( $self, $key ) = @_;
201  
202 #
203 # set cache file based on expire date
204 # set the value to blank to start
205 #
206 my $cacheDir = $self->{fileSecurePath} . "/cache/" . $self->safeFile( $key );
207 my $newFile = $cacheDir . '/value';
208 my $value;
209  
210 #
211 # if the cache file does not exist then return the blank
212 #
213 if ( !-e $newFile ) { return $value }
214  
215 #
216 # open the file and get the first line
217 #
218 open ( my $VFILE, "<", $newFile );
219 chomp ( my $timeStamp = <$VFILE> );
220  
221 #
222 # if we are still within timestamp lets return it
223 #
224 if ( $timeStamp > time() ) { while ( <$VFILE> ) { $value .= $_ } }
225
226 #
227 # Return the value and close the file
228 #
229 close $VFILE;
230 return $value;
231 }
232  
233  
234 =head2 setPageCache
235  
236 Used internally to craete combined head css and js cached files so they can be used on the page.
237  
238 =cut
239  
240 sub setPageCache {
241 my ( $self, %paramHash ) = @_;
242  
243 use Digest::MD5 qw(md5_hex);
244  
245 #
246 # PH for settables
247 #
248 my $pageHead;
249 my $pageFoot;
250 my $cacheFileName = $self->{siteId} . '-';
251  
252 #
253 # make it unique depending on flags passed
254 #
255 if ( $paramHash{jqueryOnly} ) { $cacheFileName .= 'jqueryOnly-' }
256  
257 #
258 # figure out what the js/css name is
259 #
260 if ( $self->{tinyMCEEnable} ) { $cacheFileName .= $self->{tinyMCEPath} . '-' }
261  
262 #
263 # add the jquery
264 #
265 my %jqueryHash = %{$self->{_jqueryHash}};
266 foreach my $jqueryLibrary ( sort {$jqueryHash{$a} <=> $jqueryHash{$b} } keys %jqueryHash ) {
267 $cacheFileName .= $jqueryLibrary . '-';
268 }
269  
270 #
271 # load js from elements and pages
272 #
273 my %jsHash = %{$self->{_jsHash}};
274 foreach my $jsFile ( sort {$jsHash{$a} <=> $jsHash{$b} } keys %jsHash ) {
275 $jsFile =~ s/\//-/sg;
276 $cacheFileName .= $jsFile . '-';
277 }
278  
279 #
280 # load js from elements and pages
281 #
282 my %cssHash = %{$self->{_cssHash}};
283 foreach my $cssFile ( sort {$cssHash{$a} <=> $cssHash{$b} } keys %cssHash ) {
284 $cssFile =~ s/\//-/sg;
285 $cacheFileName .= $cssFile . '-';
286 }
287  
288 #
289 # turn it into a unique short file
290 #
291 my $fileName = md5_hex( $cacheFileName );
292 my $cacheFile = $self->{filePath} . "/fws/cache/" . $fileName;
293 my $cacheWeb = $self->{fileFWSPath} . "/cache/" . $fileName;
294  
295 if ( $cacheFileName ) {
296 if ( !-e $cacheFile . ".js" ) {
297  
298 #
299 # make the dir if it doesn't exist
300 #
301 $self->makeDir( $self->{filePath} . "/fws/cache" );
302  
303 #
304 # open files to print to
305 #
306 open ( my $CSS, ">", $cacheFile . ".css" ) || $self->FWSLog( "Could not write to file: " . $cacheFile . ".css" );
307 open ( my $JS, ">", $cacheFile . ".js" ) || $self->FWSLog( "Could not write to file: " . $cacheFile . ".js" );
308  
309 print $JS "// FWS Generated JS Cache File - " . $self->formatDate( format => "dateTime" ) . "\n";
310 print $CSS "/* FWS Generated CSS Cache File - " . $self->formatDate( format => "dateTime" ) . " */\n";
311  
312 #
313 # if jquery is used or tiny mce we need these
314 #
315 if ( keys %jqueryHash || $self->{tinyMCEEnable} ) {
316 print $JS "var scriptName = \"" . $self->{scriptName} . "\";\n";
317 print $JS "var siteId = \"" . $self->{siteId} . "\";\n";
318 print $JS "var secureDomain = \"" . $self->{secureDomain} . "\";\n";
319 print $JS "var globalFiles = \"" . $self->{fileFWSPath} . "\";\n";
320 print $JS "var domain = \"" . $self->{domain} . "\";\n";
321  
322 my $fwsJS = $self->{filePath} . "/fws/fws-" . $self->{FWSVersion} . ".min.js";
323 if ( -e $fwsJS ) {
324 open ( my $FILE, "<", $fwsJS ) || $self->FWSLog( "Could not read file: " . $fwsJS );
325 print $JS "\n\n// fws-" . $self->{FWSVersion} . ".min.js\n\n";
326 while ( my $line = <$FILE> ) { print $JS $line }
327 close $FILE;
328 }
329  
330 my $fwsCSS = $self->{filePath} . "/fws/fws-" . $self->{FWSVersion} . ".css";
331 if ( -e $fwsCSS ) {
332 open ( my $FILE, "<", $fwsCSS ) || $self->FWSLog( "Could not read file: " . $fwsCSS );
333 print $CSS "\n\n/* fws-" . $self->{FWSVersion} . ".css */\n\n";
334 while ( my $line = <$FILE> ) { print $CSS $line }
335 close $FILE;
336 }
337 }
338  
339 if ( $self->{bootstrapEnable} ) {
340 my $bootstrapCSS = $self->{filePath} . "/fws/bootstrap-2.3.2/css/bootstrap.min.css";
341 if ( -e $bootstrapCSS ) {
342 open ( my $FILE, "<", $bootstrapCSS ) || $self->FWSLog( "Could not read file: " . $bootstrapCSS );
343 print $CSS "\n\n/* boostrap.min.css */\n\n";
344 while ( my $line = <$FILE> ) { print $CSS $line }
345 close $FILE;
346 }
347 }
348  
349  
350 #
351 # add tiny mce. lets do it first because it seems that if it loads after jquery dialog you might be able to dialog before you can render edit tools
352 #
353 if ( $self->{tinyMCEEnable} && !$paramHash{jqueryOnly} ) {
354 print $JS $self->tinyMCEHead();
355  
356 my $tinyMCEInit = $self->{filePath} . "/fws/" . $self->{tinyMCEPath} . "/tiny_mce_init.js";
357 if ( -e $tinyMCEInit ) {
358 open ( my $FILE, "<", $tinyMCEInit ) || $self->FWSLog( "Can not open file:" . $tinyMCEInit );
359 print $JS "\n\n// /fws/" . $self->{tinyMCEPath} . "/tiny_mce_init.js\n\n";
360 while ( my $line = <$FILE> ) { print $JS $line }
361 close $FILE;
362 }
363 }
364  
365 #
366 # all the jquery libraries
367 #
368 foreach my $jqueryLibrary (sort {$jqueryHash{$a} <=> $jqueryHash{$b} } keys %jqueryHash) {
369 my $fileName = $self->{filePath} . "/fws/jquery/jquery." . $jqueryLibrary . ".min.js";
370 if ( -e $fileName ) {
371 open ( my $FILE, "<", $fileName ) || $self->FWSLog( "Can not open file:" . $fileName );
372 print $JS "\n\n// jquery." . $jqueryLibrary . ".min.js\n\n";
373 while ( my $line = <$FILE> ) { print $JS $line }
374 close $FILE;
375 }
376  
377 $fileName = $self->{filePath} . "/fws/jquery/jquery." . $jqueryLibrary . ".css";
378 if ( -e $fileName ) {
379 open ( my $FILE, "<", $fileName ) || $self->FWSLog( "Can not open file:" . $fileName );
380 print $CSS "\n\n/* jquery." . $jqueryLibrary . ".css */\n\n";
381 while ( my $line = <$FILE> ) { print $CSS $line }
382 close $FILE;
383 }
384 }
385  
386 #
387 #
388 #
389 if ( !$paramHash{jqueryOnly} ) {
390 #
391 # load css from elements, pages and templates
392 #
393 my %cssHash = %{$self->{_cssHash}};
394 foreach my $fileName ( sort {$cssHash{$a} <=> $cssHash{$b} } keys %cssHash ) {
395
396 #
397 # If the full file is there, thats our favorite!
398 #
399 my $fullFileName = $self->{filePath} . "/" . $fileName . ".css";
400  
401 #
402 # fail over to no version name if its not there on the next version of FWS this will just only use non versioned one
403 #
404 if ( !-e $fullFileName ) {
405 $fullFileName =~ s/-[0-9\.]+.*/\.css/sg;
406 }
407  
408 if (-e $fullFileName) {
409 open ( my $FILE, "<", $fullFileName ) || $self->FWSLog( "Can not open file:" . $fullFileName );
410 print $CSS "\n\n/* " . $fileName . ".css */\n\n";
411 while ( my $line = <$FILE> ) { print $CSS $line }
412 close $FILE;
413 }
414 }
415  
416 #
417 # load js from elements and pages
418 #
419 my %jsHash = %{$self->{_jsHash}};
420 foreach my $fileName ( sort {$jsHash{$a} <=> $jsHash{$b} } keys %jsHash ) {
421
422 #
423 # full versioned file is our favorite, lets try that first
424 #
425 my $fullFileName = $self->{filePath} . "/" . $fileName . ".js";
426
427 #
428 # fail over to no version name if its not there on the next version of FWS this will just only use non versioned one
429 #
430 if ( !-e $fullFileName ) {
431 $fullFileName =~ s/-[0-9\.]+.*/\.js/sg;
432 }
433  
434 if (-e $fullFileName) {
435 open ( my $FILE, "<", $fullFileName ) || $self->FWSLog( "Can not open file:" . $fullFileName );
436 print $JS "\n\n// " . $fileName . ".js\n\n";
437 while ( my $line = <$FILE> ) { print $JS $line }
438 close $FILE;
439 }
440 }
441 }
442  
443 #
444 # close up shop, we done
445 #
446 close $CSS;
447 close $JS;
448 }
449
450 $pageFoot .= "<script type=\"text/javascript\" src=\"" . $cacheWeb . ".js\"></script>\n";
451  
452 if ( $self->{tinyMCEEnable} && !$paramHash{jqueryOnly} ) {
453 $pageFoot = "<script type=\"text/javascript\" src=\"" . $self->{fileFWSPath} . "/" . $self->{tinyMCEPath} . "/tiny_mce.js\"></script>\n" . $pageFoot;
454 }
455  
456  
457 #
458 # if we are flaged for boostrap, add it!
459 #
460 if ( $self->{bootstrapEnable} ) {
461 $pageFoot = "\n<script src=\"" . $self->{fileWebPath} . "/fws/bootstrap-2.3.2/js/bootstrap.min.js\"></script>\n" . $pageFoot;
462 }
463  
464  
465  
466 if ( keys %jqueryHash && !$self->{loadJQueryInHead} ) {
467 $pageFoot = "<script type=\"text/javascript\" src=\"" . $self->{fileFWSPath} . "/jquery/jquery-1.7.1.min.js\"></script>\n" . $pageFoot;
468 }
469
470 if ( !$paramHash{noCSS} ) { $pageHead .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . $cacheWeb . ".css\"/>\n" }
471 }
472  
473  
474 #
475 # set them to be used in templatese
476 #
477 $self->siteValue( 'pageHead', $pageHead . $self->siteValue( 'pageHead' ) );
478 $self->siteValue( 'pageFoot', $pageFoot . $self->siteValue( 'pageFoot' ) );
479  
480 return $pageHead . $pageFoot;
481 }
482  
483  
484 =head1 AUTHOR
485  
486 Nate Lewis, C<< <nlewis at gnetworks.com> >>
487  
488 =head1 BUGS
489  
490 Please report any bugs or feature requests to C<bug-fws-v2 at rt.cpan.org>, or through
491 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FWS-V2>. I will be notified, and then you'll
492 automatically be notified of progress on your bug as I make changes.
493  
494  
495  
496  
497 =head1 SUPPORT
498  
499 You can find documentation for this module with the perldoc command.
500  
501 perldoc FWS::V2::Cache
502  
503  
504 You can also look for information at:
505  
506 =over 4
507  
508 =item * RT: CPAN's request tracker (report bugs here)
509  
510 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FWS-V2>
511  
512 =item * AnnoCPAN: Annotated CPAN documentation
513  
514 L<http://annocpan.org/dist/FWS-V2>
515  
516 =item * CPAN Ratings
517  
518 L<http://cpanratings.perl.org/d/FWS-V2>
519  
520 =item * Search CPAN
521  
522 L<http://search.cpan.org/dist/FWS-V2/>
523  
524 =back
525  
526  
527 =head1 LICENSE AND COPYRIGHT
528  
529 Copyright 2013 Nate Lewis.
530  
531 This program is free software; you can redistribute it and/or modify it
532 under the terms of either: the GNU General Public License as published
533 by the Free Software Foundation; or the Artistic License.
534  
535 See http://dev.perl.org/licenses/ for more information.
536  
537  
538 =cut
539  
540 1; # End of FWS::V2::Cache