diff --git a/lang/en/cache.php b/lang/en/cache.php index 441b1910acf..73acbeb0803 100644 --- a/lang/en/cache.php +++ b/lang/en/cache.php @@ -65,6 +65,7 @@ $string['cachedef_observers'] = 'Event observers'; $string['cachedef_plugin_functions'] = 'Plugins available callbacks'; $string['cachedef_plugin_manager'] = 'Plugin info manager'; $string['cachedef_presignup'] = 'Pre sign-up data for particular unregistered user'; +$string['cachedef_portfolio_add_button_portfolio_instances'] = 'Portfolio instances for portfolio_add_button class'; $string['cachedef_postprocessedcss'] = 'Post processed CSS'; $string['cachedef_tagindexbuilder'] = 'Search results for tagged items'; $string['cachedef_questiondata'] = 'Question definitions'; diff --git a/lib/db/caches.php b/lib/db/caches.php index 71ebe33add0..7247f54386a 100644 --- a/lib/db/caches.php +++ b/lib/db/caches.php @@ -391,4 +391,13 @@ $definitions = array( 'simplekeys' => true, 'simpledata' => true, ), + + // Cache the list of portfolio instances for the logged in user + // in the portfolio_add_button constructor to avoid loading the + // same data multiple times. + 'portfolio_add_button_portfolio_instances' => [ + 'mode' => cache_store::MODE_REQUEST, + 'simplekeys' => true, + 'staticacceleration' => true + ], ); diff --git a/lib/portfoliolib.php b/lib/portfoliolib.php index 68d5fe500bf..cc00cc5b8a2 100644 --- a/lib/portfoliolib.php +++ b/lib/portfoliolib.php @@ -117,7 +117,14 @@ class portfolio_add_button { debugging('Building portfolio add button while portfolios is disabled. This code can be optimised.', DEBUG_DEVELOPER); } - $this->instances = portfolio_instances(); + $cache = cache::make('core', 'portfolio_add_button_portfolio_instances'); + $instances = $cache->get('instances'); + if ($instances === false) { + $instances = portfolio_instances(); + $cache->set('instances', $instances); + } + + $this->instances = $instances; if (empty($options)) { return true; }