|
eZ Publish
[trunk]
|
00001 <?php 00002 /** 00003 * File containing the eZTemplateTreeCache class. 00004 * 00005 * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved. 00006 * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 00007 * @version //autogentag// 00008 * @package lib 00009 */ 00010 00011 /*! 00012 \class eZTemplateTreeCache eztemplatetreecache.php 00013 \brief Cache handling for template tree nodes. 00014 00015 */ 00016 00017 class eZTemplateTreeCache 00018 { 00019 const CODE_DATE = 1044440833; 00020 00021 /*! 00022 \static 00023 \return the cache table which has cache keys and cache data. 00024 */ 00025 static function &cacheTable() 00026 { 00027 $templateCache =& $GLOBALS['eZTemplateTreeCacheTable']; 00028 if ( !is_array( $templateCache ) ) 00029 $templateCache = array(); 00030 return $templateCache; 00031 } 00032 00033 /*! 00034 \private 00035 \static 00036 \return a new key from \a $key which has some additional info. 00037 */ 00038 static function internalKey( $key ) 00039 { 00040 $ini = eZINI::instance(); 00041 $debug = $ini->variable( 'TemplateSettings', 'Debug' ) == 'enabled'; 00042 if ( $debug ) 00043 $key = $key . '-debug'; 00044 else 00045 $key = $key . '-clean'; 00046 return $key; 00047 } 00048 00049 /*! 00050 \static 00051 \return the cache node tree which is stored with the cache key \a $key. 00052 Returns \c null if no cache data was found. 00053 */ 00054 static function cachedTree( $key, $uri, $res, $templatePath, &$extraParameters ) 00055 { 00056 $templateCache =& eZTemplateTreeCache::cacheTable(); 00057 $key = eZTemplateTreeCache::internalKey( $key ); 00058 $root = null; 00059 if ( isset( $templateCache[$key] ) ) 00060 { 00061 $root =& $templateCache[$key]['root']; 00062 eZDebugSetting::writeDebug( 'eztemplate-tree-cache', "Cache hit for uri '$uri' with key '$key'", 'eZTemplateTreeCache::cachedTree' ); 00063 } 00064 else 00065 eZDebugSetting::writeDebug( 'eztemplate-tree-cache', "Cache miss for uri '$uri' with key '$key'", 'eZTemplateTreeCache::cachedTree' ); 00066 00067 return $root; 00068 } 00069 00070 /*! 00071 Sets the template tree node \a $root to be cached with the cache key $root. 00072 \note Trying to overwrite and existing cache key will give a warning and fail. 00073 */ 00074 static function setCachedTree( $originalKey, $uri, $res, $templatePath, &$extraParameters, &$root ) 00075 { 00076 if ( $root === null ) 00077 return; 00078 $templateCache =& eZTemplateTreeCache::cacheTable(); 00079 $key = eZTemplateTreeCache::internalKey( $originalKey ); 00080 if ( isset( $templateCache[$key] ) ) 00081 { 00082 eZDebug::writeDebug( "Template cache for key '$key', created from uri '$uri', already exists", __METHOD__ ); 00083 } 00084 else 00085 { 00086 $templateCache[$key] = array(); 00087 } 00088 $ini = eZINI::instance(); 00089 $debug = $ini->variable( 'TemplateSettings', 'Debug' ) == 'enabled'; 00090 $templateCache[$key]['root'] =& $root; 00091 $templateCache[$key]['info'] = array( 'original_key' => $originalKey, 00092 'key' => $key, 00093 'uri' => $uri, 00094 'debug' => $debug, 00095 'resource' => $res, 00096 'template_path' => $templatePath, 00097 'resource_parameters' => $extraParameters ); 00098 } 00099 00100 /*! 00101 \static 00102 \return true if template tree node caching is enabled. 00103 \note To change this setting edit settings/site.ini and locate the group TemplateSettings and the entry NodeTreeCaching. 00104 */ 00105 static function isCacheEnabled() 00106 { 00107 if ( isset( $GLOBALS['eZSiteBasics'] ) ) 00108 { 00109 $siteBasics = $GLOBALS['eZSiteBasics']; 00110 if ( isset( $siteBasics['no-cache-adviced'] ) and 00111 $siteBasics['no-cache-adviced'] ) 00112 { 00113 return false; 00114 } 00115 } 00116 00117 $ini = eZINI::instance(); 00118 $cacheEnabled = $ini->variable( 'TemplateSettings', 'NodeTreeCaching' ) == 'enabled'; 00119 return $cacheEnabled; 00120 } 00121 00122 /*! 00123 \static 00124 \return the cache directory for tree node cache files. 00125 */ 00126 static function cacheDirectory() 00127 { 00128 $cacheDirectory =& $GLOBALS['eZTemplateTreeCacheDirectory']; 00129 if ( !isset( $cacheDirectory ) ) 00130 { 00131 $cacheDirectory = eZDir::path( array( eZSys::cacheDirectory(), 'template/tree' ) ); 00132 } 00133 return $cacheDirectory; 00134 } 00135 00136 /*! 00137 Creates the name for the tree cache file and returns it. 00138 The name conists of the md5 of the key and charset with the original filename appended. 00139 */ 00140 static function treeCacheFilename( $key, $templateFilepath ) 00141 { 00142 $internalCharset = eZTextCodec::internalCharset(); 00143 $extraName = ''; 00144 if ( preg_match( "#^.+/(.*)\.tpl$#", $templateFilepath, $matches ) ) 00145 $extraName = '-' . $matches[1]; 00146 else if ( preg_match( "#^(.*)\.tpl$#", $templateFilepath, $matches ) ) 00147 $extraName = '-' . $matches[1]; 00148 $cacheFileKey = "$key-$internalCharset"; 00149 $cacheFileName = md5( $cacheFileKey ) . $extraName . '.php'; 00150 return $cacheFileName; 00151 } 00152 00153 /*! 00154 \static 00155 \return true if the cache with the key \a $key can be restored. 00156 A cache file is found restorable when it exists and has a timestamp 00157 higher or equal to \a $timestamp. 00158 */ 00159 static function canRestoreCache( $key, $timestamp, $templateFilepath ) 00160 { 00161 if ( !eZTemplateTreeCache::isCacheEnabled() ) 00162 return false; 00163 00164 $templateCache =& eZTemplateTreeCache::cacheTable(); 00165 $key = eZTemplateTreeCache::internalKey( $key ); 00166 if ( isset( $templateCache[$key] ) ) 00167 { 00168 return false; 00169 } 00170 $cacheFileName = eZTemplateTreeCache::treeCacheFilename( $key, $templateFilepath ); 00171 00172 $php = new eZPHPCreator( eZTemplateTreeCache::cacheDirectory(), $cacheFileName ); 00173 return $php->canRestore( $timestamp ); 00174 } 00175 00176 /*! 00177 \static 00178 Loads the cache with the key \a $key from a file and sets the result in the cache table. 00179 \return true if the cache was successfully restored. 00180 */ 00181 static function restoreCache( $key, $templateFilepath ) 00182 { 00183 if ( !eZTemplateTreeCache::isCacheEnabled() ) 00184 return false; 00185 00186 $templateCache =& eZTemplateTreeCache::cacheTable(); 00187 $key = eZTemplateTreeCache::internalKey( $key ); 00188 if ( isset( $templateCache[$key] ) ) 00189 { 00190 eZDebug::writeDebug( "Template cache for key '$key' already exist, cannot restore cache", __METHOD__ ); 00191 return false; 00192 } 00193 $cacheFileName = eZTemplateTreeCache::treeCacheFilename( $key, $templateFilepath ); 00194 00195 $php = new eZPHPCreator( eZTemplateTreeCache::cacheDirectory(), $cacheFileName ); 00196 $variables = $php->restore( array( 'info' => 'TemplateInfo', 00197 'root' => 'TemplateRoot', 00198 'cache-date' => 'eZTemplateTreeCacheCodeDate' ) ); 00199 if ( $variables['cache-date'] != eZTemplateTreeCache::CODE_DATE ) 00200 return false; 00201 $cache =& $templateCache[$key]; 00202 $cache['root'] =& $variables['root']; 00203 $cache['info'] =& $variables['info']; 00204 return true; 00205 } 00206 00207 /*! 00208 \static 00209 Stores the data of the cache with the key \a $key to a file. 00210 \return false if the cache does not exist. 00211 */ 00212 static function storeCache( $key, $templateFilepath ) 00213 { 00214 if ( !eZTemplateTreeCache::isCacheEnabled() ) 00215 return false; 00216 $templateCache =& eZTemplateTreeCache::cacheTable(); 00217 $key = eZTemplateTreeCache::internalKey( $key ); 00218 if ( !isset( $templateCache[$key] ) ) 00219 { 00220 eZDebug::writeDebug( "Template cache for key '$key' does not exist, cannot store cache", __METHOD__ ); 00221 return; 00222 } 00223 $cacheFileName = eZTemplateTreeCache::treeCacheFilename( $key, $templateFilepath ); 00224 00225 $cache =& $templateCache[$key]; 00226 00227 $php = new eZPHPCreator( eZTemplateTreeCache::cacheDirectory(), $cacheFileName ); 00228 $php->addVariable( 'eZTemplateTreeCacheCodeDate', eZTemplateTreeCache::CODE_DATE ); 00229 $php->addSpace(); 00230 $php->addVariable( 'TemplateInfo', $cache['info'] ); 00231 $php->addSpace(); 00232 $php->addVariable( 'TemplateRoot', $cache['root'] ); 00233 $php->store(); 00234 } 00235 } 00236 00237 ?>