Permalink
Browse files

MDL-36363 Adding/Removing a file store cache instance cleanups the fo…

…lder too
  • Loading branch information...
1 parent 730cf5a commit 56e65baa1e537532491c50454347835f60a5c8b1 @scara scara committed with Sam Hemelryk Dec 2, 2012
Showing with 68 additions and 14 deletions.
  1. +7 −3 cache/classes/store.php
  2. +61 −11 cache/stores/file/lib.php
View
@@ -140,7 +140,9 @@ public static function initialise_test_instance(cache_definition $definition);
*
* @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 @@ public function instance_created() {}
*
* 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 @@ public function instance_created() {}
* @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
@@ -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 @@ public function __construct($name, array $configuration = array()) {
$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 @@ public function __construct($name, array $configuration = array()) {
}
}
+ /**
+ * 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 @@ public static function is_supported_mode($mode) {
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 @@ public function has_any(array $keys) {
}
/**
- * 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 @@ protected function ensure_path_exists() {
}
/**
- * 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.
*

0 comments on commit 56e65ba

Please sign in to comment.