[TASK] Introduce runtime cache for getIndpEnv() 32/42732/5
authorMarkus Klein <markus.klein@typo3.org>
Tue, 18 Aug 2015 20:54:48 +0000 (22:54 +0200)
committerHelmut Hummel <helmut.hummel@typo3.org>
Mon, 24 Aug 2015 22:17:17 +0000 (00:17 +0200)
This method is independent of runtime state, but just reflects
environment state, which does not change during a request.

Caching the result of the method gives a performance boost for common
BE request as documented in the ticket. (85% fewer calls to the method.)

Resolves: #69173
Releases: master
Change-Id: Icb3fd5fb56434c6db2323a9038b7d486426cab7e
Reviewed-on: http://review.typo3.org/42732
Reviewed-by: Alexander Opitz <opitz.alexander@googlemail.com>
Tested-by: Alexander Opitz <opitz.alexander@googlemail.com>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Daniel Goerz <ervaude@gmail.com>
Tested-by: Daniel Goerz <ervaude@gmail.com>
Reviewed-by: Xavier Perseguers <xavier@typo3.org>
Reviewed-by: Helmut Hummel <helmut.hummel@typo3.org>
Tested-by: Helmut Hummel <helmut.hummel@typo3.org>
typo3/sysext/backend/Tests/Unit/Configuration/TypoScript/ConditionMatching/ConditionMatcherTest.php
typo3/sysext/core/Classes/Utility/GeneralUtility.php
typo3/sysext/core/Tests/Unit/Configuration/TypoScript/ConditionMatching/AbstractConditionMatcherTest.php
typo3/sysext/core/Tests/Unit/Http/ServerRequestFactoryTest.php
typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php
typo3/sysext/extbase/Tests/Unit/Mvc/Web/Routing/UriBuilderTest.php
typo3/sysext/felogin/Tests/Unit/Controller/FrontendLoginControllerTest.php
typo3/sysext/frontend/Tests/Unit/Configuration/TypoScript/ConditionMatching/ConditionMatcherTest.php
typo3/sysext/frontend/Tests/Unit/Controller/TypoScriptFrontendControllerTest.php

index 77e11b9..eff842e 100644 (file)
@@ -50,6 +50,7 @@ class ConditionMatcherTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $this->testGlobalNamespace = $this->getUniqueId('TEST');
                $GLOBALS['TCA'][$this->testTableName] = array('ctrl' => array());
                $GLOBALS[$this->testGlobalNamespace] = array();
+               GeneralUtility::flushInternalRuntimeCaches();
                $this->setUpBackend();
                $this->matchCondition = $this->getMock(\TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher::class, array('determineRootline'), array(), '', FALSE);
        }
index 250a6b0..838c7b1 100755 (executable)
@@ -109,6 +109,11 @@ class GeneralUtility {
                'srv', // HHVM with fastcgi
        );
 
