Skip to content

Commit

Permalink
MDL-68329 cache: Improve cache performance footer info
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanheywood committed Apr 28, 2020
1 parent 9df4a4d commit 9f4bb48
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 105 deletions.
39 changes: 32 additions & 7 deletions cache/classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,20 +361,23 @@ public static function purge_by_event($event) {
/**
* Ensure that the stats array is ready to collect information for the given store and definition.
* @param string $store
* @param string $storeclass
* @param string $definition A string that identifies the definition.
* @param int $mode One of cache_store::MODE_*. Since 2.9.
*/
protected static function ensure_ready_for_stats($store, $definition, $mode = cache_store::MODE_APPLICATION) {
protected static function ensure_ready_for_stats($store, $storeclass, $definition, $mode = cache_store::MODE_APPLICATION) {
// This function is performance-sensitive, so exit as quickly as possible
// if we do not need to do anything.
if (isset(self::$stats[$definition]['stores'][$store])) {
return;
}

if (!array_key_exists($definition, self::$stats)) {
self::$stats[$definition] = array(
'mode' => $mode,
'stores' => array(
$store => array(
'class' => $storeclass,
'hits' => 0,
'misses' => 0,
'sets' => 0,
Expand All @@ -383,6 +386,7 @@ protected static function ensure_ready_for_stats($store, $definition, $mode = ca
);
} else if (!array_key_exists($store, self::$stats[$definition]['stores'])) {
self::$stats[$definition]['stores'][$store] = array(
'class' => $storeclass,
'hits' => 0,
'misses' => 0,
'sets' => 0,
Expand Down Expand Up @@ -418,15 +422,22 @@ protected static function get_definition_stat_id_and_mode($definition) {
* In Moodle 2.9 the $definition argument changed from accepting only a string to accepting a string or a
* cache_definition instance. It is preferable to pass a cache definition instance.
*
* In Moodle 3.9 the first argument changed to also accept a cache_store.
*
* @internal
* @param cache_definition $store
* @param string|cache_store $store
* @param cache_definition $definition You used to be able to pass a string here, however that is deprecated please pass the
* actual cache_definition object now.
* @param int $hits The number of hits to record (by default 1)
*/
public static function record_cache_hit($store, $definition, $hits = 1) {
$storeclass = '';
if ($store instanceof cache_store) {
$storeclass = get_class($store);
$store = $store->my_name();
}
list($definitionstr, $mode) = self::get_definition_stat_id_and_mode($definition);
self::ensure_ready_for_stats($store, $definitionstr, $mode);
self::ensure_ready_for_stats($store, $storeclass, $definitionstr, $mode);
self::$stats[$definitionstr]['stores'][$store]['hits'] += $hits;
}

Expand All @@ -436,15 +447,22 @@ public static function record_cache_hit($store, $definition, $hits = 1) {
* In Moodle 2.9 the $definition argument changed from accepting only a string to accepting a string or a
* cache_definition instance. It is preferable to pass a cache definition instance.
*
* In Moodle 3.9 the first argument changed to also accept a cache_store.
*
* @internal
* @param string $store
* @param string|cache_store $store
* @param cache_definition $definition You used to be able to pass a string here, however that is deprecated please pass the
* actual cache_definition object now.
* @param int $misses The number of misses to record (by default 1)
*/
public static function record_cache_miss($store, $definition, $misses = 1) {
$storeclass = '';
if ($store instanceof cache_store) {
$storeclass = get_class($store);
$store = $store->my_name();
}
list($definitionstr, $mode) = self::get_definition_stat_id_and_mode($definition);
self::ensure_ready_for_stats($store, $definitionstr, $mode);
self::ensure_ready_for_stats($store, $storeclass, $definitionstr, $mode);
self::$stats[$definitionstr]['stores'][$store]['misses'] += $misses;
}

Expand All @@ -454,15 +472,22 @@ public static function record_cache_miss($store, $definition, $misses = 1) {
* In Moodle 2.9 the $definition argument changed from accepting only a string to accepting a string or a
* cache_definition instance. It is preferable to pass a cache definition instance.
*
* In Moodle 3.9 the first argument changed to also accept a cache_store.
*
* @internal
* @param string $store
* @param string|cache_store $store
* @param cache_definition $definition You used to be able to pass a string here, however that is deprecated please pass the
* actual cache_definition object now.
* @param int $sets The number of sets to record (by default 1)
*/
public static function record_cache_set($store, $definition, $sets = 1) {
$storeclass = '';
if ($store instanceof cache_store) {
$storeclass = get_class($store);
$store = $store->my_name();
}
list($definitionstr, $mode) = self::get_definition_stat_id_and_mode($definition);
self::ensure_ready_for_stats($store, $definitionstr, $mode);
self::ensure_ready_for_stats($store, $storeclass, $definitionstr, $mode);
self::$stats[$definitionstr]['stores'][$store]['sets'] += $sets;
}

Expand Down
28 changes: 14 additions & 14 deletions cache/classes/loaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public function get($key, $strictness = IGNORE_MISSING) {
$setaftervalidation = false;
if ($result === false) {
if ($this->perfdebug) {
cache_helper::record_cache_miss($this->storetype, $this->definition);
cache_helper::record_cache_miss($this->store, $this->definition);
}
if ($this->loader !== false) {
// We must pass the original (unparsed) key to the next loader in the chain.
Expand All @@ -426,7 +426,7 @@ public function get($key, $strictness = IGNORE_MISSING) {
}
$setaftervalidation = ($result !== false);
} else if ($this->perfdebug) {
cache_helper::record_cache_hit($this->storetype, $this->definition);
cache_helper::record_cache_hit($this->store, $this->definition);
}
// 5. Validate strictness.
if ($strictness === MUST_EXIST && $result === false) {
Expand Down Expand Up @@ -580,8 +580,8 @@ public function get_many(array $keys, $strictness = IGNORE_MISSING) {
$hits++;
}
}
cache_helper::record_cache_hit($this->storetype, $this->definition, $hits);
cache_helper::record_cache_miss($this->storetype, $this->definition, $misses);
cache_helper::record_cache_hit($this->store, $this->definition, $hits);
cache_helper::record_cache_miss($this->store, $this->definition, $misses);
}

// Return the result. Phew!
Expand All @@ -607,7 +607,7 @@ public function get_many(array $keys, $strictness = IGNORE_MISSING) {
*/
public function set($key, $data) {
if ($this->perfdebug) {
cache_helper::record_cache_set($this->storetype, $this->definition);
cache_helper::record_cache_set($this->store, $this->definition);
}
if ($this->loader !== false) {
// We have a loader available set it there as well.
Expand Down Expand Up @@ -762,7 +762,7 @@ public function set_many(array $keyvaluearray) {
}
$successfullyset = $this->store->set_many($data);
if ($this->perfdebug && $successfullyset) {
cache_helper::record_cache_set($this->storetype, $this->definition, $successfullyset);
cache_helper::record_cache_set($this->store, $this->definition, $successfullyset);
}
return $successfullyset;
}
Expand Down Expand Up @@ -1112,7 +1112,7 @@ protected function static_acceleration_get($key) {
}
if ($result !== false) {
if ($this->perfdebug) {
cache_helper::record_cache_hit('** static acceleration **', $this->definition);
cache_helper::record_cache_hit(cache_store::STATIC_ACCEL, $this->definition);
}
if ($this->staticaccelerationsize > 1 && $this->staticaccelerationcount > 1) {
// Check to see if this is the last item on the static acceleration keys array.
Expand All @@ -1126,7 +1126,7 @@ protected function static_acceleration_get($key) {
return $result;
} else {
if ($this->perfdebug) {
cache_helper::record_cache_miss('** static acceleration **', $this->definition);
cache_helper::record_cache_miss(cache_store::STATIC_ACCEL, $this->definition);
}
return false;
}
Expand Down Expand Up @@ -1830,7 +1830,7 @@ public function get($key, $strictness = IGNORE_MISSING) {
// 4. Load if from the loader/datasource if we don't already have it.
if ($result === false) {
if ($this->perfdebug) {
cache_helper::record_cache_miss($this->storetype, $this->get_definition());
cache_helper::record_cache_miss($this->get_store(), $this->get_definition());
}
if ($this->get_loader() !== false) {
// We must pass the original (unparsed) key to the next loader in the chain.
Expand All @@ -1845,7 +1845,7 @@ public function get($key, $strictness = IGNORE_MISSING) {
$this->set($key, $result);
}
} else if ($this->perfdebug) {
cache_helper::record_cache_hit($this->storetype, $this->get_definition());
cache_helper::record_cache_hit($this->get_store(), $this->get_definition());
}
// 5. Validate strictness.
if ($strictness === MUST_EXIST && $result === false) {
Expand Down Expand Up @@ -1889,7 +1889,7 @@ public function set($key, $data) {
$loader->set($key, $data);
}
if ($this->perfdebug) {
cache_helper::record_cache_set($this->storetype, $this->get_definition());
cache_helper::record_cache_set($this->get_store(), $this->get_definition());
}
if (is_object($data) && $data instanceof cacheable_object) {
$data = new cache_cached_object($data);
Expand Down Expand Up @@ -2019,8 +2019,8 @@ public function get_many(array $keys, $strictness = IGNORE_MISSING) {
$hits++;
}
}
cache_helper::record_cache_hit($this->storetype, $this->get_definition(), $hits);
cache_helper::record_cache_miss($this->storetype, $this->get_definition(), $misses);
cache_helper::record_cache_hit($this->get_store(), $this->get_definition(), $hits);
cache_helper::record_cache_miss($this->get_store(), $this->get_definition(), $misses);
}
return $return;

Expand Down Expand Up @@ -2097,7 +2097,7 @@ public function set_many(array $keyvaluearray) {
}
$successfullyset = $this->get_store()->set_many($data);
if ($this->perfdebug && $successfullyset) {
cache_helper::record_cache_set($this->storetype, $this->get_definition(), $successfullyset);
cache_helper::record_cache_set($this->store, $this->get_definition(), $successfullyset);
}
return $successfullyset;
}
Expand Down
4 changes: 4 additions & 0 deletions cache/classes/store.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ abstract class cache_store implements cache_store_interface {
* Request caches. Static caches really.
*/
const MODE_REQUEST = 4;
/**
* Static caches.
*/
const STATIC_ACCEL = '** static accel. **';

/**
* Constructs an instance of the cache store.
Expand Down
Loading

0 comments on commit 9f4bb48

Please sign in to comment.