From ad3bbeb5c0b9ca0b8233e1a296b73b950f7da4e5 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Tue, 26 Nov 2013 12:03:40 +1300 Subject: [PATCH] MDL-43033 cache: added stats logging to get_many methods --- cache/classes/helper.php | 18 ++++++++++++------ cache/classes/loaders.php | 28 +++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/cache/classes/helper.php b/cache/classes/helper.php index 96c66019a2a..970cbd500e5 100644 --- a/cache/classes/helper.php +++ b/cache/classes/helper.php @@ -382,34 +382,40 @@ class cache_helper { /** * Record a cache hit in the stats for the given store and definition. * + * @internal * @param string $store * @param string $definition + * @param int $hits The number of hits to record (by default 1) */ - public static function record_cache_hit($store, $definition) { + public static function record_cache_hit($store, $definition, $hits = 1) { self::ensure_ready_for_stats($store, $definition); - self::$stats[$definition][$store]['hits']++; + self::$stats[$definition][$store]['hits'] += $hits; } /** * Record a cache miss in the stats for the given store and definition. * + * @internal * @param string $store * @param string $definition + * @param int $misses The number of misses to record (by default 1) */ - public static function record_cache_miss($store, $definition) { + public static function record_cache_miss($store, $definition, $misses = 1) { self::ensure_ready_for_stats($store, $definition); - self::$stats[$definition][$store]['misses']++; + self::$stats[$definition][$store]['misses'] += $misses; } /** * Record a cache set in the stats for the given store and definition. * + * @internal * @param string $store * @param string $definition + * @param int $sets The number of sets to record (by default 1) */ - public static function record_cache_set($store, $definition) { + public static function record_cache_set($store, $definition, $sets = 1) { self::ensure_ready_for_stats($store, $definition); - self::$stats[$definition][$store]['sets']++; + self::$stats[$definition][$store]['sets'] += $sets; } /** diff --git a/cache/classes/loaders.php b/cache/classes/loaders.php index 10ad62095cc..b7b04c1c0ed 100644 --- a/cache/classes/loaders.php +++ b/cache/classes/loaders.php @@ -463,6 +463,20 @@ class cache implements cache_loader { } } + if ($this->perfdebug) { + $hits = 0; + $misses = 0; + foreach ($fullresult as $value) { + if ($value === false) { + $misses++; + } else { + $hits++; + } + } + cache_helper::record_cache_hit($this->storetype, $this->definition->get_id(), $hits); + cache_helper::record_cache_miss($this->storetype, $this->definition->get_id(), $misses); + } + // Return the result. Phew! return $fullresult; } @@ -1937,7 +1951,19 @@ class cache_session extends cache { if ($hasmissingkeys && $strictness === MUST_EXIST) { throw new coding_exception('Requested key did not exist in any cache stores and could not be loaded.'); } - + if ($this->perfdebug) { + $hits = 0; + $misses = 0; + foreach ($return as $value) { + if ($value === false) { + $misses++; + } else { + $hits++; + } + } + cache_helper::record_cache_hit($this->storetype, $this->get_definition()->get_id(), $hits); + cache_helper::record_cache_miss($this->storetype, $this->get_definition()->get_id(), $misses); + } return $return; }