eZ Publish  [trunk]
ezsubtreecache.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZSubtreeCache 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 kernel
00009  */
00010 
00011 /*!
00012   \class eZSubtreeCache ezsubtreecache.php
00013   \brief The class eZSubtreeCache does
00014 
00015 */
00016 
00017 class eZSubtreeCache
00018 {
00019     /*!
00020      Constructor
00021     */
00022     function eZSubtreeCache()
00023     {
00024     }
00025 
00026     /*!
00027      \static
00028      Removes caches which were created using 'cache-block' operator with 'subtree_expiry' parameter.
00029      \a $nodeList is an array of node's ids. It is used to determine caches to remove.
00030      if $nodeList is not an array or if $nodeList is empty all 'subtree_expiry' caches will be removed.
00031     */
00032     static function cleanupByNodeIDs( &$nodeIDList )
00033     {
00034         if ( !is_array( $nodeIDList ) || count( $nodeIDList ) === 0 )
00035         {
00036             eZSubtreeCache::cleanupAll();
00037         }
00038         else
00039         {
00040             $nodeList = eZContentObjectTreeNode::fetch( $nodeIDList );
00041             if ( $nodeList )
00042             {
00043                 if ( !is_array( $nodeList ) )
00044                     $nodeList = array( $nodeList );
00045 
00046                 eZSubtreeCache::cleanup( $nodeList );
00047             }
00048         }
00049     }
00050 
00051     /*!
00052      \static
00053      Clears template block caches with 'subtree_ezpiry' parameter for nodes in the $nodeList.
00054      Note: if 'DelayedCacheBlockCleanup' setting is enabled then expiried caches will be renamed only
00055      (removing from disk should be made, for example, by cronjob).
00056     */
00057     static function cleanup( &$nodeList )
00058     {
00059         if ( !is_array( $nodeList ) )
00060             return;
00061 
00062         $cacheDir = eZTemplateCacheFunction::templateBlockCacheDir();
00063 
00064         foreach ( $nodeList as $node )
00065         {
00066             $pathString = $node->attribute( 'path_string' );
00067             $pathString = trim( $pathString, '/' );
00068             $nodeListID = explode( '/', $pathString );
00069 
00070             foreach( $nodeListID as $nodeID )
00071             {
00072                 $cachePath = $cacheDir . eZTemplateCacheFunction::subtreeCacheSubDirForNode( $nodeID );
00073                 eZSubtreeCache::cleanupCacheDir( $cachePath );
00074             }
00075         }
00076     }
00077 
00078     /*!
00079      \static
00080      Removes all caches which were created using 'cache-block' operator with 'subtree_expiry' parameter.
00081     */
00082     static function cleanupAll()
00083     {
00084         $subtreeCacheDir = eZTemplateCacheFunction::templateBlockCacheDir() . eZTemplateCacheFunction::subtreeCacheBaseSubDir();
00085         eZSubtreeCache::cleanupCacheDir( $subtreeCacheDir );
00086     }
00087 
00088     /*!
00089      \static
00090      If DelayedCacheBlockCleanup is enables just renames $cachDir, otherwise removes $cacheDir from disk.
00091     */
00092     static function cleanupCacheDir( $cacheDir )
00093     {
00094         $ini = eZINI::instance();
00095         if ( $ini->variable( 'TemplateSettings', 'DelayedCacheBlockCleanup' ) === 'enabled' )
00096         {
00097             eZSubtreeCache::renameDir( $cacheDir );
00098         }
00099         else
00100         {
00101             eZSubtreeCache::removeExpiryCacheFromDisk( $cacheDir );
00102         }
00103     }
00104 
00105     /*!
00106      \static
00107      $dir is a path to the cache directory which should be renamed.
00108      $dir is relative to the root directiry of 'subtree' cache.
00109     */
00110     static function renameDir( $dir )
00111     {
00112         // just rename. Actual removing will be performed by cronjob.
00113 
00114         // This directory renaming is only performed on the local filesystem
00115         // to ensure purging of really old data. If the DB file handler is in
00116         // use it will check the modified_subnode field of the tree structure
00117         // to determin expiry when the cache-block entry is accessed.
00118         if ( file_exists( $dir ) )
00119         {
00120             if ( is_dir( $dir ) )
00121             {
00122                 $expiryCacheDir = eZTemplateCacheFunction::expiryTemplateBlockCacheDir();
00123 
00124                 $uniqid = md5( uniqid( 'ezpsubtreecache'. getmypid(), true ) );
00125                 $expiryCacheDir .= '/' . $uniqid[0] . '/' . $uniqid[1] . '/' . $uniqid[2] . '/' . $uniqid;
00126 
00127                 if ( !file_exists( $expiryCacheDir ) )
00128                 {
00129                     eZDir::mkdir( $expiryCacheDir, false, true );
00130                 }
00131                 eZFile::rename( $dir, $expiryCacheDir, false, eZFile::APPEND_DEBUG_ON_FAILURE );
00132             }
00133             else
00134             {
00135                 eZDebug::writeWarning( "$dir should be a directory. Template-block caches for 'subtree_expiry' are not removed.", __METHOD__ );
00136             }
00137         }
00138     }
00139 
00140     /*!
00141      \static
00142     */
00143     static function removeAllExpiryCacheFromDisk()
00144     {
00145         $expiryCachePath = eZTemplateCacheFunction::expiryTemplateBlockCacheDir();
00146         eZSubtreeCache::removeExpiryCacheFromDisk( $expiryCachePath );
00147     }
00148 
00149     /*!
00150      \static
00151      $expiryCachePath is a path to directory with cache that should be removed
00152     */
00153     static function removeExpiryCacheFromDisk( $expiryCachePath )
00154     {
00155         $fileHandler = eZClusterFileHandler::instance();
00156         if ( $fileHandler instanceof eZFSFileHandler
00157              or
00158              $fileHandler instanceof eZFS2FileHandler )
00159         {
00160             // We will only delete files if the FS file handler is used,
00161             // if the DB file handler is in use the system will
00162             // instead use the modified_subnode field from the tree structure
00163             // in the database to determine if the cache is expired.
00164             // This reduces the need to perform expensive modifications to the
00165             // database entries for the cluster storage.
00166             $fileHandler->fileDelete( $expiryCachePath );
00167         }
00168     }
00169 }
00170 
00171 ?>