I have modules that modify [tt]modx_site_content[/tt] and thus I use the cache_sync.class.processor.php class to clear the cache. For some reason it was causing problems and hanging script execution. So I rewrote some of the code that was causing the problem, here’s the patch:
Index: manager/processors/cache_sync.class.processor.php
===================================================================
--- manager/processors/cache_sync.class.processor.php (revision 2493)
+++ manager/processors/cache_sync.class.processor.php (working copy)
@@ -46,26 +46,42 @@
}
$filesincache = 0;
$deletedfilesincache = 0;
- if ($handle = opendir($this->cachePath)) {
- // Initialize deleted per round counter
- $deletedThisRound = 1;
- while ($deletedThisRound){
- if(!$handle) $handle = opendir($this->cachePath);
- $deletedThisRound = 0;
- while (false !== ($file = readdir($handle))) {
- if ($file != "." && $file != "..") {
- $filesincache += 1;
- if ( preg_match("/\.pageCache/", $file) && (!is_array($deletedfiles) || !array_search($file,$deletedfiles)) ) {
- $deletedfilesincache += 1;
- $deletedThisRound++;
- $deletedfiles[] = $file;
- unlink($this->cachePath.$file);
- } // End if
- } // End if
- } // End while
- closedir($handle);
- $handle = '';
- } // End while ($deletedThisRound)
+ if (version_compare(phpversion(), '4.3.0') >= 0) {
+ // New and improved!
+ $files = glob(realpath($this->cachePath).'/*');
+ $filesincache = count($files);
+ $deletedfiles = array();
+ while ($file = array_shift($files)) {
+ $name = basename($file);
+ if (preg_match('/\.pageCache/',$name) && !in_array($name, $deletedfiles)) {
+ $deletedfilesincache++;
+ $deletedfiles[] = $name;
+ unlink($file);
+ }
+ }
+ } else {
+ // Old way of doing it (versions of php < 4.3.0)
+ if ($handle = opendir($this->cachePath)) {
+ // Initialize deleted per round counter
+ $deletedThisRound = 1;
+ while ($deletedThisRound){
+ if(!$handle) $handle = opendir($this->cachePath);
+ $deletedThisRound = 0;
+ while (false !== ($file = readdir($handle))) {
+ if ($file != "." && $file != "..") {
+ $filesincache += 1;
+ if ( preg_match("/\.pageCache/", $file) && (!is_array($deletedfiles) || !array_search($file,$deletedfiles)) ) {
+ $deletedfilesincache += 1;
+ $deletedThisRound++;
+ $deletedfiles[] = $file;
+ unlink($this->cachePath.$file);
+ } // End if
+ } // End if
+ } // End while
+ closedir($handle);
+ $handle = '';
+ } // End while ($deletedThisRound)
+ }
}
/****************************************************************************/
The new code first checks the version of PHP, if it doesn’t meet requirements, the script uses the old code.
Does anyone have any objections to this being commited to the 095dev branch? If not I will commit the changes tomorrow.