Skip to content

Commit

Permalink
MDL-41292 cache: only update identifiers if they change
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-russ committed Dec 7, 2013
1 parent c36a240 commit 0dd7c71
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions cache/classes/definition.php
Expand Up @@ -281,7 +281,7 @@ class cache_definition {
* An array of identifiers provided to this cache when it was initialised.
* @var array
*/
protected $identifiers = array();
protected $identifiers = null;

/**
* Key prefix for use with single key cache stores
Expand Down Expand Up @@ -654,6 +654,9 @@ public function uses_simple_keys() {
* @return array
*/
public function get_identifiers() {
if (!isset($this->identifiers)) {
return array();
}
return $this->identifiers;
}

Expand Down Expand Up @@ -766,11 +769,22 @@ public function get_data_source() {
* @throws coding_exception
*/
public function set_identifiers(array $identifiers = array()) {
// If we are setting the exact same identifiers then just return as nothing really changed.
// We don't care about order as cache::make will use the same definition order all the time.
if ($identifiers === $this->identifiers) {
return;
}

foreach ($this->requireidentifiers as $identifier) {
if (!isset($identifiers[$identifier])) {
throw new coding_exception('Identifier required for cache has not been provided: '.$identifier);
}
}

if ($this->identifiers === null) {
// Initialize identifiers if they have not been.
$this->identifiers = array();
}
foreach ($identifiers as $name => $value) {
$this->identifiers[$name] = (string)$value;
}
Expand Down Expand Up @@ -893,7 +907,7 @@ public function generate_multi_key_parts() {
'area' => $this->area,
'siteidentifier' => $this->get_cache_identifier()
);
if (!empty($this->identifiers)) {
if (isset($this->identifiers) && !empty($this->identifiers)) {
$identifiers = array();
foreach ($this->identifiers as $key => $value) {
$identifiers[] = htmlentities($key, ENT_QUOTES, 'UTF-8').'='.htmlentities($value, ENT_QUOTES, 'UTF-8');
Expand Down

0 comments on commit 0dd7c71

Please sign in to comment.