Files
@ 654a62c4f366
Branch filter:
Location: hot67beta/libraries/pattemplate/patTemplate/TemplateCache/File.php
654a62c4f366
2.7 KiB
text/x-php
menubar 11 to 30 and revert
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | <?PHP
/**
* patTemplate Template cache that stores data on filesystem
*
* $Id: File.php 10381 2008-06-01 03:35:53Z pasamio $
*
* @package patTemplate
* @subpackage Caches
* @author Stephan Schmidt <schst@php.net>
*/
// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();
/**
* patTemplate Template cache that stores data on filesystem
*
* $Id: File.php 10381 2008-06-01 03:35:53Z pasamio $
*
* Possible parameters for the cache are:
* - cacheFolder : set the folder from which to load the cache
* - lifetime : seconds for which the cache is valid, if set to auto, it will check
* whether the cache is older than the original file (if the reader supports this)
* - prefix for the filenames
*
* @package patTemplate
* @subpackage Caches
* @author Stephan Schmidt <schst@php.net>
*/
class patTemplate_TemplateCache_File extends patTemplate_TemplateCache
{
/**
* parameters of the cache
*
* @access private
* @var array
*/
var $_params = array(
'cacheFolder' => './cache',
'lifetime' => 'auto',
'prefix' => '',
'filemode' => null
);
/**
* load template from cache
*
* @access public
* @param string cache key
* @param integer modification time of original template
* @return array|boolean either an array containing the templates or false cache could not be loaded
*/
function load($key, $modTime = -1)
{
$filename = $this->_getCachefileName( $key );
if (!file_exists($filename) || !is_readable($filename)) {
return false;
}
$generatedOn = filemtime( $filename );
$ttl = $this->getParam('lifetime');
if ($ttl == 'auto') {
if ($modTime < 1) {
return false;
}
if ($modTime > $generatedOn) {
return false;
}
return unserialize(file_get_contents($filename));
} elseif(is_int( $ttl)) {
if ($generatedOn + $ttl < time()) {
return false;
}
return unserialize( file_get_contents( $filename ) );
}
return false;
}
/**
* write template to cache
*
* @access public
* @param string cache key
* @param array templates to store
* @return boolean true on success
*/
function write( $key, $templates )
{
$cacheFile = $this->_getCachefileName($key);
$fp = @fopen($cacheFile, 'w');
if (!$fp) {
return false;
}
flock( $fp, LOCK_EX );
fputs( $fp, serialize($templates));
flock( $fp, LOCK_UN );
$filemode = $this->getParam('filemode');
if ($filemode !== null) {
chmod($cacheFile, $filemode);
}
return true;
}
/**
* get the cache filename
*
* @access private
* @param string cache key
* @return string cache file name
*/
function _getCachefileName( $key )
{
return $this->getParam( 'cacheFolder' ) . '/' . $this->getParam( 'prefix' ).$key . '.cache';
}
}
?>
|