Permalink
Browse files

MDL-39472 MUC: Optimise cache::get_from_persist_cache

  • Loading branch information...
1 parent 6117c57 commit 3a2e904098a05ab06671be595c6d9afd854fb488 @sammarshallou sammarshallou committed with danpoltawski May 2, 2013
Showing with 16 additions and 4 deletions.
  1. +5 −0 cache/classes/helper.php
  2. +11 −4 cache/classes/loaders.php
@@ -350,6 +350,11 @@ public static function purge_by_event($event) {
* @param string $definition
*/
protected static function ensure_ready_for_stats($store, $definition) {
+ // This function is performance-sensitive, so exit as quickly as possible
+ // if we do not need to do anything.
+ if (isset(self::$stats[$definition][$store])) {
+ return;
+ }
if (!array_key_exists($definition, self::$stats)) {
self::$stats[$definition] = array(
$store => array(
@@ -912,7 +912,8 @@ protected function is_using_persist_cache() {
* @return bool
*/
protected function is_in_persist_cache($key) {
- if (is_array($key)) {
+ // This method of checking if an array was supplied is faster than is_array.
+ if ($key === (array)$key) {
$key = $key['key'];
}
// This could be written as a single line, however it has been split because the ttl check is faster than the instanceof
@@ -933,10 +934,15 @@ protected function is_in_persist_cache($key) {
* @return mixed|false The data from the persist cache or false if it wasn't there.
*/
protected function get_from_persist_cache($key) {
- if (is_array($key)) {
+ // This method of checking if an array was supplied is faster than is_array.
+ if ($key === (array)$key) {
$key = $key['key'];
}
- if (!$this->persist || !array_key_exists($key, $this->persistcache)) {
+ // This isset check is faster than array_key_exists but will return false
+ // for null values, meaning null values will come from backing store not
+ // the persist cache. We think this okay because null usage should be
+ // very rare (see comment in MDL-39472).
+ if (!$this->persist || !isset($this->persistcache[$key])) {
$result = false;
} else {
$data = $this->persistcache[$key];
@@ -976,7 +982,8 @@ protected function get_from_persist_cache($key) {
* @return bool
*/
protected function set_in_persist_cache($key, $data) {
- if (is_array($key)) {
+ // This method of checking if an array was supplied is faster than is_array.
+ if ($key === (array)$key) {
$key = $key['key'];
}
$this->persistcache[$key] = $data;

0 comments on commit 3a2e904

Please sign in to comment.