From 44eaae9610c467a876b6faefaf8ea0cd7ebabdf8 Mon Sep 17 00:00:00 2001 From: Russell Smith Date: Mon, 19 Aug 2013 20:29:21 +1000 Subject: [PATCH] MDL-41292 cache: only update identifiers if they change --- cache/classes/definition.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/cache/classes/definition.php b/cache/classes/definition.php index 65629d0c0e0..47a06816223 100644 --- a/cache/classes/definition.php +++ b/cache/classes/definition.php @@ -256,7 +256,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 @@ -582,6 +582,9 @@ class cache_definition { * @return array */ public function get_identifiers() { + if (!isset($this->identifiers)) { + return array(); + } return $this->identifiers; } @@ -694,11 +697,22 @@ class cache_definition { * @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 (!array_key_exists($identifier, $identifiers)) { 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; } @@ -820,7 +834,7 @@ class cache_definition { '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');