[TASK] Cache results in doesRecordExist_pageLookUp 16/51516/12
authorThomas Hohn <thomas@hohn.dk>
Fri, 3 Feb 2017 09:00:02 +0000 (10:00 +0100)
committerBenni Mack <benni@typo3.org>
Mon, 22 May 2017 10:16:52 +0000 (12:16 +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
Change-Id: I2da164193f13759123d57c17985be46e7bd7667b
Reviewed-on: https://review.typo3.org/51516
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Kasper Ligaard <kasperligaard+typo3.org@gmail.com>
Tested-by: Kasper Ligaard <kasperligaard+typo3.org@gmail.com>
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/core/Classes/DataHandling/DataHandler.php
typo3/sysext/core/Documentation/Changelog/master/Important-79610-ChangeSignatureAndReturnValueFordoesRecordExist_pageLookUp.rst [new file with mode: 0644]

index 633c1b5..eb85716 100644 (file)
@@ -25,7 +25,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;
@@ -6570,8 +6569,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)) {
@@ -6580,8 +6578,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;
@@ -6592,16 +6590,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',
@@ -6618,7 +6625,11 @@ class DataHandler
                 $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
             ));
         }
-        return $queryBuilder;
+
+        $row = $queryBuilder->execute()->fetch();
+        $this->runtimeCache->set($cacheId, $row);
+
+        return $row;
     }
 
     /**
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Important-79610-ChangeSignatureAndReturnValueFordoesRecordExist_pageLookUp.rst b/typo3/sysext/core/Documentation/Changelog/master/Important-79610-ChangeSignatureAndReturnValueFordoesRecordExist_pageLookUp.rst
new file mode 100644 (file)
index 0000000..c8827c4
--- /dev/null
@@ -0,0 +1,22 @@
+.. include:: ../../Includes.txt
+
+====================================================================================
+Important: #79610 - Change Signature And Return Value For doesRecordExist_pageLookUp
+====================================================================================
+
+See :issue:`79610`
+
+Description
+===========
+
+Method :php:`doesRecordExist_pageLookUp()` of class :php:`DataHandler` has been
+changed. The signature and return value has been changed as a preparation for
+more optimisations in the class :php:`DataHandler`.
+
+Impact
+======
+
+None since the usages of doesRecordExist_pageLookUp should be limited
+to the class :php:`DataHandler` - see also :issue:`77391`.
+
+.. index:: PHP-API
\ No newline at end of file