MDL-43033 cache: added stats logging to get_many methods

This commit is contained in:
Sam Hemelryk 2013-11-26 12:03:40 +13:00
parent 9b37cd72a2
commit 6dbe2df208
2 changed files with 39 additions and 7 deletions

View File

@ -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;
}
/**

View File

@ -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;
}