Skip to content

Commit

Permalink
MDL-25290 cache: Added UI to view the cache lock setups and tidied up…
Browse files Browse the repository at this point in the history
… a couple of things
  • Loading branch information
Sam Hemelryk committed Oct 7, 2012
1 parent 34c84c7 commit 167ad91
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 49 deletions.
14 changes: 14 additions & 0 deletions cache/admin.php
Expand Up @@ -39,6 +39,7 @@
$plugins = cache_administration_helper::get_plugin_summaries();
$definitions = cache_administration_helper::get_definition_summaries();
$defaultmodestores = cache_administration_helper::get_default_mode_stores();
$locks = cache_administration_helper::get_lock_summaries();

$title = new lang_string('cacheadmin', 'cache');
$mform = null;
Expand All @@ -60,6 +61,12 @@
} else if ($data = $mform->get_data()) {
$config = cache_administration_helper::get_store_configuration_from_data($data);
$writer = cache_config_writer::instance();
unset($config['lock']);
foreach ($writer->get_locks() as $lock => $lockconfig) {
if ($lock == $data->lock) {
$config['lock'] = $data->lock;
}
}
$writer->add_plugin_instance($data->name, $data->plugin, $config);
redirect($PAGE->url, get_string('addstoresuccess', 'cache', $plugins[$plugin]['name']), 5);
}
Expand All @@ -74,6 +81,12 @@
} else if ($data = $mform->get_data()) {
$config = cache_administration_helper::get_store_configuration_from_data($data);
$writer = cache_config_writer::instance();
unset($config['lock']);
foreach ($writer->get_locks() as $lock => $lockconfig) {
if ($lock == $data->lock) {
$config['lock'] = $data->lock;
}
}
$writer->edit_plugin_instance($data->name, $data->plugin, $config);
redirect($PAGE->url, get_string('editstoresuccess', 'cache', $plugins[$plugin]['name']), 5);
}
Expand Down Expand Up @@ -172,6 +185,7 @@
echo $renderer->plugin_summaries($plugins);
echo $renderer->store_summariers($stores, $plugins);
echo $renderer->definition_summaries($definitions, cache_administration_helper::get_definition_actions($context));
echo $renderer->lock_summaries($locks);

