MDL-36363 Adding/Removing a file store cache instance cleanups the folder too

This commit is contained in:
Matteo Scaramuccia 2012-12-02 11:40:35 +01:00 committed by Sam Hemelryk
parent 730cf5acbc
commit 56e65baa1e
2 changed files with 68 additions and 14 deletions

View File

@ -140,7 +140,9 @@ abstract class cache_store implements cache_store_interface {
*
* @link http://tracker.moodle.org/browse/MDL-36363
*/
public function instance_created() {}
public function instance_created() {
// By default, do nothing.
}
/**
* Returns the name of this store instance.
@ -235,7 +237,7 @@ abstract class cache_store implements cache_store_interface {
*
* Please note that if the store has been already initialised with
* a definition ({@link initialise()}), cleanup will be performed against the scope
* of that definition.
* of that definition i.e. the result depends on the specific store implementation.
*
* @see instance_deleted()
*/
@ -248,7 +250,9 @@ abstract class cache_store implements cache_store_interface {
* @link http://tracker.moodle.org/browse/MDL-36363
* @see cleanup()
*/
public function instance_deleted() {}
public function instance_deleted() {
// By default, do nothing
}
/**
* Returns true if the user can add an instance of the store plugin.

View File

@ -46,10 +46,16 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i
protected $name;
/**
* The path to use for the file storage.
* The path to use for the file storage. False otherwise.
* @var string
*/
protected $path = null;
protected $filestorepath = false;
/**
* The path to use when the store instance has been initialised. False otherwise.
* @var string
*/
protected $path = false;
/**
* Set to true when a prescan has been performed.
@ -129,7 +135,9 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i
$path = make_cache_directory('cachestore_file/'.preg_replace('#[^a-zA-Z0-9\.\-_]+#', '', $name));
}
$this->isready = $path !== false;
$this->filestorepath = $path;
$this->path = $path;
// Check if we should prescan the directory.
if (array_key_exists('prescan', $configuration)) {
$this->prescan = (bool)$configuration['prescan'];
@ -146,6 +154,16 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i
}
}
/**
* Performs any necessary operation when the file store instance has been created.
*/
public function instance_created() {
if ($this->isready && !$this->prescan) {
// It is supposed the store instance to expect an empty folder.
$this->purge_all_definitions();
}
}
/**
* Returns true if this store instance is ready to be used.
* @return bool
@ -214,7 +232,7 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i
public function initialise(cache_definition $definition) {
$this->definition = $definition;
$hash = preg_replace('#[^a-zA-Z0-9]+#', '_', $this->definition->get_id());
$this->path .= '/'.$hash;
$this->path = $this->filestorepath.'/'.$hash;
make_writable_directory($this->path);
if ($this->prescan && $definition->get_mode() !== self::MODE_REQUEST) {
$this->prescan = false;
@ -485,21 +503,40 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i
}
/**
* Purges the cache deleting all items within it.
* Purges the cache definition deleting all the items within it.
*
* @return boolean True on success. False otherwise.
*/
public function purge() {
$files = glob($this->glob_keys_pattern(), GLOB_MARK | GLOB_NOSORT);
if (is_array($files)) {
foreach ($files as $filename) {
@unlink($filename);
if ($this->isready) {
$files = glob($this->glob_keys_pattern(), GLOB_MARK | GLOB_NOSORT);
if (is_array($files)) {
foreach ($files as $filename) {
@unlink($filename);
}
}
$this->keys = array();
}
$this->keys = array();
return true;
}
/**
* Purges all the cache definitions deleting all items within them.
*
* @return boolean True on success. False otherwise.
*/
protected function purge_all_definitions() {
// Warning: limit the deletion to what file store is actually able
// to create using the internal {@link purge()} providing the
// {@link $path} with a wildcard to perform a purge action over all the definitions.
$currpath = $this->path;
$this->path = $this->filestorepath.'/*';
$result = $this->purge();
$this->path = $currpath;
return $result;
}
/**
* Given the data from the add instance form this function creates a configuration array.
*
@ -567,16 +604,29 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i
}
/**
* Performs any necessary clean up when the store instance is being deleted.
* Performs any necessary clean up when the file store instance is being deleted.
*
* 1. Purges the cache directory.
* 2. Deletes the directory we created for this cache instances data.
* 2. Deletes the directory we created for the given definition.
*/
public function cleanup() {
$this->purge();
@rmdir($this->path);
}
/**
* Performs any necessary operation when the file store instance is being deleted,
* regardless the file store being initialised with a definition ({@link initialise()}).
*
* @see cleanup()
*/
public function instance_deleted() {
if ($this->isready && !$this->prescan) {
// Remove the content related to this file store.
$this->purge_all_definitions();
}
}
/**
* Generates an instance of the cache store that can be used for testing.
*