eZ Publish  [4.0]
ezsubtreecache.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZSubtreeCache class
00004 //
00005 // Created on: <21-Mar-2005 16:53:41 dl>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezsubtreecache.php
00032 */
00033 
00034 /*!
00035   \class eZSubtreeCache ezsubtreecache.php
00036   \brief The class eZSubtreeCache does
00037 
00038 */
00039 
00040 //include_once( 'lib/eztemplate/classes/eztemplatecachefunction.php' );
00041 
00042 class eZSubtreeCache
00043 {
00044     /*!
00045      Constructor
00046     */
00047     function eZSubtreeCache()
00048     {
00049     }
00050 
00051     /*!
00052      \static
00053      Removes caches which were created using 'cache-block' operator with 'subtree_expiry' parameter.
00054      \a $nodeList is an array of node's ids. It is used to determine caches to remove.
00055      if $nodeList is not an array or if $nodeList is empty all 'subtree_expiry' caches will be removed.
00056     */
00057     static function cleanupByNodeIDs( &$nodeIDList )
00058     {
00059         if ( !is_array( $nodeIDList ) || count( $nodeIDList ) === 0 )
00060         {
00061             eZSubtreeCache::cleanupAll();
00062         }
00063         else
00064         {
00065             //include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
00066             $nodeList = eZContentObjectTreeNode::fetch( $nodeIDList );
00067             if ( $nodeList )
00068             {
00069                 if ( !is_array( $nodeList ) )
00070                     $nodeList = array( $nodeList );
00071 
00072                 eZSubtreeCache::cleanup( $nodeList );
00073             }
00074         }
00075     }
00076 
00077     /*!
00078      \static
00079      Clears template block caches with 'subtree_ezpiry' parameter for nodes in the $nodeList.
00080      Note: if 'DelayedCacheBlockCleanup' setting is enabled then expiried caches will be renamed only
00081      (removing from disk should be made, for example, by cronjob).
00082     */
00083     static function cleanup( &$nodeList )
00084     {
00085         if ( !is_array( $nodeList ) )
00086             return;
00087 
00088         $cacheDir = eZTemplateCacheFunction::templateBlockCacheDir();
00089 
00090         foreach ( $nodeList as $node )
00091         {
00092             $pathString = $node->attribute( 'path_string' );
00093             $pathString = trim( $pathString, '/' );
00094             $nodeListID = explode( '/', $pathString );
00095 
00096             foreach( $nodeListID as $nodeID )
00097             {
00098                 $cachePath = $cacheDir . eZTemplateCacheFunction::subtreeCacheSubDirForNode( $nodeID );
00099                 eZSubtreeCache::cleanupCacheDir( $cachePath );
00100             }
00101         }
00102     }
00103 
00104     /*!
00105      \static
00106      Removes all caches which were created using 'cache-block' operator with 'subtree_expiry' parameter.
00107     */
00108     static function cleanupAll()
00109     {
00110         $subtreeCacheDir = eZTemplateCacheFunction::templateBlockCacheDir() . eZTemplateCacheFunction::subtreeCacheBaseSubDir();
00111         eZSubtreeCache::cleanupCacheDir( $subtreeCacheDir );
00112     }
00113 
00114     /*!
00115      \static
00116      If DelayedCacheBlockCleanup is enables just renames $cachDir, otherwise removes $cacheDir from disk.
00117     */
00118     static function cleanupCacheDir( $cacheDir )
00119     {
00120         if ( file_exists( $cacheDir ) )
00121         {
00122             //include_once( 'lib/ezutils/classes/ezini.php' );
00123             $ini = eZINI::instance();
00124             if ( $ini->variable( 'TemplateSettings', 'DelayedCacheBlockCleanup' ) === 'enabled' )
00125             {
00126                 eZSubtreeCache::renameDir( $cacheDir );
00127             }
00128             else
00129             {
00130                 eZSubtreeCache::removeExpiryCacheFromDisk( $cacheDir );
00131             }
00132         }
00133     }
00134 
00135     /*!
00136      \static
00137      $dir is a path to the cache directory which should be renamed.
00138      $dir is relative to the root directiry of 'subtree' cache.
00139     */
00140     static function renameDir( $dir )
00141     {
00142         // just rename. Actual removing will be performed by cronjob.
00143 
00144         // This directory renaming is only performed on the local filesystem
00145         // to ensure purging of really old data. If the DB file handler is in
00146         // use it will check the modified_subnode field of the tree structure
00147         // to determine expiry when the cache-block entry is accessed.
00148         if ( file_exists( $dir ) )
00149         {
00150             if ( is_dir( $dir ) )
00151             {
00152                 $expiryCacheDir = eZTemplateCacheFunction::expiryTemplateBlockCacheDir();
00153 
00154                 $uniqid = md5( uniqid( 'ezpsubtreecache'. getmypid(), true ) );
00155                 $expiryCacheDir .= '/' . $uniqid[0] . '/' . $uniqid[1] . '/' . $uniqid[2] . '/' . $uniqid;
00156 
00157                 if ( !file_exists( $expiryCacheDir ) )
00158                 {
00159                     eZDir::mkdir( $expiryCacheDir, false, true );
00160                 }
00161                 eZFile::rename( $dir, $expiryCacheDir );
00162             }
00163             else
00164             {
00165                 eZDebug::writeWarning( "$dir should be a directory. Template-block caches for 'subtree_expiry' are not removed.", "eZSubtreeCache::renameDir" );
00166             }
00167         }
00168     }
00169 
00170     /*!
00171      \static
00172     */
00173     static function removeAllExpiryCacheFromDisk()
00174     {
00175         $expiryCachePath = eZTemplateCacheFunction::expiryTemplateBlockCacheDir();
00176         eZSubtreeCache::removeExpiryCacheFromDisk( $expiryCachePath );
00177     }
00178 
00179     /*!
00180      \static
00181      $expiryCachePath is a path to directory with cache that should be removed
00182     */
00183     static function removeExpiryCacheFromDisk( $expiryCachePath )
00184     {
00185         require_once( 'kernel/classes/ezclusterfilehandler.php' );
00186         $fileHandler = eZClusterFileHandler::instance();
00187         if ( $fileHandler instanceof eZFSFileHandler )
00188         {
00189             // We will only delete files if the FS file handler is used,
00190             // if the DB file handler is in use the system will
00191             // instead use the modified_subnode field from the tree structure
00192             // in the database to determine if the cache is expired.
00193             // This reduces the need to perform expensive modifications to the
00194             // database entries for the cluster storage.
00195             $fileHandler->fileDelete( $expiryCachePath );
00196         }
00197     }
00198 }
00199 
00200 ?>