$applicationstore = join(', ', $defaultmodestores[cache_store::MODE_APPLICATION]);
$sessionstore = join(', ', $defaultmodestores[cache_store::MODE_SESSION]);
Expand Down
11 changes: 4 additions & 7 deletions cache/classes/config.php
Expand Up @@ -141,7 +141,8 @@ public function load() {
debugging('Duplicate cache lock detected. This should never happen.', DEBUG_DEVELOPER);
continue;
}
if ($defaultlock === null || !empty($this->configlocks['default'])) {
$conf['default'] = (!empty($conf['default']));
if ($defaultlock === null || $conf['default']) {
$defaultlock = $name;
}
$this->configlocks[$name] = $conf;
Expand Down Expand Up @@ -175,13 +176,9 @@ public function load() {
if (!array_key_exists('configuration', $store) || !is_array($store['configuration'])) {
$store['configuration'] = array();
}
if (!empty($store['useforlocking'])) {
// The site has a specified cache for locking.
unset($this->configstores['default_locking']);
}
$store['class'] = $class;
$store['default'] = !empty($store['default']);
if (!array_key_exists('lock', $store) || !array_key_exists($this->configlocks, $store['lock'])) {
if (!array_key_exists('lock', $store) || !array_key_exists($store['lock'], $this->configlocks)) {
$store['lock'] = $defaultlock;
}

Expand Down Expand Up @@ -434,7 +431,7 @@ public function get_definition_mappings() {

/**
* Returns an array of the configured locks.
* @return array
* @return array Array of name => config
*/
public function get_locks() {
return $this->configlocks;
Expand Down
9 changes: 9 additions & 0 deletions cache/forms.php
Expand Up @@ -46,6 +46,7 @@ protected final function definition() {
$form = $this->_form;
$store = $this->_customdata['store'];
$plugin = $this->_customdata['plugin'];
$locks = $this->_customdata['locks'];

$form->addElement('hidden', 'plugin', $plugin);
$form->addElement('hidden', 'editing', !empty($this->_customdata['store']));
Expand All @@ -60,6 +61,14 @@ protected final function definition() {
$form->addElement('static', 'name-value', get_string('storename', 'cache'), $store);
}

if (is_array($locks)) {
$form->addElement('select', 'lock', get_string('lockmethod', 'cache'), $locks);
$form->addHelpButton('lock', 'lockmethod', 'cache');
$form->setType('lock', PARAM_TEXT);
} else {
$form->addElement('hidden', 'lock', '');
$form->addElement('static', 'lock-value', get_string('lockmethod', 'cache'), '<em>'.get_string('nativelocking', 'cache').'</em>');
}

if (method_exists($this, 'configuration_definition')) {
$form->addElement('header', 'storeconfiguration', get_string('storeconfiguration', 'cache'));
Expand Down
99 changes: 70 additions & 29 deletions cache/locallib.php
Expand Up @@ -137,10 +137,12 @@ public function add_plugin_instance($name, $plugin, array $configuration = array
'configuration' => $configuration,
'features' => $class::get_supported_features($configuration),
'modes' => $class::get_supported_modes($configuration),
'mappingsonly' => !empty($configuration['mappingsonly']),
'useforlocking' => !empty($configuration['useforlocking'])

'mappingsonly' => !empty($configuration['mappingsonly'])
);
if (array_key_exists('lock', $configuration)) {
$this->configstores[$name]['lock'] = $configuration['lock'];
unset($this->configstores[$name]['configuration']['lock']);
}
$this->config_save();
return true;
}
Expand Down Expand Up @@ -228,9 +230,12 @@ public function edit_plugin_instance($name, $plugin, $configuration) {
'configuration' => $configuration,
'features' => $class::get_supported_features($configuration),
'modes' => $class::get_supported_modes($configuration),
'mappingsonly' => !empty($configuration['mappingsonly']),
'useforlocking' => !empty($configuration['useforlocking'])
'mappingsonly' => !empty($configuration['mappingsonly'])
);
if (array_key_exists('lock', $configuration)) {
$this->configstores[$name]['lock'] = $configuration['lock'];
unset($this->configstores[$name]['configuration']['lock']);
}
$this->config_save();
return true;
}
Expand Down Expand Up @@ -285,17 +290,6 @@ public static function create_default_configuration() {

$writer = new self;
$writer->configstores = array(
'default_locking' => array(
'name' => 'default_locking',
'plugin' => 'file',
'configuration' => array(),
'features' => cachestore_file::get_supported_features(),
'modes' => cache_store::MODE_APPLICATION,
'useforlocking' => true,
'mappingsonly' => true,
'default' => true,
//'class' => 'cachestore_file'
),
'default_application' => array(
'name' => 'default_application',
'plugin' => 'file',
Expand Down Expand Up @@ -342,18 +336,12 @@ public static function create_default_configuration() {
'sort' => -1
)
);
$writer->configdefinitionmappings = array(
array(
'store' => 'default_locking',
'definition' => 'core/locking',
'sort' => -1
)
);
$writer->configlocks = array(
'default_file_lock' => array(
'name' => 'default_file_lock',
'name' => 'cachelock_file_default',
'type' => 'cachelock_file',
'dir' => 'filelocks'
'dir' => 'filelocks',
'default' => true
)
);
$writer->config_save();
Expand Down Expand Up @@ -740,8 +728,33 @@ public static function get_add_store_form($plugin) {
}
}

$supportsnativelocking = false;
if (file_exists($plugindir.'/lib.php')) {
require_once($plugindir.'/lib.php');
$pluginclass = 'cachestore_'.$plugin;
if (class_exists($pluginclass)) {
$supportsnativelocking = array_key_exists('cache_is_lockable', class_implements($pluginclass));
}
}

if (!$supportsnativelocking) {
$config = cache_config::instance();
$locks = array();
foreach ($config->get_locks() as $lock => $conf) {
debug($conf);
if (!empty($conf['default'])) {
$name = get_string($lock, 'cache');
} else {
$name = $lock;
}
$locks[$lock] = $name;
}
} else {
$locks = false;
}

$url = new moodle_url('/cache/admin.php', array('action' => 'addstore'));
return new $class($url, array('plugin' => $plugin, 'store' => null));
return new $class($url, array('plugin' => $plugin, 'store' => null, 'locks' => $locks));
}

/**
Expand Down Expand Up @@ -821,9 +834,7 @@ public static function get_definition_store_options($component, $area) {
}
}
foreach ($possiblestores as $key => $store) {
if ($key === 'default_locking') {
unset($possiblestores[$key]);
} else if ($store['default']) {
if ($store['default']) {
unset($possiblestores[$key]);
$possiblestores[$key] = $store;
}
Expand Down Expand Up @@ -863,4 +874,34 @@ public static function get_default_mode_stores() {
}
return $modemappings;
}

/**
* Returns an array summarising the locks available in the system
*/
public static function get_lock_summaries() {
$locks = array();
$instance = cache_config::instance();
$stores = $instance->get_all_stores();
foreach ($instance->get_locks() as $lock) {
$default = !empty($lock['default']);
if ($default) {
$name = new lang_string($lock['name'], 'cache');
} else {
$name = $lock['name'];
}
$uses = 0;
foreach ($stores as $store) {
if (!empty($store['lock']) && $store['lock'] === $lock['name']) {
$uses++;
}
}
$lockdata = array(
'name' => $name,
'default' => $default,
'uses' => $uses
);
$locks[] = $lockdata;
}
return $locks;
}
}
45 changes: 45 additions & 0 deletions cache/renderer.php
Expand Up @@ -282,4 +282,49 @@ public function mode_mappings($applicationstore, $sessionstore, $requeststore, m
$html .= html_writer::end_tag('div');
return $html;
}

/**
* Display basic information about lock instances.
*
* @todo Add some actions so that people can configure lock instances.
*
* @param array $locks
* @return string
*/
public function lock_summaries(array $locks) {
$table = new html_table();
$table->colclasses = array(
'name',
'default',
'uses',
// 'actions'
);
$table->rowclasses = array(
'lock_name',
'lock_default',
'lock_uses',
// 'lock_actions',
);
$table->head = array(
get_string('lockname', 'cache'),
get_string('lockdefault', 'cache'),
get_string('lockuses', 'cache'),
// get_string('actions', 'cache')
);
$table->data = array();
$tick = $this->output->pix_icon('i/tick_green_big', '');
foreach ($locks as $lock) {
$table->data[] = new html_table_row(array(
new html_table_cell($lock['name']),
new html_table_cell($lock['default'] ? $tick : ''),
new html_table_cell($lock['uses']),
));
}

$html = html_writer::start_tag('div', array('id' => 'core-cache-lock-summary'));
$html .= $this->output->heading(get_string('locksummary', 'cache'), 3);
$html .= html_writer::table($table);
$html .= html_writer::end_tag('div');
return $html;
}
}
6 changes: 0 additions & 6 deletions cache/tests/cache_test.php
Expand Up @@ -102,21 +102,15 @@ public function test_cache_config() {
}

$definitions = $instance->get_definitions();
// The default locking definition is required for the cache API and must be there.
$this->assertArrayHasKey('core/locking', $definitions);
// The event invalidation definition is required for the cache API and must be there.
$this->assertArrayHasKey('core/eventinvalidation', $definitions);

$definitionmappings = $instance->get_definition_mappings();
// There should be a mapping for default locking to default_locking
$found = false;
foreach ($definitionmappings as $mapping) {
// Required attributes = definition + store
$this->assertArrayHasKey('definition', $mapping);
$this->assertArrayHasKey('store', $mapping);
if ($mapping['store'] == 'default_locking' && $mapping['definition'] == 'core/locking') {
$found = true;
}
}
$this->assertTrue($found, 'The expected mapping for default locking definition to the default locking store was not found.');
}
Expand Down
10 changes: 8 additions & 2 deletions lang/en/cache.php
Expand Up @@ -13,14 +13,14 @@
$string['cachedef_eventinvalidation'] = 'Event invalidation';
$string['cachedef_locking'] = 'Locking';
$string['cachedef_string'] = 'Language string cache';
$string['cachelock_file_default'] = 'Default file locking';
$string['cachestores'] = 'Cache stores';
$string['component'] = 'Component';
$string['confirmstoredeletion'] = 'Confirm store deletion';
$string['defaultmappings'] = 'Default mappings';
$string['defaultmappings_help'] = 'These are the default stores that will be used if you don\'t map one or more stores to the cache definition.';
$string['defaultstoreactions'] = 'Default stores cannot be modified';
$string['default_application'] = 'Default application store';
$string['default_locking'] = 'Default store for locking';
$string['default_request'] = 'Default request store';
$string['default_session'] = 'Default session store';
$string['definition'] = 'Definition';
Expand All @@ -41,6 +41,12 @@
$string['getmiss'] = 'Get - Miss';
$string['invalidplugin'] = 'Invalid plugin';
$string['invalidstore'] = 'Invalid cache store provided';
$string['lockdefault'] = 'Default';
$string['lockmethod'] = 'Lock method';
$string['lockmethod_help'] = 'This is the method used for locking when required of this store.';
$string['lockname'] = 'Name';
$string['locksummary'] = 'Summary of cache lock instances.';
$string['lockuses'] = 'Uses';
$string['mappings'] = 'Store mappings';
$string['mappingdefault'] = '(default)';
$string['mappingprimary'] = 'Primary store';
Expand All @@ -50,6 +56,7 @@
$string['mode_1'] = 'Application';
$string['mode_2'] = 'Session';
$string['mode_4'] = 'Request';
$string['nativelocking'] = 'This plugin handles its own locking.';
$string['none'] = 'None';
$string['plugin'] = 'Plugin';
$string['pluginsummaries'] = 'Plugin summaries';
Expand All @@ -70,7 +77,6 @@
$string['storeresults_session'] = 'Store requests when used as a session cache.';
$string['stores'] = 'Stores';
$string['store_default_application'] = 'Default file store for application caches';
$string['store_default_locking'] = 'Default file store for locking';
$string['store_default_request'] = 'Default static store for request caches';
$string['store_default_session'] = 'Default session store for session caches';
$string['storesummaries'] = 'Store summaries';
Expand Down

0 comments on commit 167ad91

Please sign in to comment.