Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit 2215cc347a0ac85e6fbba39c88b45eb4f098299e 1 parent 919251f
mr-russ mr-russ authored
39 cache/stores/session/lib.php
View
@@ -218,11 +218,10 @@ public function is_ready() {
* @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];
}
}
@@ -240,12 +239,15 @@ public function get($key) {
*/
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];
}
@@ -263,7 +265,7 @@ public function get_many($keys) {
*/
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());
}
@@ -294,11 +296,10 @@ public function set_many(array $keyvaluearray) {
* @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;
}
}
@@ -312,9 +313,12 @@ public function has($key) {
* @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) {
@@ -331,9 +335,12 @@ public function has_all(array $keys) {
* @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;
}
}
@@ -436,4 +443,4 @@ public function find_by_prefix($prefix) {
}
return $return;
}
-}
+}
39 cache/stores/static/lib.php
View
@@ -213,11 +213,10 @@ public function is_ready() {
* @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 @@ public function get($key) {
*/
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 @@ public function get_many($keys) {
*/
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 @@ public function set_many(array $keyvaluearray) {
* @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 @@ public function has($key) {
* @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 @@ public function has_all(array $keys) {
* @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;
}
}
@@ -408,4 +415,4 @@ public static function initialise_test_instance(cache_definition $definition) {
public function my_name() {
return $this->name;
}
-}
+}
75 cache/tests/cache_test.php
View
@@ -192,41 +192,43 @@ public function test_on_cache_without_store() {
*/
protected function run_on_cache(cache_loader $cache) {
$key = 'testkey';
- $datascalar = 'test data';
+ $datascalars = array('test data', null);
$dataarray = array('test' => 'data', 'part' => 'two');
$dataobject = (object)$dataarray;
- $this->assertTrue($cache->purge());
-
- // Check all read methods.
- $this->assertFalse($cache->get($key));
- $this->assertFalse($cache->has($key));
- $result = $cache->get_many(array($key));
- $this->assertCount(1, $result);
- $this->assertFalse(reset($result));
- $this->assertFalse($cache->has_any(array($key)));
- $this->assertFalse($cache->has_all(array($key)));
-
- // Set the data.
- $this->assertTrue($cache->set($key, $datascalar));
- // Setting it more than once should be permitted.
- $this->assertTrue($cache->set($key, $datascalar));
-
- // Recheck the read methods.
- $this->assertEquals($datascalar, $cache->get($key));
- $this->assertTrue($cache->has($key));
- $result = $cache->get_many(array($key));
- $this->assertCount(1, $result);
- $this->assertEquals($datascalar, reset($result));
- $this->assertTrue($cache->has_any(array($key)));
- $this->assertTrue($cache->has_all(array($key)));
-
- // Delete it.
- $this->assertTrue($cache->delete($key));
-
- // Check its gone.
- $this->assertFalse($cache->get($key));
- $this->assertFalse($cache->has($key));
+ foreach ($datascalars as $datascalar) {
+ $this->assertTrue($cache->purge());
+
+ // Check all read methods.
+ $this->assertFalse($cache->get($key));
+ $this->assertFalse($cache->has($key));
+ $result = $cache->get_many(array($key));
+ $this->assertCount(1, $result);
+ $this->assertFalse(reset($result));
+ $this->assertFalse($cache->has_any(array($key)));
+ $this->assertFalse($cache->has_all(array($key)));
+
+ // Set the data.
+ $this->assertTrue($cache->set($key, $datascalar));
+ // Setting it more than once should be permitted.
+ $this->assertTrue($cache->set($key, $datascalar));
+
+ // Recheck the read methods.
+ $this->assertEquals($datascalar, $cache->get($key));
+ $this->assertTrue($cache->has($key));
+ $result = $cache->get_many(array($key));
+ $this->assertCount(1, $result);
+ $this->assertEquals($datascalar, reset($result));
+ $this->assertTrue($cache->has_any(array($key)));
+ $this->assertTrue($cache->has_all(array($key)));
+
+ // Delete it.
+ $this->assertTrue($cache->delete($key));
+
+ // Check its gone.
+ $this->assertFalse($cache->get($key));
+ $this->assertFalse($cache->has($key));
+ }
// Test arrays.
$this->assertTrue($cache->set($key, $dataarray));
@@ -244,11 +246,13 @@ protected function run_on_cache(cache_loader $cache) {
$this->assertEquals('blue_ptc_wfc', $result->property2);
// Test set many.
- $cache->set_many(array('key1' => 'data1', 'key2' => 'data2'));
+ $cache->set_many(array('key1' => 'data1', 'key2' => 'data2', 'key3' => null));
$this->assertEquals('data1', $cache->get('key1'));
$this->assertEquals('data2', $cache->get('key2'));
+ $this->assertEquals(null, $cache->get('key3'));
$this->assertTrue($cache->delete('key1'));
$this->assertTrue($cache->delete('key2'));
+ $this->assertTrue($cache->delete('key3'));
$cache->set_many(array(
'key1' => array(1, 2, 3),
@@ -265,14 +269,17 @@ protected function run_on_cache(cache_loader $cache) {
// Test delete many.
$this->assertTrue($cache->set('key1', 'data1'));
$this->assertTrue($cache->set('key2', 'data2'));
+ $this->assertTrue($cache->set('key3', null));
$this->assertEquals('data1', $cache->get('key1'));
$this->assertEquals('data2', $cache->get('key2'));
+ $this->assertEquals(null, $cache->get('key3'));
- $this->assertEquals(2, $cache->delete_many(array('key1', 'key2')));
+ $this->assertEquals(3, $cache->delete_many(array('key1', 'key2', 'key3')));
$this->assertFalse($cache->get('key1'));
$this->assertFalse($cache->get('key2'));
+ $this->assertFalse($cache->get('key3'));
// Quick reference test.
$obj = new stdClass;
Please sign in to comment.
Something went wrong with that request. Please try again.