Skip to content

Commit

Permalink
MDL-78297 core: Less cache lookups in deprecated capabilities feature
Browse files Browse the repository at this point in the history
Avoid use of $cache->has.
  • Loading branch information
MartinGauk committed Jun 7, 2023
1 parent 84bb572 commit 695c74a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
1 change: 0 additions & 1 deletion lang/en/cache.php
Expand Up @@ -57,7 +57,6 @@
$string['cachedef_course_image'] = 'Course images';
$string['cachedef_course_user_dates'] = 'The user dates for courses set to relative dates mode';
$string['cachedef_completion'] = 'Activity completion status';
$string['cachedef_deprecatedcapabilities'] = 'System deprecated capabilities list';
$string['cachedef_databasemeta'] = 'Database meta information';
$string['cachedef_eventinvalidation'] = 'Event invalidation';
$string['cachedef_externalbadges'] = 'External badges for particular user';
Expand Down
20 changes: 9 additions & 11 deletions lib/accesslib.php
Expand Up @@ -2608,13 +2608,10 @@ function get_capability_info($capabilityname) {
* @return stdClass|null with deprecation message and potential replacement if not null
*/
function get_deprecated_capability_info($capabilityname) {
// Here if we do like get_all_capabilities, we run into performance issues as the full array is unserialised each time.
// We could have used an adhoc task but this also had performance issue. Last solution was to create a cache using
// the official caches.php file. The performance issue shows in test_permission_evaluation.
$cache = cache::make('core', 'deprecatedcapabilities');
// Cache has not be initialised.
if (!$cache->get('deprecated_capabilities_initialised')) {
// Look for deprecated capabilities in each components.
$cache = cache::make('core', 'capabilities');
$alldeprecatedcaps = $cache->get('deprecated_capabilities');
if ($alldeprecatedcaps === false) {
// Look for deprecated capabilities in each component.
$allcaps = get_all_capabilities();
$components = [];
$alldeprecatedcaps = [];
Expand All @@ -2627,18 +2624,19 @@ function get_deprecated_capability_info($capabilityname) {
require($defpath);
if (!empty($deprecatedcapabilities)) {
foreach ($deprecatedcapabilities as $cname => $cdef) {
$cache->set($cname, $cdef);
$alldeprecatedcaps[$cname] = $cdef;
}
}
}
}
}
$cache->set('deprecated_capabilities_initialised', true);
$cache->set('deprecated_capabilities', $alldeprecatedcaps);
}
if (!$cache->has($capabilityname)) {

if (!isset($alldeprecatedcaps[$capabilityname])) {
return null;
}
$deprecatedinfo = $cache->get($capabilityname);
$deprecatedinfo = $alldeprecatedcaps[$capabilityname];
$deprecatedinfo['fullmessage'] = "The capability '{$capabilityname}' is deprecated.";
if (!empty($deprecatedinfo['message'])) {
$deprecatedinfo['fullmessage'] .= $deprecatedinfo['message'];
Expand Down
14 changes: 2 additions & 12 deletions lib/db/caches.php
Expand Up @@ -160,23 +160,13 @@
'ttl' => 900,
),

// Cache the capabilities list DB table. See get_all_capabilities in accesslib.
// Cache the capabilities list DB table. See get_all_capabilities and get_deprecated_capability_info in accesslib.
'capabilities' => array(
'mode' => cache_store::MODE_APPLICATION,
'simplekeys' => true,
'simpledata' => true,
'staticacceleration' => true,
'staticaccelerationsize' => 1,
'ttl' => 3600, // Just in case.
),

// Cache the deprecated capabilities list. See get_deprecated_capability_info in accesslib.
'deprecatedcapabilities' => array(
'mode' => cache_store::MODE_APPLICATION,
'simplekeys' => false, // We need to hash the key.
'simpledata' => true,
'staticacceleration' => true,
'staticaccelerationsize' => 1,
'staticaccelerationsize' => 2, // Should be main capabilities and deprecated capabilities.
'ttl' => 3600, // Just in case.
),

Expand Down

0 comments on commit 695c74a

Please sign in to comment.