Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-34401 Replace static variables in pluginlib with ad-hoc request c…

…aches

Where the static variable was not really needed (as in case of arrays
defined by the hard-coded list of items), non-static variable is used as
I believe that there is no real performance gain.
  • Loading branch information...
commit b8a6f26ee3b73b966fdeeb3f958028a49303ee6a 1 parent 07622d7
@mudrd8mz mudrd8mz authored
Showing with 97 additions and 59 deletions.
  1. +97 −59 lib/pluginlib.php
View
156 lib/pluginlib.php
@@ -360,8 +360,17 @@ public function some_plugins_updatable() {
* @return bool
*/
public static function is_deleted_standard_plugin($type, $name) {
- static $plugins = array(
- // do not add 1.9-2.2 plugin removals here
+
+ // Example of the array structure:
+ // $plugins = array(
+ // 'block' => array('admin', 'admin_tree'),
+ // 'mod' => array('assignment'),
+ // );
+ // Do not include plugins that were removed during upgrades to versions that are
+ // not supported as source versions for upgrade any more. For example, at MOODLE_23_STABLE
+ // branch, listed should be no plugins that were removed at 1.9.x - 2.1.x versions as
+ // Moodle 2.3 supports upgrades from 2.2.x only.
+ $plugins = array(
'qformat' => array('blackboard'),
);
@@ -378,7 +387,7 @@ public static function is_deleted_standard_plugin($type, $name) {
* @return false|array array of standard plugins or false if the type is unknown
*/
public static function standard_plugins_list($type) {
- static $standard_plugins = array(
+ $standard_plugins = array(
'assignment' => array(
'offline', 'online', 'upload', 'uploadsingle'
@@ -2461,30 +2470,35 @@ public function get_dir() {
}
/**
- * Provides access to plugin versions from {config_plugins}
+ * Provides access to plugin versions from the {config_plugins} table
*
* @param string $plugin plugin name
- * @param double $disablecache optional, defaults to false
- * @return int|false the stored value or false if not found
+ * @param bool $disablecache do not attempt to obtain data from the cache
+ * @return int|bool the stored value or false if not found
*/
protected function get_version_from_config_plugins($plugin, $disablecache=false) {
global $DB;
- static $pluginversions = null;
- if (is_null($pluginversions) or $disablecache) {
+ $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'core_plugin', 'plugininfo_base',
+ array(), array('simplekeys' => true, 'simpledata' => true));
+
+ $pluginversions = $cache->get('versions_db');
+
+ if ($pluginversions === false or $disablecache) {
try {
$pluginversions = $DB->get_records_menu('config_plugins', array('name' => 'version'), 'plugin', 'plugin,value');
} catch (dml_exception $e) {
// before install
$pluginversions = array();
}
+ $cache->set('versions_db', $pluginversions);
}
- if (!array_key_exists($plugin, $pluginversions)) {
+ if (isset($pluginversions[$plugin])) {
+ return $pluginversions[$plugin];
+ } else {
return false;
}
-
- return $pluginversions[$plugin];
}
}
@@ -2621,23 +2635,28 @@ public function get_uninstall_url() {
/**
* Provides access to the records in {block} table
*
- * @param bool $disablecache do not use internal static cache
+ * @param bool $disablecache do not attempt to obtain data from the cache
* @return array array of stdClasses
*/
protected static function get_blocks_info($disablecache=false) {
global $DB;
- static $blocksinfocache = null;
- if (is_null($blocksinfocache) or $disablecache) {
+ $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'core_plugin', 'plugininfo_block',
+ array(), array('simplekeys' => true, 'simpledata' => true));
+
+ $blocktypes = $cache->get('blocktypes');
+
+ if ($blocktypes === false or $disablecache) {
try {
- $blocksinfocache = $DB->get_records('block', null, 'name', 'name,id,version,visible');
+ $blocktypes = $DB->get_records('block', null, 'name', 'name,id,version,visible');
} catch (dml_exception $e) {
// before install
- $blocksinfocache = array();
+ $blocktypes = array();
}
+ $cache->set('blocktypes', $blocktypes);
}
- return $blocksinfocache;
+ return $blocktypes;
}
}
@@ -2761,35 +2780,43 @@ public function get_uninstall_url() {
*
* The legacy filter name is available as ->legacyname property.
*
- * @param bool $disablecache
+ * @param bool $disablecache do not attempt to obtain data from the cache
* @return array
*/
protected static function get_global_states($disablecache=false) {
global $DB;
- static $globalstatescache = null;
- if ($disablecache or is_null($globalstatescache)) {
- $globalstatescache = array();
+ $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'core_plugin', 'plugininfo_filter',
+ array(), array('simplekeys' => true, 'simpledata' => true));
+
+ $globalstates = $cache->get('globalstates');
+
+ if ($globalstates === false or $disablecache) {
if (!$DB->get_manager()->table_exists('filter_active')) {
// Not installed yet.
- return $globalstatescache;
+ $cache->set('globalstates', array());
+ return array();
}
+ $globalstates = array();
+
foreach (filter_get_global_states() as $name => $info) {
if (strpos($name, '/') !== false) {
// Skip existing before upgrade to new names.
continue;
}
- $filterinfo = new stdClass();
- $filterinfo->active = $info->active;
- $filterinfo->sortorder = $info->sortorder;
- $globalstatescache[$name] = $filterinfo;
+ $filterinfo = new stdClass();
+ $filterinfo->active = $info->active;
+ $filterinfo->sortorder = $info->sortorder;
+ $globalstates[$name] = $filterinfo;
}
+
+ $cache->set('globalstates', $globalstates);
}
- return $globalstatescache;
+ return $globalstates;
}
}
@@ -2925,23 +2952,28 @@ public function get_uninstall_url() {
/**
* Provides access to the records in {modules} table
*
- * @param bool $disablecache do not use internal static cache
+ * @param bool $disablecache do not attempt to obtain data from the cache
* @return array array of stdClasses
*/
protected static function get_modules_info($disablecache=false) {
global $DB;
- static $modulesinfocache = null;
- if (is_null($modulesinfocache) or $disablecache) {
+ $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'core_plugin', 'plugininfo_mod',
+ array(), array('simplekeys' => true, 'simpledata' => true));
+
+ $modulesinfo = $cache->get('modulesinfo');
+
+ if ($modulesinfo === false or $disablecache) {
try {
- $modulesinfocache = $DB->get_records('modules', null, 'name', 'name,id,version,visible');
+ $modulesinfo = $DB->get_records('modules', null, 'name', 'name,id,version,visible');
} catch (dml_exception $e) {
// before install
- $modulesinfocache = array();
+ $modulesinfo = array();
}
+ $cache->set('modulesinfo', $modulesinfo);
}
- return $modulesinfocache;
+ return $modulesinfo;
}
}
@@ -3000,17 +3032,13 @@ class plugininfo_auth extends plugininfo_base {
public function is_enabled() {
global $CFG;
- /** @var null|array list of enabled authentication plugins */
- static $enabled = null;
if (in_array($this->name, array('nologin', 'manual'))) {
// these two are always enabled and can't be disabled
return null;
}
- if (is_null($enabled)) {
- $enabled = array_flip(explode(',', $CFG->auth));
- }
+ $enabled = array_flip(explode(',', $CFG->auth));
return isset($enabled[$this->name]);
}
@@ -3052,17 +3080,13 @@ class plugininfo_enrol extends plugininfo_base {
public function is_enabled() {
global $CFG;
- /** @var null|array list of enabled enrolment plugins */
- static $enabled = null;
// We do not actually need whole enrolment classes here so we do not call
// {@link enrol_get_plugins()}. Note that this may produce slightly different
// results, for example if the enrolment plugin does not contain lib.php
// but it is listed in $CFG->enrol_plugins_enabled
- if (is_null($enabled)) {
- $enabled = array_flip(explode(',', $CFG->enrol_plugins_enabled));
- }
+ $enabled = array_flip(explode(',', $CFG->enrol_plugins_enabled));
return isset($enabled[$this->name]);
}
@@ -3190,18 +3214,23 @@ public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $h
/**
* Provides access to the records in {repository} table
*
- * @param bool $disablecache do not use internal static cache
+ * @param bool $disablecache do not attempt to obtain data from the cache
* @return array array of stdClasses
*/
protected static function get_enabled_repositories($disablecache=false) {
global $DB;
- static $repositories = null;
- if (is_null($repositories) or $disablecache) {
- $repositories = $DB->get_records('repository', null, 'type', 'type,visible,sortorder');
+ $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'core_plugin', 'plugininfo_repository',
+ array(), array('simplekeys' => true, 'simpledata' => true));
+
+ $enabled = $cache->get('enabled');
+
+ if ($enabled === false or $disablecache) {
+ $enabled = $DB->get_records('repository', null, 'type', 'type,visible,sortorder');
+ $cache->set('enabled', $enabled);
}
- return $repositories;
+ return $enabled;
}
}
@@ -3219,30 +3248,39 @@ public function is_enabled() {
}
/**
- * Provides access to the records in {portfolio_instance} table
+ * Returns list of enabled portfolio plugins
*
- * @param bool $disablecache do not use internal static cache
- * @return array array of stdClasses
+ * Portfolio plugin is enabled if there is at least one record in the {portfolio_instance}
+ * table for it.
+ *
+ * @param bool $disablecache do not attempt to obtain data from the cache
+ * @return array array of stdClasses with properties plugin and visible indexed by plugin
*/
protected static function get_enabled_portfolios($disablecache=false) {
global $DB;
- static $portfolios = null;
- if (is_null($portfolios) or $disablecache) {
- $portfolios = array();
- $instances = $DB->get_recordset('portfolio_instance', null, 'plugin');
+ $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'core_plugin', 'plugininfo_portfolio',
+ array(), array('simplekeys' => true, 'simpledata' => true));
+
+ $enabled = $cache->get('enabled');
+
+ if ($enabled === false or $disablecache) {
+ $enabled = array();
+ $instances = $DB->get_recordset('portfolio_instance', null, '', 'plugin,visible');
foreach ($instances as $instance) {
- if (isset($portfolios[$instance->plugin])) {
+ if (isset($enabled[$instance->plugin])) {
if ($instance->visible) {
- $portfolios[$instance->plugin]->visible = $instance->visible;
+ $enabled[$instance->plugin]->visible = $instance->visible;
}
} else {
- $portfolios[$instance->plugin] = $instance;
+ $enabled[$instance->plugin] = $instance;
}
}
+ $instances->close();
+ $cache->set('enabled', $enabled);
}
- return $portfolios;
+ return $enabled;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.