From 856ddf39eeb32dc4a0fb07ea7f9508e21683fb5d Mon Sep 17 00:00:00 2001
From: Ralf Zimmermann <ralf.zimmermann@tritum.de>
Date: Sun, 10 Sep 2017 18:51:35 +0200
Subject: [PATCH] [TASK] EXT:form - cache merged YAML settings

Cache the merged YAML settings into the assets cache to improve the
performance.

Resolves: #82373
Releases: master, 8.7
Change-Id: I601364d5957f26f1b369b513534936820f72b78e
Reviewed-on: https://review.typo3.org/54106
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Mathias Brodala <mbrodala@pagemachine.de>
Tested-by: Mathias Brodala <mbrodala@pagemachine.de>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Reviewed-by: Henning Liebe <h.liebe@neusta.de>
Tested-by: Bjoern Jacob <bjoern.jacob@tritum.de>
Reviewed-by: Sebastian Fischer <typo3@evoweb.de>
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
---
 .../Configuration/ConfigurationManager.php    | 90 +++++++++++++++----
 1 file changed, 72 insertions(+), 18 deletions(-)

diff --git a/typo3/sysext/form/Classes/Mvc/Configuration/ConfigurationManager.php b/typo3/sysext/form/Classes/Mvc/Configuration/ConfigurationManager.php
index f3b138648c3b..f8291b7a3e2e 100644
--- a/typo3/sysext/form/Classes/Mvc/Configuration/ConfigurationManager.php
+++ b/typo3/sysext/form/Classes/Mvc/Configuration/ConfigurationManager.php
@@ -15,7 +15,10 @@ namespace TYPO3\CMS\Form\Mvc\Configuration;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Cache\CacheManager;
+use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
 use TYPO3\CMS\Core\Utility\ArrayUtility;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Configuration\ConfigurationManager as ExtbaseConfigurationManager;
 use TYPO3\CMS\Form\Mvc\Configuration\Exception\ExtensionNameRequiredException;
 
@@ -27,17 +30,16 @@ use TYPO3\CMS\Form\Mvc\Configuration\Exception\ExtensionNameRequiredException;
  */
 class ConfigurationManager extends ExtbaseConfigurationManager implements ConfigurationManagerInterface
 {
+
     /**
-     * @var \TYPO3\CMS\Form\Mvc\Configuration\YamlSource
+     * @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
      */
-    protected $yamlSource;
+    protected $cache;
 
     /**
-     * 1st level configuration cache
-     *
-     * @var array
+     * @var \TYPO3\CMS\Form\Mvc\Configuration\YamlSource
      */
-    protected $configurationCache = [];
+    protected $yamlSource;
 
     /**
      * @param \TYPO3\CMS\Form\Mvc\Configuration\YamlSource $yamlSource
@@ -93,16 +95,13 @@ class ConfigurationManager extends ExtbaseConfigurationManager implements Config
         }
         $ucFirstExtensioName = ucfirst($extensionName);
 
-        // 1st level cache
-        $configurationCacheKey = strtolower(self::CONFIGURATION_TYPE_YAML_SETTINGS . '|' . $extensionName);
-        if (isset($this->configurationCache[$configurationCacheKey])) {
-            return $this->configurationCache[$configurationCacheKey];
+        $yamlSettings = $this->getYamlSettingsFromCache($extensionName);
+        if (!empty($yamlSettings)) {
+            return $this->overrideConfigurationByTypoScript($yamlSettings, $extensionName);
         }
 
-        $typoscriptSettings = parent::getConfiguration(
-            ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
-            $extensionName
-        );
+        $typoscriptSettings = $this->getTypoScriptSettings($extensionName);
+
         $yamlSettingsFilePaths = isset($typoscriptSettings['yamlConfigurations'])
             ? ArrayUtility::sortArrayWithIntegerKeys($typoscriptSettings['yamlConfigurations'])
             : [];
@@ -114,11 +113,9 @@ class ConfigurationManager extends ExtbaseConfigurationManager implements Config
             ? $yamlSettings['TYPO3']['CMS'][$ucFirstExtensioName]
             : [];
         $yamlSettings = ArrayUtility::sortArrayWithIntegerKeysRecursive($yamlSettings);
-        $yamlSettings = $this->overrideConfigurationByTypoScript($yamlSettings, $extensionName);
+        $this->setYamlSettingsIntoCache($extensionName, $yamlSettings);
 
-        // 1st level cache
-        $this->configurationCache[$configurationCacheKey] = $yamlSettings;
-        return $yamlSettings;
+        return $this->overrideConfigurationByTypoScript($yamlSettings, $extensionName);
     }
 
     /**
@@ -142,4 +139,61 @@ class ConfigurationManager extends ExtbaseConfigurationManager implements Config
         }
         return $yamlSettings;
     }
+
+    /**
+     * @return \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
+     */
+    protected function getCacheFrontend(): FrontendInterface
+    {
+        if ($this->cache === null) {
+            $this->cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('assets');
+        }
+        return $this->cache;
+    }
+
+    /**
+     * @param string $extensionName
+     * @return string
+     */
+    protected function getConfigurationCacheKey(string $extensionName): string
+    {
+        return strtolower(self::CONFIGURATION_TYPE_YAML_SETTINGS . '_' . $extensionName);
+    }
+
+    /**
+     * @param string $extensionName
+     * @return mixed
+     */
+    protected function getYamlSettingsFromCache(string $extensionName)
+    {
+        return $this->getCacheFrontend()->get(
+            $this->getConfigurationCacheKey($extensionName)
+        );
+    }
+
+    /**
+     * @param string $extensionName
+     * @param array $yamlSettings
+     */
+    protected function setYamlSettingsIntoCache(
+        string $extensionName,
+        array $yamlSettings
+    ) {
+        $this->getCacheFrontend()->set(
+            $this->getConfigurationCacheKey($extensionName),
+            $yamlSettings
+        );
+    }
+
+    /**
+     * @param string $extensionName
+     * @return null|[]
+     */
+    protected function getTypoScriptSettings(string $extensionName)
+    {
+        return parent::getConfiguration(
+            ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
+            $extensionName
+        );
+    }
 }
-- 
GitLab