This commit is contained in:
Damyon Wiese 2013-07-16 11:02:54 +08:00
commit 4d4fb80d8f
3 changed files with 82 additions and 61 deletions

View File

@ -218,11 +218,10 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
* @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 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
*/
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 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
*/
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 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
* @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 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
* @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 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
* @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;
}
}
@ -439,4 +446,4 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
}
return $return;
}
}
}

View File

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

View File

@ -192,41 +192,43 @@ class cache_phpunit_tests extends advanced_testcase {
*/
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());
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)));
// 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));
// 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)));
// 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));
// Delete it.
$this->assertTrue($cache->delete($key));
// Check its gone.
$this->assertFalse($cache->get($key));
$this->assertFalse($cache->has($key));
// Check its gone.
$this->assertFalse($cache->get($key));
$this->assertFalse($cache->has($key));
}
// Test arrays.
$this->assertTrue($cache->set($key, $dataarray));
@ -261,11 +263,13 @@ class cache_phpunit_tests extends advanced_testcase {
}
// 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),
@ -282,14 +286,17 @@ class cache_phpunit_tests extends advanced_testcase {
// 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;