Merge branch 'wip-MDL-36407-m24' of git://github.com/samhemelryk/moodle

This commit is contained in:
Dan Poltawski
2012-11-16 15:04:17 +08:00
7 changed files with 120 additions and 38 deletions
+17 -6
View File
@@ -457,20 +457,31 @@ class cache_definition {
* @param int $mode One of cache_store::MODE_*
* @param string $component The component this definition relates to.
* @param string $area The area this definition relates to.
* @param string $overrideclass The class to use as the loader.
* @param bool $persistent If this cache should be persistent.
* @param array $options An array of options, available options are:
* - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_
* - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars
* - overrideclass : The class to use as the loader.
* - persistent : If set to true the cache will persist construction requests.
* @return cache_application|cache_session|cache_request
*/
public static function load_adhoc($mode, $component, $area, $overrideclass = null, $persistent = false) {
public static function load_adhoc($mode, $component, $area, array $options = array()) {
$id = 'adhoc/'.$component.'_'.$area;
$definition = array(
'mode' => $mode,
'component' => $component,
'area' => $area,
'persistent' => $persistent
);
if (!is_null($overrideclass)) {
$definition['overrideclass'] = $overrideclass;
if (!empty($options['simplekeys'])) {
$definition['simplekeys'] = $options['simplekeys'];
}
if (!empty($options['simpledata'])) {
$definition['simpledata'] = $options['simpledata'];
}
if (!empty($options['persistent'])) {
$definition['persistent'] = $options['persistent'];
}
if (!empty($options['overrideclass'])) {
$definition['overrideclass'] = $options['overrideclass'];
}
return self::load($id, $definition, null);
}
+6 -3
View File
@@ -151,16 +151,19 @@ class cache_factory {
* @param string $component
* @param string $area
* @param array $identifiers
* @param bool $persistent
* @param array $options An array of options, available options are:
* - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_
* - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars
* - persistent : If set to true the cache will persist construction requests.
* @return cache_application|cache_session|cache_request
*/
public function create_cache_from_params($mode, $component, $area, array $identifiers = array(), $persistent = false) {
public function create_cache_from_params($mode, $component, $area, array $identifiers = array(), array $options = array()) {
$key = "{$mode}_{$component}_{$area}";
if (array_key_exists($key, $this->cachesfromparams)) {
return $this->cachesfromparams[$key];
}
// Get the class. Note this is a late static binding so we need to use get_called_class.
$definition = cache_definition::load_adhoc($mode, $component, $area, null, $persistent);
$definition = cache_definition::load_adhoc($mode, $component, $area, $options);
$definition->set_identifiers($identifiers);
$cache = $this->create_cache($definition, $identifiers);
if ($definition->should_be_persistent()) {
+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();
}
+36 -24
View File
@@ -176,12 +176,15 @@ class cache implements cache_loader {
* @param string $component The component this cache relates to.
* @param string $area The area this cache relates to.
* @param array $identifiers Any additional identifiers that should be provided to the definition.
* @param bool $persistent If set to true the cache will persist construction requests.
* @param array $options An array of options, available options are:
* - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_
* - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars
* - persistent : If set to true the cache will persist construction requests.
* @return cache_application|cache_session|cache_store
*/
public static function make_from_params($mode, $component, $area, array $identifiers = array(), $persistent = false) {
public static function make_from_params($mode, $component, $area, array $identifiers = array(), array $options = array()) {
$factory = cache_factory::instance();
return $factory->create_cache_from_params($mode, $component, $area, $identifiers, $persistent);
return $factory->create_cache_from_params($mode, $component, $area, $identifiers, $options);
}
/**
@@ -272,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.
@@ -285,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);
@@ -906,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;
}
}
+3 -3
View File
@@ -47,9 +47,9 @@ $sessiontable = clone($applicationtable);
$requesttable = clone($applicationtable);
$application = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cache', 'applicationtest', null, false);
$session = cache_definition::load_adhoc(cache_store::MODE_SESSION, 'cache', 'sessiontest', null, false);
$request = cache_definition::load_adhoc(cache_store::MODE_REQUEST, 'cache', 'requesttest', null, false);
$application = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cache', 'applicationtest');
$session = cache_definition::load_adhoc(cache_store::MODE_SESSION, 'cache', 'sessiontest');
$request = cache_definition::load_adhoc(cache_store::MODE_REQUEST, 'cache', 'requesttest');
$strinvalidplugin = new lang_string('invalidplugin', 'cache');
$strunsupportedmode = new lang_string('unsupportedmode', 'cache');
+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
@@ -6490,7 +6490,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);
}
}