Skip to content

Commit

Permalink
MDL-25290 cache: Fixed things up per coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Oct 7, 2012
1 parent bb250f0 commit 170f821
Show file tree
Hide file tree
Showing 32 changed files with 245 additions and 156 deletions.
24 changes: 14 additions & 10 deletions cache/admin.php
Expand Up @@ -48,11 +48,11 @@

if (!empty($action) && confirm_sesskey()) {
switch ($action) {
case 'rescandefinitions' :
case 'rescandefinitions' : // Rescan definitions.
cache_config_writer::update_definitions();
redirect($PAGE->url);
break;
case 'addstore' :
case 'addstore' : // Add the requested store.
$plugin = required_param('plugin', PARAM_PLUGIN);
$mform = cache_administration_helper::get_add_store_form($plugin);
$title = get_string('addstore', 'cache', $plugins[$plugin]['name']);
Expand All @@ -71,7 +71,7 @@
redirect($PAGE->url, get_string('addstoresuccess', 'cache', $plugins[$plugin]['name']), 5);
}
break;
case 'editstore' :
case 'editstore' : // Edit the requested store.
$plugin = required_param('plugin', PARAM_PLUGIN);
$store = required_param('store', PARAM_TEXT);
$mform = cache_administration_helper::get_edit_store_form($plugin, $store);
Expand All @@ -91,7 +91,7 @@
redirect($PAGE->url, get_string('editstoresuccess', 'cache', $plugins[$plugin]['name']), 5);
}
break;
case 'deletestore' :
case 'deletestore' : // Delete a given store.
$store = required_param('store', PARAM_TEXT);
$confirm = optional_param('confirm', false, PARAM_BOOL);

