Permalink
Browse files

Merge branch 'wip-MDL-36363-m24' of https://github.com/samhemelryk/mo…

…odle into MOODLE_24_STABLE
  • Loading branch information...
2 parents 9f9bc1f + a50cdcc commit becce23e4aaa80866313bdbd1ef3465a8e65a475 @stronk7 stronk7 committed Jan 29, 2013
View
@@ -225,8 +225,37 @@ public static function initialise_test_instance(cache_definition $definition);
/**
* Performs any necessary clean up when the store instance is being deleted.
+ *
+ * @deprecated since 2.5
*/
- abstract public function cleanup();
+ public function cleanup() {
+ debugging('This function has been renamed to instance_deleted. Please update your calls.', DEBUG_DEVELOPER);
+ }
+
+ /**
+ * Performs any necessary operation when the store instance has been created.
+ *
+ * @since 2.5
+ */
+ public function instance_created() {
+ // By default, do nothing.
+ }
+
+ /**
+ * Performs any necessary operation when the store instance is being deleted.
+ *
+ * This method may be called before the store has been initialised.
+ *
+ * @since 2.5
+ * @see cleanup()
+ */
+ public function instance_deleted() {
+ if (method_exists($this, 'cleanup')) {
+ // There used to be a legacy function called cleanup, it was renamed to instance delete.
+ // To be removed in 2.6.
+ $this->cleanup();
+ }
+ }
/**
* Returns true if the user can add an instance of the store plugin.
View
@@ -178,6 +178,10 @@ public function add_store_instance($name, $plugin, array $configuration = array(
$this->configstores[$name]['lock'] = $configuration['lock'];
unset($this->configstores[$name]['configuration']['lock']);
}
+ // Call instance_created()
+ $store = new $class($name, $this->configstores[$name]['configuration']);
+ $store->instance_created();
+
$this->config_save();
return true;
}
@@ -304,6 +308,12 @@ public function delete_store_instance($name) {
throw new cache_exception('You cannot delete a cache store that has definition mappings.');
}
}
+
+ // Call instance_deleted()
+ $class = 'cachestore_'.$this->configstores[$name]['plugin'];
+ $store = new $class($name, $this->configstores[$name]['configuration']);
+ $store->instance_deleted();
+
unset($this->configstores[$name]);
$this->config_save();
return true;
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 used to store files for this store and the definition it was initialised with.
* @var string
*/
- protected $path = null;
+ protected $path = false;
+
+ /**
+ * The path in which definition specific sub directories will be created for caching.
+ * @var string
+ */
+ protected $filestorepath = false;
/**
* Set to true when a prescan has been performed.
@@ -129,7 +135,10 @@ 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 will be updated once the store has been initialised for a definition.
$this->path = $path;
+
// Check if we should prescan the directory.
if (array_key_exists('prescan', $configuration)) {
$this->prescan = (bool)$configuration['prescan'];
@@ -146,6 +155,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 +233,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 +504,39 @@ 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,14 +604,14 @@ 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);
+ public function instance_deleted() {
+ $this->purge_all_definitions();
+ @rmdir($this->filestorepath);
}
/**
@@ -319,8 +319,18 @@ public static function config_set_edit_form_data(moodleform $editform, array $co
/**
* Performs any necessary clean up when the store instance is being deleted.
*/
- public function cleanup() {
- $this->purge();
+ public function instance_deleted() {
+ if ($this->connection) {
+ $connection = $this->connection;
+ } else {
+ $connection = new Memcache;
+ foreach ($this->servers as $server) {
+ $connection->addServer($server[0], $server[1], true, $server[2]);
+ }
+ }
+ $connection->flush();
+ unset($connection);
+ unset($this->connection);
}
/**
@@ -401,8 +401,22 @@ public static function config_set_edit_form_data(moodleform $editform, array $co
/**
* Performs any necessary clean up when the store instance is being deleted.
*/
- public function cleanup() {
- $this->purge();
+ public function instance_deleted() {
+ if ($this->connection) {
+ $connection = $this->connection;
+ } else {
+ $connection = new Memcached(crc32($this->name));
+ $servers = $connection->getServerList();
+ if (empty($servers)) {
+ foreach ($this->options as $key => $value) {
+ $connection->setOption($key, $value);
+ }
+ $connection->addServers($this->servers);
+ }
+ }
+ $connection->flush();
+ unset($connection);
+ unset($this->connection);
}
/**
@@ -67,7 +67,7 @@ class cachestore_mongodb extends cache_store implements cache_is_configurable {
* The Connection object
* @var Mongo
*/
- protected $connection;
+ protected $connection = false;
/**
* The Database Object
@@ -439,8 +439,22 @@ public static function config_set_edit_form_data(moodleform $editform, array $co
/**
* Performs any necessary clean up when the store instance is being deleted.
*/
- public function cleanup() {
- $this->purge();
+ public function instance_deleted() {
+ // We can't use purge here that acts upon a collection.
+ // Instead we must drop the named database.
+ if ($this->connection) {
+ $connection = $this->connection;
+ } else {
+ $connection = new Mongo($this->server, $this->options);
+ }
+ $database = $connection->selectDB($this->databasename);
+ $database->drop();
+ $connection = null;
+ $database = null;
+ // Explicitly unset things to cause a close.
+ $this->collection = null;
+ $this->database = null;
+ $this->connection = null;
}
/**
@@ -375,7 +375,7 @@ public static function can_add_instance() {
/**
* Performs any necessary clean up when the store instance is being deleted.
*/
- public function cleanup() {
+ public function instance_deleted() {
$this->purge();
}
@@ -372,7 +372,7 @@ public static function can_add_instance() {
/**
* Performs any necessary clean up when the store instance is being deleted.
*/
- public function cleanup() {
+ public function instance_deleted() {
$this->purge();
}
@@ -100,7 +100,7 @@
}
$result[5] = sprintf('%01.4f', microtime(true) - $start);
$applicationtable->data[] = $result;
- $store->cleanup();
+ $store->instance_deleted();
}
}
@@ -136,7 +136,7 @@
}
$result[5] = sprintf('%01.4f', microtime(true) - $start);
$sessiontable->data[] = $result;
- $store->cleanup();
+ $store->instance_deleted();
}
}
@@ -172,7 +172,7 @@
}
$result[5] = sprintf('%01.4f', microtime(true) - $start);
$requesttable->data[] = $result;
- $store->cleanup();
+ $store->instance_deleted();
}
}
View
@@ -0,0 +1,8 @@
+This files describes API changes in /cache/stores/* - cache store plugins.
+Information provided here is intended especially for developers.
+
+=== 2.5 ===
+* cleanup method renamed to instance_deleted.
+ It is now called when the store is deleted as all comments suggested anyway.
+* instance_created method added.
+ It is called when the store is created for the very first time.

0 comments on commit becce23

Please sign in to comment.