[BUGFIX] Cache backend usernames and workspace titles 25/35525/2
authorTim Lochmueller <tim@fruit-lab.de>
Sun, 23 Mar 2014 16:48:01 +0000 (17:48 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Tue, 16 Dec 2014 19:43:39 +0000 (20:43 +0100)
Add a runtime cache for improved performance of user and
workspace titles in ext:belog. Those view helpers are
called twice per displayed row and should have a shortcut
if the name resolution was done already.

Resolves: #55653
Releases: master, 6.2
Change-Id: I62465faaebc13ba0dfe781350fa6c87cc87f54a6
Reviewed-on: http://review.typo3.org/35525
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/belog/Classes/ViewHelpers/UsernameViewHelper.php
typo3/sysext/belog/Classes/ViewHelpers/WorkspaceTitleViewHelper.php

index b60217d..71b5903 100644 (file)
@@ -28,18 +28,28 @@ class UsernameViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHe
        protected $backendUserRepository;
 
        /**
+        * First level cache of user names
+        *
+        * @var array
+        */
+       static protected $usernameRuntimeCache = array();
+
+       /**
         * Resolve user name from backend user id.
         *
         * @param integer $uid Uid of the user
         * @return string Username or an empty string if there is no user with that UID
         */
        public function render($uid) {
+               if (isset(static::$usernameRuntimeCache[$uid])) {
+                       return static::$usernameRuntimeCache[$uid];
+               }
+
                /** @var $user \TYPO3\CMS\Extbase\Domain\Model\BackendUser */
                $user = $this->backendUserRepository->findByUid($uid);
-               if ($user === NULL) {
-                       return '';
-               }
-               return $user->getUserName();
+               // $user may be NULL if user was deleted from DB, set it to empty string to always return a string
+               static::$usernameRuntimeCache[$uid] = ($user === NULL) ? '' : $user->getUserName();
+               return static::$usernameRuntimeCache[$uid];
        }
 
 }
index 98202e0..ca8eb40 100644 (file)
@@ -28,26 +28,35 @@ class WorkspaceTitleViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\Abstract
        protected $workspaceRepository = NULL;
 
        /**
+        * First level cache of workspace titles
+        *
+        * @var array
+        */
+       static protected $workspaceTitleRuntimeCache = array();
+
+       /**
         * Resolve workspace title from UID.
         *
         * @param integer $uid UID of the workspace
         * @return string username or UID
         */
        public function render($uid) {
-               if ($uid === 0) {
-                       return \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('live', $this->controllerContext->getRequest()->getControllerExtensionName());
+               if (isset(static::$workspaceTitleRuntimeCache[$uid])) {
+                       return static::$workspaceTitleRuntimeCache[$uid];
                }
-               if (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces')) {
-                       return '';
-               }
-               /** @var $workspace \TYPO3\CMS\Belog\Domain\Model\Workspace */
-               $workspace = $this->workspaceRepository->findByUid($uid);
-               if ($workspace !== NULL) {
-                       $title = $workspace->getTitle();
+
+               if ($uid === 0) {
+                       static::$workspaceTitleRuntimeCache[$uid] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('live', $this->controllerContext->getRequest()->getControllerExtensionName());
+               } elseif (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces')) {
+                       static::$workspaceTitleRuntimeCache[$uid] = '';
                } else {
-                       $title = '';
+                       /** @var $workspace \TYPO3\CMS\Belog\Domain\Model\Workspace */
+                       $workspace = $this->workspaceRepository->findByUid($uid);
+                       // $workspace may be null, force empty string in this case
+                       static::$workspaceTitleRuntimeCache[$uid] = ($workspace === NULL) ? '' : $workspace->getTitle();
                }
-               return $title;
+
+               return static::$workspaceTitleRuntimeCache[$uid];
        }
 
 }