diff --git a/cache/classes/helper.php b/cache/classes/helper.php index 9ac4977c0d3..5a088bb8c51 100644 --- a/cache/classes/helper.php +++ b/cache/classes/helper.php @@ -377,34 +377,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 dc2b96ab76c..ae2956e9b4e 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; } @@ -1872,7 +1886,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; }