Task to reset the partial cache

Simple task to reset the partial cache

This has been bugging me for a long time - on some hosting accounts the partial cache files cannot be removed bij the user, because they are owned by the webserver. I've had situations where I couldn't even remove them from the control panel! So I tended to whip up stupid scripts to remove them. But now I've turned them into a simple SilverStripe task that can forever live in mysite/code. Feel free to extend it as you see fit. It's all about the principle... 

The basic setup:

This task wil remove all partial cache files, and optionally remove the /silverstripe-cache/cache/ directory where they live. So it expects you to use the silverstipe-cache directory and not depend on the tmp directory, whereven that lives.

/mysite/code/ResetPartialCacheTask.php

<?php
class ResetPartialCacheTask extends BuildTask {
 
	protected $title = 'Reset partial cache';

	protected $description = 'This class will reset (empty) the partial cache folder'.
	                         'and optionally remove the folder itself as well';
 
	protected $enabled = true;


	/**
	  *   Provide basic instructions
	  */
	function run($request) {
		echo "<br />READY<br />";
	}

    
	/**
	  *   Provide basic instructions
	  */
	protected function showHelp() {

	}
}

Check out http://mydomain/dev/tasks You might have to do a ?flush=1, but after that you'll see a list of available development tasks, and it should have found this task as well, showing you the title and desction. If you run it, it will say READY and that's it.

Admin only...

Now we need to set some things straight. First of all, only the Administrator should be able to execute this task. Further more I don't want the task to start executing right away - you'll have to add a parameter first. If you don't, you'll be presented with a help screen that tells you what to do. The idea is that:

?rmdir=0 remove the partial cache files, but leave the cache directory
?rmdir=1 remove the partial cache and the cache directory as well
	function run($request) {

		// need Admin permission
		// todo: point to login? No, don't wake sleeping dogs
		if (!Permission::check("ADMIN")) {
			echo "You need Administrator rights to perform this task!";
			return;
		}


		// Need to add rmdir=0 or rmdir=1 (as an extra check)
		if(!isset($_GET['rmdir'])){
			$this->showHelp();
			return;
		}

		// start executing the task...
	}

Show help

This is a simple function that will display some hints:

	/**
	  *   Provide basic instructions if no task parameters 
	  *   are given in the querystring
	  */
	protected function showHelp() {

		$help = <<<EOD
<h2>Note:</h2>
<p>
This task will reset (empty) the partial cache or remove the partial cache <br />
directory completely - depending on the setting of rmdir parameter. It expects <br />
the partial cache to live in the /silverstripe-cache/cache/ directory.
</p>
<p>
This task is provided by the author 'as is', there are no guaranties as to its <br />
functioning properly, and the author will not accept any responsibility for <br />
any damages resulting from executing this task.
</p>
<h2>Reset the cache:</h2>
<p>
add ?rmdir=0 to the URL
</p>
<h2>Completely remove the cache directory:</h2>
<p>
add ?rmdir=1 to the URL
</p>

EOD;
		echo $help;
	}

Finally the task...

So far this really is a skeleton that can be used for any kind of task, but now let's start execuing it. Put the following code to the end of the run() function:

		// find the default partial cache folder
		$cacheFolder = Director::baseFolder() . '/silverstripe-cache/cache/';

		if (!is_dir($cacheFolder)) {
			echo "Couldn't find the partial cache in {$cacheFolder}";
			return;
		}

		// unlink all files in the SilverStripe cache directory
		$files = glob($cacheFolder . '*');

		if (!empty($files)) {

			foreach($files as $file) {
				echo "unlinking: $file<br />";
				unlink($file);
			}
		}
		else {
		  echo "No partial cache files found.<br />";
		}


		// permanently remove the partialcache directory
		if ($_GET['rmdir']) {
			echo "<br />Removing directory /silverstripe-cache/cache/<br />";
			rmdir($cacheFolder);
		}

		echo "<br />READY<br />";

SilverStripe 3.x update

For SilverStripe 3 the temp directory is slightly different, as there is an extra subdirectory named after the current user. I'm not sure how this would help in a shared hosting setup that uses one tmp directory for all, because for my hosting provider that user would always be apache but as I always add the silverstripe-cache directory to the site root, it doesn't really matter. The directory to find: /silverstripe-cache/some-user/cache/.

	// find the default partial cache folder
		$cacheFolder = getTempFolder() . '/cache/';

Also there are other cached files besides the partial cache files for the frontent in that directory. The partially cached files seem to all have names that start with zend_cache---cacheblock, so instead of removing all, you could:

		foreach($files as $file) {
			if (stristr($file, 'zend_cache---cacheblock')) {
				echo "unlinking: $file<br />";
				unlink($file);
			}
		}

That is basically it. Of course the server needs to support the unlink and rmdir functions. If it doesn't you're back to square one... 

Comments

Het versturen van reacties is uitgeschakeld.

RSS feed voor reacties op deze pagina