Skip to content

Commit

Permalink
Enable caching of menu data in JMenuSite (#10797)
Browse files Browse the repository at this point in the history
* Enable caching of menu data in JMenuSite

* Use a lambda function for the query instead
  • Loading branch information
Michael Babker authored and wilsonge committed Sep 6, 2016
1 parent d6dcc76 commit d41e779
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
1 change: 1 addition & 0 deletions administrator/components/com_menus/models/item.php
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,7 @@ protected function generateNewTitle($parent_id, $alias, $title)
*/
protected function cleanCache($group = null, $client_id = 0)
{
parent::cleanCache('com_menus', 0);
parent::cleanCache('com_modules');
parent::cleanCache('mod_menu');
}
Expand Down
1 change: 1 addition & 0 deletions administrator/components/com_menus/models/menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ public function &getModules()
*/
protected function cleanCache($group = null, $client_id = 0)
{
parent::cleanCache('com_menus', 0);
parent::cleanCache('com_modules');
parent::cleanCache('mod_menu');
}
Expand Down
43 changes: 28 additions & 15 deletions libraries/cms/menu/site.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,37 @@ public function __construct($options = array())
*/
public function load()
{
$db = $this->db;
$query = $db->getQuery(true)
->select('m.id, m.menutype, m.title, m.alias, m.note, m.path AS route, m.link, m.type, m.level, m.language')
->select($db->quoteName('m.browserNav') . ', m.access, m.params, m.home, m.img, m.template_style_id, m.component_id, m.parent_id')
->select('e.element as component')
->from('#__menu AS m')
->join('LEFT', '#__extensions AS e ON m.component_id = e.extension_id')
->where('m.published = 1')
->where('m.parent_id > 0')
->where('m.client_id = 0')
->order('m.lft');

// Set the query
$db->setQuery($query);
// For PHP 5.3 compat we can't use $this in the lambda function below
$db = $this->db;

try
{
$this->_items = $db->loadObjectList('id');
/** @var JCacheControllerCallback $cache */
$cache = JFactory::getCache('com_menus', 'callback');

$this->_items = $cache->get(
function () use ($db)
{
$query = $db->getQuery(true)
->select('m.id, m.menutype, m.title, m.alias, m.note, m.path AS route, m.link, m.type, m.level, m.language')
->select($db->quoteName('m.browserNav') . ', m.access, m.params, m.home, m.img, m.template_style_id, m.component_id, m.parent_id')
->select('e.element as component')
->from('#__menu AS m')
->join('LEFT', '#__extensions AS e ON m.component_id = e.extension_id')
->where('m.published = 1')
->where('m.parent_id > 0')
->where('m.client_id = 0')
->order('m.lft');

// Set the query
$db->setQuery($query);

return $db->loadObjectList('id');
},
array(),
md5(get_class($this)),
false
);
}
catch (RuntimeException $e)
{
Expand Down

0 comments on commit d41e779

Please sign in to comment.