MDL-36407 cache: unit tests + checks + improved string cache

This commit is contained in:
Sam Hemelryk
2012-11-16 09:46:49 +13:00
parent 2566210c63
commit 5dd68a7523
4 changed files with 88 additions and 23 deletions
+3
View File
@@ -453,6 +453,9 @@ class cache_helper {
*/
public static function hash_key($key, cache_definition $definition) {
if ($definition->uses_simple_keys()) {
if (debugging() && preg_match('#[^a-zA-Z0-9_]#', $key)) {
throw new coding_exception('Cache definition '.$definition->get_id().' requires simple keys. Invalid key provided.', $key);
}
// We put the key first so that we can be sure the start of the key changes.
return (string)$key . '-' . $definition->generate_single_key_prefix();
}
+30 -21
View File
@@ -275,11 +275,11 @@ class cache implements cache_loader {
// 1. Parse the key.
$parsedkey = $this->parse_key($key);
// 2. Get it from the persist cache if we can (only when persist is enabled and it has already been requested/set).
$result = $this->get_from_persist_cache($parsedkey);
$result = false;
if ($this->is_using_persist_cache()) {
$result = $this->get_from_persist_cache($parsedkey);
}
if ($result !== false) {
if ($this->perfdebug) {
cache_helper::record_cache_hit('** static persist **', $this->definition->get_id());
}
if (!is_scalar($result)) {
// If data is an object it will be a reference.
// If data is an array if may contain references.
@@ -288,8 +288,6 @@ class cache implements cache_loader {
$result = $this->unref($result);
}
return $result;
} else if ($this->perfdebug) {
cache_helper::record_cache_miss('** static persist **', $this->definition->get_id());
}
// 3. Get it from the store. Obviously wasn't in the persist cache.
$result = $this->store->get($parsedkey);
@@ -909,23 +907,34 @@ class cache implements cache_loader {
$key = $key['key'];
}
if (!$this->persist || !array_key_exists($key, $this->persistcache)) {
return false;
}
$data = $this->persistcache[$key];
if (!$this->has_a_ttl() || !$data instanceof cache_ttl_wrapper) {
if ($data instanceof cache_cached_object) {
$data = $data->restore_object();
}
return $data;
}
if ($data->has_expired()) {
$this->delete_from_persist_cache($key);
return false;
$result = false;
} else {
if ($data instanceof cache_cached_object) {
$data = $data->restore_object();
$data = $this->persistcache[$key];
if (!$this->has_a_ttl() || !$data instanceof cache_ttl_wrapper) {
if ($data instanceof cache_cached_object) {
$data = $data->restore_object();
}
$result = $data;
} else if ($data->has_expired()) {
$this->delete_from_persist_cache($key);
$result = false;
} else {
if ($data instanceof cache_cached_object) {
$data = $data->restore_object();
}
$result = $data->data;
}
return $data->data;
}
if ($result) {
if ($this->perfdebug) {
cache_helper::record_cache_hit('** static persist **', $this->definition->get_id());
}
return $result;
} else {
if ($this->perfdebug) {
cache_helper::record_cache_miss('** static persist **', $this->definition->get_id());
}
return false;
}
}
+50 -1
View File
@@ -422,4 +422,53 @@ class cache_administration_helper_phpunit_tests extends advanced_testcase {
$this->assertInstanceOf('coding_exception', $e);
}
}
}
/**
* Test the hash_key functionality.
*/
public function test_hash_key() {
global $CFG;
$currentdebugging = $CFG->debug;
$CFG->debug = E_ALL;
// First with simplekeys
$instance = cache_config_phpunittest::instance(true);
$instance->phpunit_add_definition('phpunit/hashtest', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'hashtest',
'simplekeys' => true
));
$factory = cache_factory::instance();
$definition = $factory->create_definition('phpunit', 'hashtest');
$result = cache_helper::hash_key('test', $definition);
$this->assertEquals('test-'.$definition->generate_single_key_prefix(), $result);
try {
cache_helper::hash_key('test/test', $definition);
$this->fail('Invalid key was allowed, you should see this.');
} catch (coding_exception $e) {
$this->assertEquals('test/test', $e->debuginfo);
}
// Second without simple keys
$instance->phpunit_add_definition('phpunit/hashtest2', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'hashtest2',
'simplekeys' => false
));
$definition = $factory->create_definition('phpunit', 'hashtest2');
$result = cache_helper::hash_key('test', $definition);
$this->assertEquals(sha1($definition->generate_single_key_prefix().'-test'), $result);
$result = cache_helper::hash_key('test/test', $definition);
$this->assertEquals(sha1($definition->generate_single_key_prefix().'-test/test'), $result);
$CFG->debug = $currentdebugging;
}
}
+5 -1
View File
@@ -6489,7 +6489,11 @@ class core_string_manager implements string_manager {
$this->cache = cache::make('core', 'string');
} else {
// We only want a cache for the length of the request, create a static cache.
$this->cache = cache::make_from_params(cache_store::MODE_REQUEST, 'core', 'string');
$options = array(
'simplekeys' => true,
'simpledata' => true
);
$this->cache = cache::make_from_params(cache_store::MODE_REQUEST, 'core', 'string', array(), $options);
}
}