From 5d5209affedec6a444930b9953b73baff44274ca Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Fri, 12 Aug 2022 17:46:00 +0200 Subject: [PATCH] Add cache for page objects in menus If several articles were linked in the menu, this led to a lot of SQL queries. --- .../install/files/lib/data/menu/item/MenuItem.class.php | 7 +++++++ .../files/lib/data/menu/item/MenuItemNodeTree.class.php | 2 ++ .../system/page/handler/AbstractMenuPageHandler.class.php | 7 +++++++ .../lib/system/page/handler/ArticlePageHandler.class.php | 8 ++++++++ .../lib/system/page/handler/IMenuPageHandler.class.php | 7 +++++++ 5 files changed, 31 insertions(+) diff --git a/wcfsetup/install/files/lib/data/menu/item/MenuItem.class.php b/wcfsetup/install/files/lib/data/menu/item/MenuItem.class.php index 272d0026df..d97018c7d8 100644 --- a/wcfsetup/install/files/lib/data/menu/item/MenuItem.class.php +++ b/wcfsetup/install/files/lib/data/menu/item/MenuItem.class.php @@ -191,4 +191,11 @@ class MenuItem extends DatabaseObject implements ITitledObject return $this->handler; } + + public function cachePageObject(): void + { + if ($this->pageObjectID && $this->getMenuPageHandler() !== null) { + $this->getMenuPageHandler()->cacheObject($this->pageObjectID); + } + } } diff --git a/wcfsetup/install/files/lib/data/menu/item/MenuItemNodeTree.class.php b/wcfsetup/install/files/lib/data/menu/item/MenuItemNodeTree.class.php index 454617eee4..947ccd50f2 100644 --- a/wcfsetup/install/files/lib/data/menu/item/MenuItemNodeTree.class.php +++ b/wcfsetup/install/files/lib/data/menu/item/MenuItemNodeTree.class.php @@ -93,6 +93,8 @@ class MenuItemNodeTree // build menu structure foreach ($menuItemList as $menuItem) { + $menuItem->cachePageObject(); + $this->menuItems[$menuItem->itemID] = $menuItem; if (!isset($this->menuItemStructure[$menuItem->parentItemID])) { diff --git a/wcfsetup/install/files/lib/system/page/handler/AbstractMenuPageHandler.class.php b/wcfsetup/install/files/lib/system/page/handler/AbstractMenuPageHandler.class.php index 0b4725ffd9..a0d5892e4c 100644 --- a/wcfsetup/install/files/lib/system/page/handler/AbstractMenuPageHandler.class.php +++ b/wcfsetup/install/files/lib/system/page/handler/AbstractMenuPageHandler.class.php @@ -31,4 +31,11 @@ abstract class AbstractMenuPageHandler implements IMenuPageHandler { return true; } + + /** + * @inheritDoc + */ + public function cacheObject(int $objectID): void + { + } } diff --git a/wcfsetup/install/files/lib/system/page/handler/ArticlePageHandler.class.php b/wcfsetup/install/files/lib/system/page/handler/ArticlePageHandler.class.php index d44f58f773..caac8b5722 100644 --- a/wcfsetup/install/files/lib/system/page/handler/ArticlePageHandler.class.php +++ b/wcfsetup/install/files/lib/system/page/handler/ArticlePageHandler.class.php @@ -119,4 +119,12 @@ class ArticlePageHandler extends AbstractLookupPageHandler implements IOnlineLoc ViewableArticleContentRuntimeCache::getInstance()->cacheObjectID($user->pageObjectID); } } + + /** + * @inheritDoc + */ + public function cacheObject(int $objectID): void + { + ViewableArticleRuntimeCache::getInstance()->cacheObjectID($objectID); + } } diff --git a/wcfsetup/install/files/lib/system/page/handler/IMenuPageHandler.class.php b/wcfsetup/install/files/lib/system/page/handler/IMenuPageHandler.class.php index 54c81f3da9..9b25cb1bf2 100644 --- a/wcfsetup/install/files/lib/system/page/handler/IMenuPageHandler.class.php +++ b/wcfsetup/install/files/lib/system/page/handler/IMenuPageHandler.class.php @@ -30,4 +30,11 @@ interface IMenuPageHandler * @return bool false if the page should be hidden from menus */ public function isVisible($objectID = null); + + /** + * Caches the given object id to save SQL queries if multiple objects of the same type are queried in the menu. + * + * @since 6.0 + */ + public function cacheObject(int $objectID): void; } -- 2.20.1