Permalink
Browse files

MDL-36322 cache: implemented cache_store::create_clone

  • Loading branch information...
Sam Hemelryk
Sam Hemelryk committed Jan 30, 2013
1 parent 722907c commit 3fbba4bf48d1d225a3ca9bd5cadd0bb9878fe025
Showing with 26 additions and 2 deletions.
  1. +8 −2 cache/classes/factory.php
  2. +18 −0 cache/classes/store.php
View
@@ -263,7 +263,13 @@ public function create_store_from_config($name, array $details, cache_definition
if (!$store->is_ready() || !$store->is_supported_mode($definition->get_mode())) {
return false;
}
- $store = clone($this->stores[$name]);
+ // We always create a clone of the original store.
+ // If we were to clone a store that had already been initialised with a definition then
+ // we'd run into a myriad of issues.
+ // We use a method of the store to create a clone rather than just creating it ourselves
+ // so that if any store out there doesn't handle cloning they can override this method in
+ // order to address the issues.
+ $store = $this->stores[$name]->create_clone($details);
$store->initialise($definition);
return $store;
}
@@ -550,4 +556,4 @@ public static function disable_stores() {
$factory = self::instance();
$factory->set_state(self::STATE_STORES_DISABLED);
}
-}
+}
View
@@ -306,4 +306,22 @@ public function supports_multiple_identifiers() {
public function supports_native_ttl() {
return $this::get_supported_features() & self::SUPPORTS_NATIVE_TTL;
}
+
+ /**
+ * Creates a clone of this store instance ready to be initialised.
+ *
+ * This method is used so that a cache store needs only be constructed once.
+ * Future requests for an instance of the store will be given a cloned instance.
+ *
+ * If you are writing a cache store that isn't compatible with the clone operation
+ * you can override this method to handle any situations you want before cloning.
+ *
+ * @param array $details An array containing the details of the store from the cache config.
+ * @return cache_store
+ */
+ public function create_clone(array $details = array()) {
+ // By default we just run clone.
+ // Any stores that have an issue with this will need to override the create_clone method.
+ return clone($this);
+ }
}

0 comments on commit 3fbba4b

Please sign in to comment.