Add cache for page objects in menus
authorMarcel Werk <burntime@woltlab.com>
Fri, 12 Aug 2022 15:46:00 +0000 (17:46 +0200)
committerMarcel Werk <burntime@woltlab.com>
Fri, 12 Aug 2022 15:46:00 +0000 (17:46 +0200)
If several articles were linked in the menu, this led to a lot of SQL queries.

wcfsetup/install/files/lib/data/menu/item/MenuItem.class.php
wcfsetup/install/files/lib/data/menu/item/MenuItemNodeTree.class.php
wcfsetup/install/files/lib/system/page/handler/AbstractMenuPageHandler.class.php
wcfsetup/install/files/lib/system/page/handler/ArticlePageHandler.class.php
wcfsetup/install/files/lib/system/page/handler/IMenuPageHandler.class.php

index 272d0026df00a373af6405d36e4e08cdf87c3595..d97018c7d8539736dc1666cdc0eb75bbae8668ef 100644 (file)
@@ -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);
+        }
+    }
 }
index 454617eee4bf27c3092136b8ab27bfbc230b5591..947ccd50f202723ff92c709bcff1f578e6b679a1 100644 (file)
@@ -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])) {
index 0b4725ffd984f7948c05574d24331b93fe7377d2..a0d5892e4c00d2962cd0d6f524cc6d738f1e1518 100644 (file)
@@ -31,4 +31,11 @@ abstract class AbstractMenuPageHandler implements IMenuPageHandler
     {
         return true;
     }
+
+    /**
+     * @inheritDoc
+     */
+    public function cacheObject(int $objectID): void
+    {
+    }
 }
index d44f58f773e6b8e5a2574c0a4726ae91676fcf66..caac8b57229209d298253922b7841b20cd917fb4 100644 (file)
@@ -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);
+    }
 }
index 54c81f3da9b44978e5096891adf5d52b10a71519..9b25cb1bf2a24403d4ebc5313366172ea4e66232 100644 (file)
@@ -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;
 }