Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-37683 cache: improved indentifier collection and storage

  • Loading branch information...
commit 7634410b6eaa23fd5e88dbf11f71dca30d5b3818 1 parent e0d9b7c
@samhemelryk samhemelryk authored
View
28 cache/classes/definition.php
@@ -275,12 +275,6 @@ class cache_definition {
protected $definitionhash = null;
/**
- * An identifier to make cache keys predictably unique.
- * @var string
- */
- protected $cacheidentifier = '0';
-
- /**
* Creates a cache definition given a definition from the cache configuration or from a caches.php file.
*
* @param string $id
@@ -681,15 +675,6 @@ public function set_identifiers(array $identifiers = array()) {
}
/**
- * Sets an identifier for the cache.
- * This can be used
- * @param string $identifier
- */
- public function set_cache_identifier($identifier) {
- $this->cacheidentifier = (string)$identifier;
- }
-
- /**
* Returns the requirements of this definition as a binary flag.
* @return int
*/
@@ -739,7 +724,7 @@ public function generate_definition_hash() {
public function generate_single_key_prefix() {
if ($this->keyprefixsingle === null) {
$this->keyprefixsingle = $this->mode.'/'.$this->component.'/'.$this->area;
- $this->keyprefixsingle .= '/'.$this->cacheidentifier;
+ $this->keyprefixsingle .= '/'.$this->get_cache_identifier();
$identifiers = $this->get_identifiers();
if ($identifiers) {
foreach ($identifiers as $key => $value) {
@@ -762,7 +747,7 @@ public function generate_multi_key_parts() {
'mode' => $this->mode,
'component' => $this->component,
'area' => $this->area,
- 'siteidentifier' => $this->cacheidentifier
+ 'siteidentifier' => $this->get_cache_identifier()
);
if (!empty($this->identifiers)) {
$identifiers = array();
@@ -802,4 +787,13 @@ public function has_invalidation_events() {
public function get_invalidation_events() {
return $this->invalidationevents;
}
+
+ /**
+ * Returns a cache identification string.
+ *
+ * @return string A string to be used as part of keys.
+ */
+ protected function get_cache_identifier() {
+ return cache_helper::get_site_identifier();
+ }
}
View
2  cache/classes/factory.php
@@ -204,7 +204,6 @@ public function create_cache_from_params($mode, $component, $area, array $identi
// Get the class. Note this is a late static binding so we need to use get_called_class.
$definition = cache_definition::load_adhoc($mode, $component, $area, $options);
$config = $this->create_config_instance();
- $definition->set_cache_identifier($config->get_site_identifier());
$definition->set_identifiers($identifiers);
$cache = $this->create_cache($definition, $identifiers);
if ($definition->should_be_persistent()) {
@@ -392,7 +391,6 @@ public function create_definition($component, $area, $aggregate = null) {
} else {
$definition = cache_definition::load($id, $definition, $aggregate);
}
- $definition->set_cache_identifier($instance->get_site_identifier());
}
$this->definitions[$id] = $definition;
}
View
33 cache/classes/helper.php
@@ -55,6 +55,13 @@ class cache_helper {
protected static $instance;
/**
+ * The site identifier used by the cache.
+ * Set the first time get_site_identifier is called.
+ * @var string
+ */
+ protected static $siteidentifier = null;
+
+ /**
* Returns true if the cache API can be initialised before Moodle has finished initialising itself.
*
* This check is essential when trying to cache the likes of configuration information. It checks to make sure that the cache
@@ -234,7 +241,6 @@ public static function invalidate_by_event($event, array $keys) {
$factory = cache_factory::instance();
foreach ($instance->get_definitions() as $name => $definitionarr) {
$definition = cache_definition::load($name, $definitionarr);
- $definition->set_cache_identifier($instance->get_site_identifier());
if ($definition->invalidates_on_event($event)) {
// OK at this point we know that the definition has information to invalidate on the event.
// There are two routes, either its an application cache in which case we can invalidate it now.
@@ -305,7 +311,6 @@ public static function purge_by_event($event) {
$factory = cache_factory::instance();
foreach ($instance->get_definitions() as $name => $definitionarr) {
$definition = cache_definition::load($name, $definitionarr);
- $definition->set_cache_identifier($instance->get_site_identifier());
if ($definition->invalidates_on_event($event)) {
// Create the cache.
$cache = $factory->create_cache($definition);
@@ -515,4 +520,28 @@ public static function update_site_identifier($siteidentifier) {
$factory->updating_finished();
cache_factory::reset();
}
+
+ /**
+ * Returns the site identifier.
+ *
+ * @return string
+ */
+ public static function get_site_identifier() {
+ if (is_null(self::$siteidentifier)) {
+ $factory = cache_factory::instance();
+ $config = $factory->create_config_instance();
+ self::$siteidentifier = $config->get_site_identifier();
+ }
+ return self::$siteidentifier;
+ }
+
+ /**
+ * Returns the site version.
+ *
+ * @return string
+ */
+ public static function get_site_version() {
+ global $CFG;
+ return (string)$CFG->version;
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.