[TASK] Cache results in doesRecordExist_pageLookUp 80/53280/3
authorThomas Hohn <thomas@hohn.dk>
Fri, 3 Feb 2017 09:00:02 +0000 (10:00 +0100)
committerMarkus Klein <markus.klein@typo3.org>
Tue, 20 Jun 2017 11:47:03 +0000 (13:47 +0200)
Copying big sub-trees can benefit from caching the calls
to doesRecordExist_pageLookUp in a run-time cache since
there are a lot of identical calls to the database.

In addition the signature was adjusted - see also the already
merged issue #77391 which altered the signature.

Resolves: #79610
Releases: master, 8.7
Change-Id: I2da164193f13759123d57c17985be46e7bd7667b
Reviewed-on: https://review.typo3.org/53280
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
typo3/sysext/core/Classes/DataHandling/DataHandler.php

index cd8363f..34ee4cf 100644 (file)
@@ -26,7 +26,6 @@ use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
 use TYPO3\CMS\Core\Configuration\Richtext;
 use TYPO3\CMS\Core\Database\Connection;
 use TYPO3\CMS\Core\Database\ConnectionPool;
-use TYPO3\CMS\Core\Database\Query\QueryBuilder;
 use TYPO3\CMS\Core\Database\Query\QueryHelper;
 use TYPO3\CMS\Core\Database\Query\Restriction\BackendWorkspaceRestriction;
 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
@@ -6657,8 +6656,7 @@ class DataHandler
                 // If record found, check page as well:
                 if (is_array($output)) {
                     // Looking up the page for record:
-                    $queryBuilder = $this->doesRecordExist_pageLookUp($output['pid'], $perms);
-                    $pageRec = $queryBuilder->select('uid')->execute()->fetch();
+                    $pageRec = $this->doesRecordExist_pageLookUp($output['pid'], $perms);
                     // Return TRUE if either a page was found OR if the PID is zero AND the user is ADMIN (in which case the record is at root-level):
                     $isRootLevelRestrictionIgnored = BackendUtility::isRootLevelRestrictionIgnored($table);
                     if (is_array($pageRec) || !$output['pid'] && ($isRootLevelRestrictionIgnored || $this->admin)) {
@@ -6667,8 +6665,8 @@ class DataHandler
                 }
                 return false;
             } else {
-                $queryBuilder = $this->doesRecordExist_pageLookUp($id, $perms);
-                return $queryBuilder->count('uid')->execute()->fetchColumn(0);
+                $pageRec = $this->doesRecordExist_pageLookUp($id, $perms);
+                return is_array($pageRec);
             }
         }
         return false;
@@ -6679,16 +6677,25 @@ class DataHandler
      *
      * @param int $id Page id
      * @param int $perms Permission integer
-     * @return QueryBuilder
+     * @param string $fields List of fields (SQL CSV list) to select
+     * @return bool|array
      * @access private
      * @see doesRecordExist()
      */
-    protected function doesRecordExist_pageLookUp($id, $perms)
+    protected function doesRecordExist_pageLookUp($id, $perms, $fieldList = 'uid')
     {
+        $cacheId = md5('doesRecordExist_pageLookUp' . '_' . $id . '_' . $perms . '_' . $fieldList . '_' . (string)$this->admin);
+
+        // If result is cached, return it
+        $cachedResult = $this->runtimeCache->get($cacheId);
+        if (!empty($cachedResult)) {
+            return $cachedResult;
+        }
+
         $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
         $this->addDeleteRestriction($queryBuilder->getRestrictions()->removeAll());
         $queryBuilder
-            ->select('uid')
+            ->select(...GeneralUtility::trimExplode(',', $fieldList, true))
             ->from('pages')
             ->where($queryBuilder->expr()->eq(
                 'uid',
@@ -6705,7 +6712,11 @@ class DataHandler
                 $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
             ));
         }
-        return $queryBuilder;
+
+        $row = $queryBuilder->execute()->fetch();
+        $this->runtimeCache->set($cacheId, $row);
+
+        return $row;
     }
 
     /**