We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27376
    • 576 Posts
    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.
      • 23491 ☆ A M B ☆
      • 1,056 Posts
      Nicely done! I dig the glob usage and backwards compatibility. Thumbs up from me. smiley
        Mike Reid - www.pixelchutes.com
        MODx Ambassador / Contributor
        [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
        ________________________________
        Where every pixel matters.
        • 22303 MODX Staff
        • 10,725 Posts
        Can you briefly describe the differences between the methods and why the original was causing problems for you?
          • 23491 ☆ A M B ☆
          • 1,056 Posts
          I wonder if it had anything to do with the use of !array_search

          http://www.php.net/array_search


          array_search — Searches the array for a given value and returns the corresponding key if successful

          ...so what would happen if it found the file, but the array index was 0? That would satisfy the !array_search clause, but not the way I believe it was intended... ??
            Mike Reid - www.pixelchutes.com
            MODx Ambassador / Contributor
            [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
            ________________________________
            Where every pixel matters.
            • 27376
            • 576 Posts
            I have not the slightest idea as to why MY execution of the emptyCache function caused the endless loop and MODx’s execution of it didn’t. I first thought it was a relative path issue: my script runs inside [tt]assets/modules/jobadmin/[/tt] but then I fed an absolute path and came out with the same results...

            I couldn’t think too much about it because I needed a quick solution so I just rewrote the problem section. Then later on made it backwards compatible smiley

            The main difference is the new code uses a function introduced in PHP 4.3.0 called [tt]glob()[/tt] which is designed to grab and return an array of files/directories from a specified folder. An ideal solution I thought. Though I haven’t benchmarked the old and new methods to see which is faster...

            On a side note: The new method also does the same job in half the lines laugh

            php.net/glob - documentation
              • 27376
              • 576 Posts
              Commited to 095dev branch: Revision 2520