+       /**
+        * @var array
+        */
+       static protected $indpEnvCache = [];
+
        /*************************
         *
         * GET/POST Variables
@@ -3211,6 +3216,10 @@ Connection: close
         * @throws \UnexpectedValueException
         */
        static public function getIndpEnv($getEnvName) {
+               if (isset(self::$indpEnvCache[$getEnvName])) {
+                       return self::$indpEnvCache[$getEnvName];
+               }
+
                /*
                Conventions:
                output from parse_url():
@@ -3490,6 +3499,7 @@ Connection: close
                                $retVal = $out;
                                break;
                }
+               self::$indpEnvCache[$getEnvName] = $retVal;
                return $retVal;
        }
 
@@ -4554,6 +4564,19 @@ Connection: close
        }
 
        /**
+        * Flush internal runtime caches
+        *
+        * Used in unit tests only.
+        *
+        * @return void
+        * @internal
+        */
+       static public function flushInternalRuntimeCaches() {
+               self::$indpEnvCache = [];
+               self::$idnaStringCache = [];
+       }
+
+       /**
         * Find the best service and check if it works.
         * Returns object of the service class.
         *
index 44c44d1..53f11d3 100644 (file)
@@ -45,6 +45,8 @@ class AbstractConditionMatcherTest extends UnitTestCase {
        protected function setUp() {
                require_once('Fixtures/ConditionMatcherUserFuncs.php');
 
+               GeneralUtility::flushInternalRuntimeCaches();
+
                $this->backupApplicationContext = GeneralUtility::getApplicationContext();
                $this->conditionMatcher = $this->getMockForAbstractClass(AbstractConditionMatcher::class);
                $this->evaluateConditionCommonMethod = new \ReflectionMethod(AbstractConditionMatcher::class, 'evaluateConditionCommon');
index d1f7b21..e1f7642 100644 (file)
@@ -16,6 +16,7 @@ namespace TYPO3\CMS\Core\Tests\Unit\Http;
 
 use TYPO3\CMS\Core\Http\ServerRequestFactory;
 use TYPO3\CMS\Core\Http\UploadedFile;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
  * Testcase for \TYPO3\CMS\Core\Http\ServerRequestFactory
@@ -23,6 +24,13 @@ use TYPO3\CMS\Core\Http\UploadedFile;
 class ServerRequestFactoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 
        /**
+        * Set up
+        */
+       protected function setUp() {
+               GeneralUtility::flushInternalRuntimeCaches();
+       }
+
+       /**
         * @test
         */
        public function uploadedFilesAreNormalizedFromFilesSuperGlobal() {
index df561bf..ecb1a6c 100644 (file)
@@ -37,6 +37,7 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        protected $singletonInstances = array();
 
        protected function setUp() {
+               GeneralUtilityFixture::flushInternalRuntimeCaches();
                GeneralUtilityFixture::$isAllowedHostHeaderValueCallCount = 0;
                GeneralUtilityFixture::setAllowHostHeaderValue(FALSE);
                $GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] = GeneralUtility::ENV_TRUSTED_HOSTS_PATTERN_ALLOW_ALL;
@@ -1550,6 +1551,7 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @dataProvider hostnameAndPortDataProvider
         */
        public function getIndpEnvTypo3HostOnlyParsesHostnamesAndIpAdresses($httpHost, $expectedIp) {
+               GeneralUtility::flushInternalRuntimeCaches();
                $_SERVER['HTTP_HOST'] = $httpHost;
                $this->assertEquals($expectedIp, GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY'));
        }
@@ -1714,8 +1716,11 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         */
        public function allGetIndpEnvCallsRelatedToHostNamesCallIsAllowedHostHeaderValue() {
                GeneralUtilityFixture::getIndpEnv('HTTP_HOST');
+               GeneralUtility::flushInternalRuntimeCaches();
                GeneralUtilityFixture::getIndpEnv('TYPO3_HOST_ONLY');
+               GeneralUtility::flushInternalRuntimeCaches();
                GeneralUtilityFixture::getIndpEnv('TYPO3_REQUEST_HOST');
+               GeneralUtility::flushInternalRuntimeCaches();
                GeneralUtilityFixture::getIndpEnv('TYPO3_REQUEST_URL');
                $this->assertSame(4, GeneralUtilityFixture::$isAllowedHostHeaderValueCallCount);
        }
index 3bfad71..06f12f7 100644 (file)
@@ -395,6 +395,7 @@ class UriBuilderTest extends UnitTestCase {
         * @test
         */
        public function buildBackendUriCreatesAbsoluteUrisIfSpecified() {
+               GeneralUtility::flushInternalRuntimeCaches();
                GeneralUtility::_GETset(array('M' => 'moduleKey'));
                $_SERVER['HTTP_HOST'] = 'baseuri';
                $_SERVER['SCRIPT_NAME'] = '/typo3/index.php';
index 518e4f5..afbea45 100644 (file)
@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Felogin\Tests\Unit\Controller;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
 
 /**
@@ -58,6 +59,7 @@ class FrontendLoginControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * Set up a fake site path and host
         */
        protected function setUpFakeSitePathAndHost() {
+               GeneralUtility::flushInternalRuntimeCaches();
                $_SERVER['ORIG_PATH_INFO'] = $_SERVER['PATH_INFO'] = $_SERVER['ORIG_SCRIPT_NAME'] = $_SERVER['SCRIPT_NAME'] = $this->testSitePath . TYPO3_mainDir;
                $_SERVER['HTTP_HOST'] = $this->testHostName;
        }
index dacc096..8379754 100644 (file)
@@ -14,6 +14,8 @@ namespace TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatchi
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
 /**
  * Test case
  */
@@ -31,6 +33,7 @@ class ConditionMatcherTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 
        protected function setUp() {
                $this->testGlobalNamespace = $this->getUniqueId('TEST');
+               GeneralUtility::flushInternalRuntimeCaches();
                $GLOBALS[$this->testGlobalNamespace] = array();
                $GLOBALS['TSFE'] = new \stdClass();
                $GLOBALS['TSFE']->tmpl = new \stdClass();
index 342e661..7a9b0f3 100644 (file)
@@ -14,22 +14,29 @@ namespace TYPO3\CMS\Frontend\Tests\Unit\Controller;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Cache\CacheManager;
+use TYPO3\CMS\Core\Database\DatabaseConnection;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
+use TYPO3\CMS\Frontend\Page\PageRepository;
+
 /**
  * Testcase for TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
  */
 class TypoScriptFrontendControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 
        /**
-        * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
+        * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|TypoScriptFrontendController
         */
        protected $subject;
 
        protected function setUp() {
-               $this->subject = $this->getAccessibleMock(\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class, array('dummy'), array(), '', FALSE);
+               GeneralUtility::flushInternalRuntimeCaches();
+               $this->subject = $this->getAccessibleMock(TypoScriptFrontendController::class, array('dummy'), array(), '', FALSE);
                $this->subject->TYPO3_CONF_VARS = $GLOBALS['TYPO3_CONF_VARS'];
                $this->subject->TYPO3_CONF_VARS['SYS']['encryptionKey'] = '170928423746123078941623042360abceb12341234231';
 
-               $pageRepository = $this->getMock(\TYPO3\CMS\Frontend\Page\PageRepository::class);
+               $pageRepository = $this->getMock(PageRepository::class);
                $this->subject->sys_page = $pageRepository;
        }
 
@@ -59,11 +66,11 @@ class TypoScriptFrontendControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCas
         * Setup a \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController object only for testing the header and footer
         * replacement during USER_INT rendering
         *
-        * @return \PHPUnit_Framework_MockObject_MockObject
+        * @return \PHPUnit_Framework_MockObject_MockObject|TypoScriptFrontendController
         */
        protected function setupTsfeMockForHeaderFooterReplacementCheck() {
-               /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $tsfe */
-               $tsfe = $this->getMock(\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class, array(
+               /** @var \PHPUnit_Framework_MockObject_MockObject|TypoScriptFrontendController $tsfe */
+               $tsfe = $this->getMock(TypoScriptFrontendController::class, array(
                        'INTincScript_process',
                        'INTincScript_includeLibs',
                        'INTincScript_loadJSCode',
@@ -138,9 +145,9 @@ class TypoScriptFrontendControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCas
                                'forced' => 0,
                        ),
                );
-               $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array('exec_SELECTgetRows'));
+               $GLOBALS['TYPO3_DB'] = $this->getMock(DatabaseConnection::class, array('exec_SELECTgetRows'));
                $GLOBALS['TYPO3_DB']->expects($this->any())->method('exec_SELECTgetRows')->willReturn($domainRecords);
-               \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('cache_runtime')->flush();
+               GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime')->flush();
                $expectedResult = array(
                        $domainRecords[$currentDomain]['pid'] => $domainRecords[$currentDomain],
                );
@@ -174,9 +181,9 @@ class TypoScriptFrontendControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCas
                                'forced' => 0,
                        ),
                );
-               $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array('exec_SELECTgetRows'));
+               $GLOBALS['TYPO3_DB'] = $this->getMock(DatabaseConnection::class, array('exec_SELECTgetRows'));
                $GLOBALS['TYPO3_DB']->expects($this->any())->method('exec_SELECTgetRows')->willReturn($domainRecords);
-               \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('cache_runtime')->flush();
+               GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime')->flush();
                $expectedResult = array(
                        $domainRecords[$currentDomain]['pid'] => $domainRecords['foo.bar'],
                );