Expand All @@ -106,14 +106,16 @@
if ($notifysuccess) {
if (!$confirm) {
$title = get_string('confirmstoredeletion', 'cache');
$url = new moodle_url($PAGE->url, array('store' => $store, 'confirm' => 1, 'action' => $action, 'sesskey' => sesskey()));
$params = array('store' => $store, 'confirm' => 1, 'action' => $action, 'sesskey' => sesskey());
$url = new moodle_url($PAGE->url, $params);
$button = new single_button($url, get_string('deletestore', 'cache'));

$PAGE->set_title($title);
$PAGE->set_heading($SITE->fullname);
echo $OUTPUT->header();
echo $OUTPUT->heading($title);
echo $OUTPUT->confirm(get_string('deletestoreconfirmation', 'cache', $stores[$store]['name']), $button, $PAGE->url);
$confirmation = get_string('deletestoreconfirmation', 'cache', $stores[$store]['name']);
echo $OUTPUT->confirm($confirmation, $button, $PAGE->url);
echo $OUTPUT->footer();
exit;
} else {
Expand All @@ -123,7 +125,7 @@
}
}
break;
case 'editdefinitionmapping' :
case 'editdefinitionmapping' : // Edit definition mappings.
$definition = required_param('definition', PARAM_TEXT);
$title = get_string('editdefinitionmappings', 'cache', $definition);
$mform = new cache_definition_mappings_form($PAGE->url, array('definition' => $definition));
Expand All @@ -141,7 +143,7 @@
redirect($PAGE->url);
}
break;
case 'editmodemappings': // Edit default mode mappings
case 'editmodemappings': // Edit default mode mappings.
$mform = new cache_mode_mappings_form(null, $stores);
$mform->set_data(array(
'mode_'.cache_store::MODE_APPLICATION => key($defaultmodestores[cache_store::MODE_APPLICATION]),
Expand All @@ -162,8 +164,10 @@
}
break;

case 'purge': // Purge a store cache
// TODO
case 'purge': // Purge a store cache.
$store = required_param('store', PARAM_TEXT);
cache_helper::purge_store($store);
redirect($PAGE->url, get_string('purgestoresuccess', 'cache'), 5);
break;
}
}
Expand Down
41 changes: 21 additions & 20 deletions cache/classes/config.php
Expand Up @@ -75,7 +75,7 @@ class cache_config {
* Please use cache_config::instance to get an instance of the cache config that is ready to be used.
*/
public function __construct() {
//$this->config_load();
// Nothing to do here but look pretty.
}

/**
Expand All @@ -94,7 +94,7 @@ public static function instance() {
* @return bool True if it exists
*/
public static function config_file_exists() {
// Allow for late static binding
// Allow for late static binding.
return file_exists(self::get_config_file_path());
}

Expand Down Expand Up @@ -136,15 +136,15 @@ public function load() {
$this->configdefinitionmappings = array();
$this->configlockmappings = array();

// Filter the lock instances
// Filter the lock instances.
$defaultlock = null;
foreach ($configuration['locks'] as $conf) {
if (!is_array($conf)) {
// Something is very wrong here.
continue;
}
if (!array_key_exists('name', $conf)) {
// Not a valid definition configuration
// Not a valid definition configuration.
continue;
}
$name = $conf['name'];
Expand All @@ -159,17 +159,18 @@ public function load() {
$this->configlocks[$name] = $conf;
}

// Filter the stores
// Filter the stores.
$availableplugins = cache_helper::early_get_cache_plugins();
foreach ($configuration['stores'] as $store) {
if (!is_array($store) || !array_key_exists('name', $store) || !array_key_exists('plugin', $store)) {
// Not a valid instance configuration
// Not a valid instance configuration.
debugging('Invalid cache store in config. Missing name or plugin.', DEBUG_DEVELOPER);
continue;
}
$plugin = $store['plugin'];
$class = 'cachestore_'.$plugin;
if (!array_key_exists($plugin, $availableplugins) && (!class_exists($class) || !is_subclass_of($class, 'cache_store'))) {
$exists = array_key_exists($plugin, $availableplugins);
if (!$exists && (!class_exists($class) || !is_subclass_of($class, 'cache_store'))) {
// Not a valid plugin, or has been uninstalled, just skip it an carry on.
debugging('Invalid cache store in config. Not an available plugin.', DEBUG_DEVELOPER);
continue;
Expand All @@ -196,14 +197,14 @@ public function load() {
$this->configstores[$store['name']] = $store;
}

// Filter the definitions
// Filter the definitions.
foreach ($configuration['definitions'] as $id => $conf) {
if (!is_array($conf)) {
// Something is very wrong here.
continue;
}
if (!array_key_exists('mode', $conf) || !array_key_exists('component', $conf) || !array_key_exists('area', $conf)) {
// Not a valid definition configuration
// Not a valid definition configuration.
continue;
}
if (array_key_exists($id, $this->configdefinitions)) {
Expand All @@ -212,27 +213,27 @@ public function load() {
}
$conf['mode'] = (int)$conf['mode'];
if ($conf['mode'] < cache_store::MODE_APPLICATION || $conf['mode'] > cache_store::MODE_REQUEST) {
// Invalid cache mode used for the definition
// Invalid cache mode used for the definition.
continue;
}
$this->configdefinitions[$id] = $conf;
}

// Filter the mode mappings
// Filter the mode mappings.
foreach ($configuration['modemappings'] as $mapping) {
if (!is_array($mapping) || !array_key_exists('mode', $mapping) || !array_key_exists('store', $mapping)) {
// Not a valid mapping configuration
// Not a valid mapping configuration.
debugging('A cache mode mapping entry is invalid.', DEBUG_DEVELOPER);
continue;
}
if (!array_key_exists($mapping['store'], $this->configstores)) {
// Mapped array instance doesn't exist
// Mapped array instance doesn't exist.
debugging('A cache mode mapping exists for a mode or store that does not exist.', DEBUG_DEVELOPER);
continue;
}
$mapping['mode'] = (int)$mapping['mode'];
if ($mapping['mode'] < 0 || $mapping['mode'] > 4) {
// Invalid cache type used for the mapping
// Invalid cache type used for the mapping.
continue;
}
if (!array_key_exists('sort', $mapping)) {
Expand All @@ -241,18 +242,18 @@ public function load() {
$this->configmodemappings[] = $mapping;
}

// Filter the definition mappings
// Filter the definition mappings.
foreach ($configuration['definitionmappings'] as $mapping) {
if (!is_array($mapping) || !array_key_exists('definition', $mapping) || !array_key_exists('store', $mapping)) {
// Not a valid mapping configuration
// Not a valid mapping configuration.
continue;
}
if (!array_key_exists($mapping['store'], $this->configstores)) {
// Mapped array instance doesn't exist
// Mapped array instance doesn't exist.
continue;
}
if (!array_key_exists($mapping['definition'], $this->configdefinitions)) {
// Mapped array instance doesn't exist
// Mapped array instance doesn't exist.
continue;
}
if (!array_key_exists('sort', $mapping)) {
Expand Down Expand Up @@ -280,7 +281,7 @@ protected function include_configuration() {
$cachefile = self::get_config_file_path();

if (!file_exists($cachefile)) {
throw new cache_exception('Default cache configuration could not be found. It should have already been created by now.');
throw new cache_exception('Default cache config could not be found. It should have already been created by now.');
}
include($cachefile);
if (!is_array($configuration)) {
Expand Down Expand Up @@ -318,7 +319,7 @@ protected function sort_mappings(array $a, array $b) {
if ($a['sort'] == $b['sort']) {
return 0;
}
return ($a['sort'] < $b['sort']) ? 1 : -1 ;
return ($a['sort'] < $b['sort']) ? 1 : -1;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion cache/classes/definition.php
Expand Up @@ -278,7 +278,7 @@ public static function load($id, array $definition, $datasourceaggregate = null)
$component = (string)$definition['component'];
$area = (string)$definition['area'];

// Set the defaults
// Set the defaults.
$requireidentifiers = array();
$requiredataguarantee = false;
$requiremultipleidentifiers = false;
Expand Down
4 changes: 2 additions & 2 deletions cache/classes/dummystore.php
Expand Up @@ -38,7 +38,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cachestore_dummy implements cache_store {

/**
* The name of this store.
* @var string
Expand All @@ -51,7 +51,7 @@ class cachestore_dummy implements cache_store {
* @var bool
*/
protected $persist = false;

/**
* The persistent store array
* @var array
Expand Down
7 changes: 4 additions & 3 deletions cache/classes/factory.php
Expand Up @@ -209,7 +209,7 @@ public function create_cache(cache_definition $definition) {
*/
public function create_store_from_config($name, array $details, cache_definition $definition) {
if (!array_key_exists($name, $this->stores)) {
// name, plugin, configuration, class
// Properties: name, plugin, configuration, class.
$class = $details['class'];
$store = new $class($details['name'], $details['configuration']);
$this->stores[$name] = $store;
Expand Down Expand Up @@ -241,7 +241,7 @@ public function create_config_instance($writer = false) {
require_once($CFG->dirroot.'/cache/locallib.php');
$class .= '_writer';
}

// Check if this is a PHPUnit test and redirect to the phpunit config classes if it is.
if (defined('PHPUNIT_TEST') && PHPUNIT_TEST) {
require_once($CFG->dirroot.'/cache/locallib.php');
Expand Down Expand Up @@ -290,7 +290,8 @@ public function create_definition($component, $area, $aggregate = null) {
if (!$definition) {
throw new coding_exception('The requested cache definition does not exist.'. $id, $id);
} else {
debugging('Cache definitions reparsed causing cache reset in order to locate definition. You should bump the version number to ensure definitions are reprocessed.', DEBUG_DEVELOPER);
debugging('Cache definitions reparsed causing cache reset in order to locate definition.
You should bump the version number to ensure definitions are reprocessed.', DEBUG_DEVELOPER);
}
}
$this->definitions[$id] = cache_definition::load($id, $definition, $aggregate);
Expand Down
31 changes: 28 additions & 3 deletions cache/classes/helper.php
Expand Up @@ -187,7 +187,7 @@ public static function early_get_cache_plugins() {
}
$pluginname = clean_param($pluginname, PARAM_PLUGIN);
if (empty($pluginname)) {
// better ignore plugins with problematic names here
// Better ignore plugins with problematic names here.
continue;
}
$result[$pluginname] = $fulldir.'/'.$pluginname;
Expand Down Expand Up @@ -276,7 +276,7 @@ public static function invalidate_by_event($event, array $keys) {
* @return bool
*/
public static function purge_by_definition($component, $area, array $identifiers = array()) {
// Create the cache
// Create the cache.
$cache = cache::make($component, $area, $identifiers);
// Purge baby, purge.
$cache->purge();
Expand All @@ -303,7 +303,7 @@ public static function purge_by_event($event) {
if ($invalidationeventset === false) {
// Get the event invalidation cache.
$cache = cache::make('core', 'eventinvalidation');
// Create a key to invalidate all
// Create a key to invalidate all.
$data = array(
'purged' => cache::now()
);
Expand Down Expand Up @@ -402,6 +402,31 @@ public static function purge_all() {
}
}

/**
* Purges a store given its name.
*
* @param string $storename
* @return bool
*/
public static function purge_store($storename) {
$config = cache_config::instance();
foreach ($config->get_all_stores() as $store) {
if ($store['name'] !== $storename) {
continue;
}
$class = $store['class'];
$instance = new $class($store['name'], $store['configuration']);
if (!$instance->is_ready()) {
continue;
}
$definition = cache_definition::load_adhoc(cache_store::MODE_REQUEST, 'core', 'cache_purge');
$instance->initialise($definition);
$instance->purge();
return true;
}
return false;
}

/**
* Returns the translated name of the definition.
*
Expand Down

0 comments on commit 170f821

Please sign in to comment.