mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 08:56:36 +02:00
MDL-40538 caching: Use isset for performance
isset is much faster than array_key_exists. For caches we need to ensure as much performance as possible. To all null to continue to be stored and provide correct results, non ttl also now saves in $this->store[$key][0]. As isset($this->store[$key]) will return true in all cases. time() as found in cache::now() can be slow on some platforms and we shouldn't bother checking it unless needed. It also stops non-ttl caches paying the ttl price. Unit tests were adjusted to ensure null is still handled correctly when we are only using isset.
This commit is contained in:
parent
f8e6e5bc30
commit
ae21341523
3 changed files with 82 additions and 61 deletions
39
cache/stores/static/lib.php
vendored
39
cache/stores/static/lib.php
vendored
|
@ -213,11 +213,10 @@ class cachestore_static extends static_data_store implements cache_is_key_aware
|
|||
* @return mixed The data that was associated with the key, or false if the key did not exist.
|
||||
*/
|
||||
public function get($key) {
|
||||
$maxtime = cache::now() - $this->ttl;
|
||||
if (array_key_exists($key, $this->store)) {
|
||||
if (isset($this->store[$key])) {
|
||||
if ($this->ttl == 0) {
|
||||
return $this->store[$key];
|
||||
} else if ($this->store[$key][1] >= $maxtime) {
|
||||
return $this->store[$key][0];
|
||||
} else if ($this->store[$key][1] >= (cache::now() - $this->ttl)) {
|
||||
return $this->store[$key][0];
|
||||
}
|
||||
}
|
||||
|
@ -235,12 +234,15 @@ class cachestore_static extends static_data_store implements cache_is_key_aware
|
|||
*/
|
||||
public function get_many($keys) {
|
||||
$return = array();
|
||||
$maxtime = cache::now() - $this->ttl;
|
||||
if ($this->ttl != 0) {
|
||||
$maxtime = cache::now() - $this->ttl;
|
||||
}
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$return[$key] = false;
|
||||
if (array_key_exists($key, $this->store)) {
|
||||
if (isset($this->store[$key])) {
|
||||
if ($this->ttl == 0) {
|
||||
$return[$key] = $this->store[$key];
|
||||
$return[$key] = $this->store[$key][0];
|
||||
} else if ($this->store[$key][1] >= $maxtime) {
|
||||
$return[$key] = $this->store[$key][0];
|
||||
}
|
||||
|
@ -258,7 +260,7 @@ class cachestore_static extends static_data_store implements cache_is_key_aware
|
|||
*/
|
||||
public function set($key, $data) {
|
||||
if ($this->ttl == 0) {
|
||||
$this->store[$key] = $data;
|
||||
$this->store[$key][0] = $data;
|
||||
} else {
|
||||
$this->store[$key] = array($data, cache::now());
|
||||
}
|
||||
|
@ -289,11 +291,10 @@ class cachestore_static extends static_data_store implements cache_is_key_aware
|
|||
* @return bool
|
||||
*/
|
||||
public function has($key) {
|
||||
$maxtime = cache::now() - $this->ttl;
|
||||
if (array_key_exists($key, $this->store)) {
|
||||
if (isset($this->store[$key])) {
|
||||
if ($this->ttl == 0) {
|
||||
return true;
|
||||
} else if ($this->store[$key][1] >= $maxtime) {
|
||||
} else if ($this->store[$key][1] >= (cache::now() - $this->ttl)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -307,9 +308,12 @@ class cachestore_static extends static_data_store implements cache_is_key_aware
|
|||
* @return bool
|
||||
*/
|
||||
public function has_all(array $keys) {
|
||||
$maxtime = cache::now() - $this->ttl;
|
||||
if ($this->ttl != 0) {
|
||||
$maxtime = cache::now() - $this->ttl;
|
||||
}
|
||||
|
||||
foreach ($keys as $key) {
|
||||
if (!array_key_exists($key, $this->store)) {
|
||||
if (!isset($this->store[$key])) {
|
||||
return false;
|
||||
}
|
||||
if ($this->ttl != 0 && $this->store[$key][1] < $maxtime) {
|
||||
|
@ -326,9 +330,12 @@ class cachestore_static extends static_data_store implements cache_is_key_aware
|
|||
* @return bool
|
||||
*/
|
||||
public function has_any(array $keys) {
|
||||
$maxtime = cache::now() - $this->ttl;
|
||||
if ($this->ttl != 0) {
|
||||
$maxtime = cache::now() - $this->ttl;
|
||||
}
|
||||
|
||||
foreach ($keys as $key) {
|
||||
if (array_key_exists($key, $this->store) && ($this->ttl == 0 || $this->store[$key][1] >= $maxtime)) {
|
||||
if (isset($this->store[$key]) && ($this->ttl == 0 || $this->store[$key][1] >= $maxtime)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -411,4 +418,4 @@ class cachestore_static extends static_data_store implements cache_is_key_aware
|
|||
public function my_name() {
|
||||
return $this->name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue