[TASK] Performance: Use cache_hash with "mixed" data 12/24512/4
authorStefan Neufeind <typo3.neufeind@speedpartner.de>
Thu, 10 Oct 2013 00:32:00 +0000 (02:32 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Fri, 11 Oct 2013 14:09:11 +0000 (16:09 +0200)
The cache-frontend is VariableFrontendCache that already uses
serialize() or (if available) igbinary_serialie() itself.
No need to additionally serialize data.

Change-Id: I8c3cfd2a279f8a38bc5c4d860329f2333f1304a5
Resolves: #52648
Releases: 6.2
Reviewed-on: https://review.typo3.org/24512
Reviewed-by: Philipp Gampe
Tested-by: Philipp Gampe
Reviewed-by: Stefan Neufeind
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
typo3/sysext/backend/Classes/Configuration/TsConfigParser.php
typo3/sysext/backend/Classes/Utility/BackendUtility.php
typo3/sysext/core/Classes/Authentication/BackendUserAuthentication.php
typo3/sysext/core/Classes/ExtDirect/ExtDirectApi.php
typo3/sysext/core/Classes/TypoScript/TemplateService.php
typo3/sysext/core/Classes/Utility/GeneralUtility.php
typo3/sysext/cshmanual/Classes/Controller/HelpModuleController.php
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Classes/ContentObject/Menu/AbstractMenuContentObject.php
typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
typo3/sysext/frontend/Classes/Page/PageRepository.php

index 7bbeea5..15b49b4 100644 (file)
@@ -57,9 +57,9 @@ class TsConfigParser extends \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser
                $this->rootLine = $rootLine;
                $hash = md5($type . ':' . $TStext);
                $cachedContent = BackendUtility::getHash($hash);
-               if ($cachedContent) {
-                       $storedData = unserialize($cachedContent);
-                       $storedMD5 = substr($cachedContent, -strlen($hash));
+               if (is_array($cachedContent)) {
+                       $storedData = $cachedContent[0];
+                       $storedMD5 = $cachedContent[1];
                        $storedData['match'] = array();
                        $storedData = $this->matching($storedData);
                        $checkMD5 = md5(serialize($storedData));
@@ -71,16 +71,15 @@ class TsConfigParser extends \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser
                        } else {
                                $shash = md5($checkMD5 . $hash);
                                $cachedSpec = BackendUtility::getHash($shash);
-                               if ($cachedSpec) {
-                                       $storedData = unserialize($cachedSpec);
+                               if (is_array($cachedSpec)) {
+                                       $storedData = $cachedSpec;
                                        $res = array(
                                                'TSconfig' => $storedData['TSconfig'],
                                                'cached' => 1
                                        );
                                } else {
                                        $storeData = $this->parseWithConditions($TStext);
-                                       $serData = serialize($storeData);
-                                       BackendUtility::storeHash($shash, $serData, $type . '_TSconfig');
+                                       BackendUtility::storeHash($shash, $storeData, $type . '_TSconfig');
                                        $res = array(
                                                'TSconfig' => $storeData['TSconfig'],
                                                'cached' => 0
@@ -89,9 +88,8 @@ class TsConfigParser extends \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser
                        }
                } else {
                        $storeData = $this->parseWithConditions($TStext);
-                       $serData = serialize($storeData);
-                       $md5 = md5($serData);
-                       BackendUtility::storeHash($hash, $serData . $md5, $type . '_TSconfig');
+                       $md5 = md5(serialize($storeData));
+                       BackendUtility::storeHash($hash, array($storeData, $md5), $type . '_TSconfig');
                        $res = array(
                                'TSconfig' => $storeData['TSconfig'],
                                'cached' => 0
index 1662f4a..38b9193 100644 (file)
@@ -1080,12 +1080,13 @@ class BackendUtility {
         *
         *******************************************/
        /**
-        * Stores the string value $data in the 'cache_hash' cache with the
-        * hash key, $hash, and visual/symbolic identification, $ident
+        * Stores $data in the 'cache_hash' cache with the hash key, $hash
+        * and visual/symbolic identification, $ident
+        *
         * IDENTICAL to the function by same name found in \TYPO3\CMS\Frontend\Page\PageRepository
         *
         * @param string $hash 32 bit hash string (eg. a md5 hash of a serialized array identifying the data being stored)
-        * @param string $data The data string. If you want to store an array, then just serialize it first.
+        * @param mixed $data The data to store
         * @param string $ident $ident is just a textual identification in order to inform about the content!
         * @return      void
         */
@@ -1094,14 +1095,14 @@ class BackendUtility {
        }
 
        /**
-        * Returns string value stored for the hash string in the cache "cache_hash"
-        * Can be used to retrieved a cached value
+        * Returns data stored for the hash string in the cache "cache_hash"
+        * Can be used to retrieved a cached value, array or object
         *
         * IDENTICAL to the function by same name found in \TYPO3\CMS\Frontend\Page\PageRepository
         *
         * @param string $hash The hash-string which was used to store the data value
         * @param integer $expTime Variabele is not used in the function
-        * @return string
+        * @return mixed The "data" from the cache
         */
        static public function getHash($hash, $expTime = 0) {
                $hashContent = NULL;
index 1037ee0..7782913 100644 (file)
@@ -1284,13 +1284,13 @@ class BackendUserAuthentication extends \TYPO3\CMS\Core\Authentication\AbstractU
                                // Parsing the user TSconfig (or getting from cache)
                                $hash = md5('userTS:' . $this->userTS_text);
                                $cachedContent = BackendUtility::getHash($hash);
-                               if (isset($cachedContent) && !$this->userTS_dontGetCached) {
-                                       $this->userTS = unserialize($cachedContent);
+                               if (is_array($cachedContent) && !$this->userTS_dontGetCached) {
+                                       $this->userTS = $cachedContent;
                                } else {
                                        $parseObj = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\Parser\\TypoScriptParser');
                                        $parseObj->parse($this->userTS_text);
                                        $this->userTS = $parseObj->setup;
-                                       BackendUtility::storeHash($hash, serialize($this->userTS), 'BE_USER_TSconfig');
+                                       BackendUtility::storeHash($hash, $this->userTS, 'BE_USER_TSconfig');
                                        // Update UC:
                                        $this->userTSUpdated = 1;
                                }
index 4814abd..602959d 100644 (file)
@@ -166,15 +166,12 @@ class ExtDirectApi {
                $cacheIdentifier = 'ExtDirectApi';
                $cacheHash = md5($cacheIdentifier . implode(',', $filterNamespaces) . \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SSL') . serialize($this->settings) . TYPO3_MODE . \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_HOST'));
                // With no_cache always generate the javascript content
-               $cacheContent = $noCache ? '' : \TYPO3\CMS\Frontend\Page\PageRepository::getHash($cacheHash);
                // Generate the javascript content if it wasn't found inside the cache and cache it!
-               if (!$cacheContent) {
+               if ($noCache || !is_array(($javascriptNamespaces = \TYPO3\CMS\Frontend\Page\PageRepository::getHash($cacheHash)))) {
                        $javascriptNamespaces = $this->generateAPI($filterNamespaces);
                        if (count($javascriptNamespaces)) {
-                               \TYPO3\CMS\Frontend\Page\PageRepository::storeHash($cacheHash, serialize($javascriptNamespaces), $cacheIdentifier);
+                               \TYPO3\CMS\Frontend\Page\PageRepository::storeHash($cacheHash, $javascriptNamespaces, $cacheIdentifier);
                        }
-               } else {
-                       $javascriptNamespaces = unserialize($cacheContent);
                }
                return $javascriptNamespaces;
        }
index ed82106..8d459f0 100644 (file)
@@ -464,9 +464,9 @@ class TemplateService {
                                // If currentPageData was not there, we first find $rowSum (freshly generated). After that we try to see, if it is stored with a list of all conditions. If so we match the result.
                                $rowSumHash = md5('ROWSUM:' . serialize($this->rowSum));
                                $result = \TYPO3\CMS\Frontend\Page\PageRepository::getHash($rowSumHash);
-                               if ($result) {
+                               if (is_array($result)) {
                                        $cc = array();
-                                       $cc['all'] = unserialize($result);
+                                       $cc['all'] = $result;
                                        $cc['rowSum'] = $this->rowSum;
                                        $cc = $this->matching($cc);
                                        ksort($cc);
@@ -477,9 +477,9 @@ class TemplateService {
                                // Get TypoScript setup array
                                $setupData = \TYPO3\CMS\Frontend\Page\PageRepository::getHash($hash);
                        }
-                       if ($setupData && !$this->forceTemplateParsing) {
+                       if (is_array($setupData) && !$this->forceTemplateParsing) {
                                // If TypoScript setup structure was cached we unserialize it here:
-                               $this->setup = unserialize($setupData);
+                               $this->setup = $setupData;
                                if ($this->tt_track) {
                                        $GLOBALS['TT']->setTSLogMessage('Using cached TS template data');
                                }
@@ -500,12 +500,12 @@ class TemplateService {
                                ksort($cc);
                                $hash = md5(serialize($cc));
                                // This stores the data.
-                               \TYPO3\CMS\Frontend\Page\PageRepository::storeHash($hash, serialize($this->setup), 'TS_TEMPLATE');
+                               \TYPO3\CMS\Frontend\Page\PageRepository::storeHash($hash, $this->setup, 'TS_TEMPLATE');
                                if ($this->tt_track) {
                                        $GLOBALS['TT']->setTSlogMessage('TS template size, serialized: ' . strlen(serialize($this->setup)) . ' bytes');
                                }
                                $rowSumHash = md5('ROWSUM:' . serialize($this->rowSum));
-                               \TYPO3\CMS\Frontend\Page\PageRepository::storeHash($rowSumHash, serialize($cc['all']), 'TMPL_CONDITIONS_ALL');
+                               \TYPO3\CMS\Frontend\Page\PageRepository::storeHash($rowSumHash, $cc['all'], 'TMPL_CONDITIONS_ALL');
                        }
                        // Add rootLine
                        $cc['rootLine'] = $this->rootLine;
index 790208f..5436ca6 100644 (file)
@@ -2092,11 +2092,10 @@ class GeneralUtility {
                        $array = $firstLevelCache[$identifier];
                } else {
                        // Look up in second level cache
-                       $cacheContent = \TYPO3\CMS\Frontend\Page\PageRepository::getHash($identifier, 0);
-                       $array = unserialize($cacheContent);
-                       if ($array === FALSE) {
+                       $array = \TYPO3\CMS\Frontend\Page\PageRepository::getHash($identifier, 0);
+                       if (!is_array($array)) {
                                $array = self::xml2arrayProcess($string, $NSprefix, $reportDocTag);
-                               \TYPO3\CMS\Frontend\Page\PageRepository::storeHash($identifier, serialize($array), 'ident_xml2array');
+                               \TYPO3\CMS\Frontend\Page\PageRepository::storeHash($identifier, $array, 'ident_xml2array');
                        }
                        // Store content in first level cache
                        $firstLevelCache[$identifier] = $array;
index 491ca81..4cce3f8 100644 (file)
@@ -636,9 +636,11 @@ class HelpModuleController {
        public function createGlossaryIndex() {
                // Create hash string and try to retrieve glossary array:
                $hash = md5('typo3/mod.php?M=help_cshmanual:glossary');
-               list($this->glossaryWords, $this->substWords) = unserialize(BackendUtility::getHash($hash));
+               $cachedData = BackendUtility::getHash($hash);
                // Generate glossary words if not found:
-               if (!is_array($this->glossaryWords)) {
+               if (is_array($cachedData)) {
+                       list($this->glossaryWords, $this->substWords) = $cachedData;
+               } else {
                        // Initialize:
                        $this->glossaryWords = array();
                        $this->substWords = array();
@@ -670,7 +672,7 @@ class HelpModuleController {
                                }
                        }
                        krsort($this->substWords);
-                       BackendUtility::storeHash($hash, serialize(array($this->glossaryWords, $this->substWords)), 'Glossary');
+                       BackendUtility::storeHash($hash, array($this->glossaryWords, $this->substWords), 'Glossary');
                }
        }
 
index 3954f84..32035be 100644 (file)
@@ -1898,7 +1898,12 @@ class ContentObjectRenderer {
                        $GLOBALS['TT']->setTSlogMessage('Cached', 0);
                } else {
                        $storeArrDat = $GLOBALS['TSFE']->sys_page->getHash($storeKey);
-                       if (!isset($storeArrDat)) {
+                       if (is_array($storeArrDat)) {
+                               $storeArr = $storeArrDat;
+                               // Setting cache:
+                               $this->substMarkerCache[$storeKey] = $storeArr;
+                               $GLOBALS['TT']->setTSlogMessage('Cached from DB', 0);
+                       } else {
                                // Initialize storeArr
                                $storeArr = array();
                                // Finding subparts and substituting them with the subpart as a marker
@@ -1924,14 +1929,8 @@ class ContentObjectRenderer {
                                // Setting cache:
                                $this->substMarkerCache[$storeKey] = $storeArr;
                                // Storing the cached data:
-                               $GLOBALS['TSFE']->sys_page->storeHash($storeKey, serialize($storeArr), 'substMarkArrayCached');
+                               $GLOBALS['TSFE']->sys_page->storeHash($storeKey, $storeArr, 'substMarkArrayCached');
                                $GLOBALS['TT']->setTSlogMessage('Parsing', 0);
-                       } else {
-                               // Unserializing
-                               $storeArr = unserialize($storeArrDat);
-                               // Setting cache:
-                               $this->substMarkerCache[$storeKey] = $storeArr;
-                               $GLOBALS['TT']->setTSlogMessage('Cached from DB', 0);
                        }
                }
                // Substitution/Merging:
index e0559d4..8fd9d37 100644 (file)
@@ -892,12 +892,12 @@ class AbstractMenuContentObject {
                        } else {
                                $cacheTimeout = $GLOBALS['TSFE']->get_cache_timeout();
                        }
-                       $serData = $this->sys_page->getHash($this->hash);
-                       if (!$serData) {
+                       $cachedData = $this->sys_page->getHash($this->hash);
+                       if (!is_array($cachedData)) {
                                $this->generate();
-                               $this->sys_page->storeHash($this->hash, serialize($this->result), 'MENUDATA', $cacheTimeout);
+                               $this->sys_page->storeHash($this->hash, $this->result, 'MENUDATA', $cacheTimeout);
                        } else {
-                               $this->result = unserialize($serData);
+                               $this->result = $cachedData;
                        }
                        // End showAccessRestrictedPages
                        if ($this->mconf['showAccessRestrictedPages']) {
index 13951f7..26715c2 100644 (file)
@@ -4336,13 +4336,13 @@ if (version == "n3") {
                        $userTS = implode(LF . '[GLOBAL]' . LF, $TSdataArray);
                        $hash = md5('pageTS:' . $userTS);
                        $cachedContent = $this->sys_page->getHash($hash);
-                       if (isset($cachedContent)) {
-                               $this->pagesTSconfig = unserialize($cachedContent);
+                       if (is_array($cachedContent)) {
+                               $this->pagesTSconfig = $cachedContent;
                        } else {
                                $parseObj = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\Parser\\TypoScriptParser');
                                $parseObj->parse($userTS);
                                $this->pagesTSconfig = $parseObj->setup;
-                               $this->sys_page->storeHash($hash, serialize($this->pagesTSconfig), 'PAGES_TSconfig');
+                               $this->sys_page->storeHash($hash, $this->pagesTSconfig, 'PAGES_TSconfig');
                        }
                }
                return $this->pagesTSconfig;
index 38a15ce..0165797 100644 (file)
@@ -780,15 +780,15 @@ class PageRepository {
         *
         **********************************/
        /**
-        * Returns string value stored for the hash string in the cache "cache_hash"
-        * Can be used to retrieved a cached value
+        * Returns data stored for the hash string in the cache "cache_hash"
+        * Can be used to retrieved a cached value, array or object
         * Can be used from your frontend plugins if you like. It is also used to
         * store the parsed TypoScript template structures. You can call it directly
         * like \TYPO3\CMS\Frontend\Page\PageRepository::getHash()
         *
         * @param string $hash The hash-string which was used to store the data value
         * @param integer The expiration time (not used anymore)
-        * @return string The "content" field of the "cache_hash" cache entry.
+        * @return mixed The "data" from the cache
         * @see tslib_TStemplate::start(), storeHash()
         */
        static public function getHash($hash, $expTime = 0) {
@@ -804,12 +804,13 @@ class PageRepository {
        }
 
        /**
-        * Stores a string value in the cache_hash cache identified by $hash.
+        * Stores $data in the 'cache_hash' cache with the hash key, $hash
+        * and visual/symbolic identification, $ident
         * Can be used from your frontend plugins if you like. You can call it
         * directly like \TYPO3\CMS\Frontend\Page\PageRepository::storeHash()
         *
         * @param string $hash 32 bit hash string (eg. a md5 hash of a serialized array identifying the data being stored)
-        * @param string $data The data string. If you want to store an array, then just serialize it first.
+        * @param mixed $data The data to store
         * @param string $ident Is just a textual identification in order to inform about the content!
         * @param integer $lifetime The lifetime for the cache entry in seconds
         * @return void