MDL-37683 cache: siteidentifier is now included in the keys

This commit is contained in:
Sam Hemelryk 2013-02-06 13:45:17 +13:00
parent 1dd6835d8c
commit e0d9b7c0d4
11 changed files with 140 additions and 12 deletions

View file

@ -71,6 +71,12 @@ class cache_config {
*/
protected $configlocks = array();
/**
* The site identifier used when the cache config was last saved.
* @var string
*/
protected $siteidentifier = null;
/**
* Please use cache_config::instance to get an instance of the cache config that is ready to be used.
*/
@ -139,6 +145,12 @@ class cache_config {
$this->configdefinitionmappings = array();
$this->configlockmappings = array();
$siteidentifier = 'unknown';
if (array_key_exists('siteidentifier', $configuration)) {
$siteidentifier = $configuration['siteidentifier'];
}
$this->siteidentifier = $siteidentifier;
// Filter the lock instances.
$defaultlock = null;
foreach ($configuration['locks'] as $conf) {
@ -271,6 +283,14 @@ class cache_config {
return true;
}
/**
* Returns the site identifier used by the cache API.
* @return string
*/
public function get_site_identifier() {
return $this->siteidentifier;
}
/**
* Includes the configuration file and makes sure it contains the expected bits.
*

View file

@ -274,6 +274,12 @@ 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.
*
@ -674,6 +680,15 @@ class cache_definition {
$this->keyprefixmulti = null;
}
/**
* 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
@ -723,7 +738,8 @@ class cache_definition {
*/
public function generate_single_key_prefix() {
if ($this->keyprefixsingle === null) {
$this->keyprefixsingle = $this->mode.'/'.$this->mode;
$this->keyprefixsingle = $this->mode.'/'.$this->component.'/'.$this->area;
$this->keyprefixsingle .= '/'.$this->cacheidentifier;
$identifiers = $this->get_identifiers();
if ($identifiers) {
foreach ($identifiers as $key => $value) {
@ -746,6 +762,7 @@ class cache_definition {
'mode' => $this->mode,
'component' => $this->component,
'area' => $this->area,
'siteidentifier' => $this->cacheidentifier
);
if (!empty($this->identifiers)) {
$identifiers = array();

View file

@ -203,6 +203,8 @@ class cache_factory {
}
// 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()) {
@ -390,6 +392,7 @@ class cache_factory {
} else {
$definition = cache_definition::load($id, $definition, $aggregate);
}
$definition->set_cache_identifier($instance->get_site_identifier());
}
$this->definitions[$id] = $definition;
}

View file

@ -234,6 +234,7 @@ class cache_helper {
$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.
@ -304,6 +305,7 @@ class cache_helper {
$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);
@ -496,4 +498,21 @@ class cache_helper {
// Second reset anything we have already initialised to ensure we're all up to date.
cache_factory::reset();
}
/**
* Update the site identifier stored by the cache API.
*
* @param string $siteidentifier
*/
public static function update_site_identifier($siteidentifier) {
global $CFG;
// Include locallib
require_once($CFG->dirroot.'/cache/locallib.php');
$factory = cache_factory::instance();
$factory->updating_started();
$config = $factory->create_config_instance(true);
$config->update_site_identifier($siteidentifier);
$factory->updating_finished();
cache_factory::reset();
}
}