PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/kernel/classes/ezsubtreecache.php

https://github.com/zerustech/ezpublish
PHP | 159 lines | 88 code | 15 blank | 56 comment | 11 complexity | 4d3adbbb193d7493dda515910870c29a MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the eZSubtreeCache class.
  4. *
  5. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  6. * @license For full copyright and license information view LICENSE file distributed with this source code.
  7. * @version //autogentag//
  8. * @package kernel
  9. */
  10. /*!
  11. \class eZSubtreeCache ezsubtreecache.php
  12. \brief The class eZSubtreeCache does
  13. */
  14. class eZSubtreeCache
  15. {
  16. /*!
  17. \static
  18. Removes caches which were created using 'cache-block' operator with 'subtree_expiry' parameter.
  19. \a $nodeList is an array of node's ids. It is used to determine caches to remove.
  20. if $nodeList is not an array or if $nodeList is empty all 'subtree_expiry' caches will be removed.
  21. */
  22. static function cleanupByNodeIDs( &$nodeIDList )
  23. {
  24. if ( !is_array( $nodeIDList ) || count( $nodeIDList ) === 0 )
  25. {
  26. eZSubtreeCache::cleanupAll();
  27. }
  28. else
  29. {
  30. $nodeList = eZContentObjectTreeNode::fetch( $nodeIDList );
  31. if ( $nodeList )
  32. {
  33. if ( !is_array( $nodeList ) )
  34. $nodeList = array( $nodeList );
  35. eZSubtreeCache::cleanup( $nodeList );
  36. }
  37. }
  38. }
  39. /*!
  40. \static
  41. Clears template block caches with 'subtree_ezpiry' parameter for nodes in the $nodeList.
  42. Note: if 'DelayedCacheBlockCleanup' setting is enabled then expiried caches will be renamed only
  43. (removing from disk should be made, for example, by cronjob).
  44. */
  45. static function cleanup( &$nodeList )
  46. {
  47. if ( !is_array( $nodeList ) )
  48. return;
  49. $cacheDir = eZTemplateCacheBlock::templateBlockCacheDir();
  50. foreach ( $nodeList as $node )
  51. {
  52. $pathString = $node->attribute( 'path_string' );
  53. $pathString = trim( $pathString, '/' );
  54. $nodeListID = explode( '/', $pathString );
  55. foreach( $nodeListID as $nodeID )
  56. {
  57. $cachePath = $cacheDir . eZTemplateCacheBlock::subtreeCacheSubDirForNode( $nodeID );
  58. eZSubtreeCache::cleanupCacheDir( $cachePath );
  59. }
  60. }
  61. }
  62. /*!
  63. \static
  64. Removes all caches which were created using 'cache-block' operator with 'subtree_expiry' parameter.
  65. */
  66. static function cleanupAll()
  67. {
  68. $subtreeCacheDir = eZTemplateCacheBlock::templateBlockCacheDir() . eZTemplateCacheBlock::subtreeCacheBaseSubDir();
  69. eZSubtreeCache::cleanupCacheDir( $subtreeCacheDir );
  70. }
  71. /*!
  72. \static
  73. If DelayedCacheBlockCleanup is enables just renames $cachDir, otherwise removes $cacheDir from disk.
  74. */
  75. static function cleanupCacheDir( $cacheDir )
  76. {
  77. $ini = eZINI::instance();
  78. if ( $ini->variable( 'TemplateSettings', 'DelayedCacheBlockCleanup' ) === 'enabled' )
  79. {
  80. eZSubtreeCache::renameDir( $cacheDir );
  81. }
  82. else
  83. {
  84. eZSubtreeCache::removeExpiryCacheFromDisk( $cacheDir );
  85. }
  86. }
  87. /*!
  88. \static
  89. $dir is a path to the cache directory which should be renamed.
  90. $dir is relative to the root directiry of 'subtree' cache.
  91. */
  92. static function renameDir( $dir )
  93. {
  94. // just rename. Actual removing will be performed by cronjob.
  95. // This directory renaming is only performed on the local filesystem
  96. // to ensure purging of really old data. If the DB file handler is in
  97. // use it will check the modified_subnode field of the tree structure
  98. // to determin expiry when the cache-block entry is accessed.
  99. if ( file_exists( $dir ) )
  100. {
  101. if ( is_dir( $dir ) )
  102. {
  103. $uniqid = md5( uniqid( 'ezpsubtreecache'. getmypid(), true ) );
  104. $expiryCacheDir = eZSys::cacheDirectory() . '/template-block-expiry/' . $uniqid[0] . '/' . $uniqid[1] . '/' . $uniqid[2] . '/' . $uniqid;
  105. if ( !file_exists( $expiryCacheDir ) )
  106. {
  107. eZDir::mkdir( $expiryCacheDir, false, true );
  108. }
  109. eZFile::rename( $dir, $expiryCacheDir, false, eZFile::APPEND_DEBUG_ON_FAILURE );
  110. }
  111. else
  112. {
  113. eZDebug::writeWarning( "$dir should be a directory. Template-block caches for 'subtree_expiry' are not removed.", __METHOD__ );
  114. }
  115. }
  116. }
  117. /*!
  118. \static
  119. */
  120. static function removeAllExpiryCacheFromDisk()
  121. {
  122. eZSubtreeCache::removeExpiryCacheFromDisk( eZSys::cacheDirectory() . '/template-block-expiry' );
  123. }
  124. /*!
  125. \static
  126. $expiryCachePath is a path to directory with cache that should be removed
  127. */
  128. static function removeExpiryCacheFromDisk( $expiryCachePath )
  129. {
  130. $fileHandler = eZClusterFileHandler::instance();
  131. if ( $fileHandler instanceof eZFSFileHandler )
  132. {
  133. // We will only delete files if the FS file handler is used,
  134. // if the DB file handler is in use the system will
  135. // instead use the modified_subnode field from the tree structure
  136. // in the database to determine if the cache is expired.
  137. // This reduces the need to perform expensive modifications to the
  138. // database entries for the cluster storage.
  139. $fileHandler->fileDelete( $expiryCachePath );
  140. }
  141. }
  142. }
  143. ?>