Skip to content

Commit

Permalink
[TASK] Introduce runtime cache for getIndpEnv()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
liayn authored and helhum committed Aug 24, 2015
1 parent fec8612 commit 3c7802e
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ protected function setUp() {
$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);
}
Expand Down
23 changes: 23 additions & 0 deletions typo3/sysext/core/Classes/Utility/GeneralUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ class GeneralUtility {
'srv', // HHVM with fastcgi
);

/**
* @var array
*/
static protected $indpEnvCache = [];

/*************************
*
* GET/POST Variables
Expand Down Expand Up @@ -3211,6 +3216,10 @@ static public function linkThisUrl($url, array $getParams = array()) {
* @throws \UnexpectedValueException
*/
static public function getIndpEnv($getEnvName) {
if (isset(self::$indpEnvCache[$getEnvName])) {
return self::$indpEnvCache[$getEnvName];
}

/*
Conventions:
output from parse_url():
Expand Down Expand Up @@ -3490,6 +3499,7 @@ static public function getIndpEnv($getEnvName) {
$retVal = $out;
break;
}
self::$indpEnvCache[$getEnvName] = $retVal;
return $retVal;
}

Expand Down Expand Up @@ -4553,6 +4563,19 @@ static public function purgeInstances() {
self::$nonSingletonInstances = array();
}

/**
* 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@

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
*/
class ServerRequestFactoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {

/**
* Set up
*/
protected function setUp() {
GeneralUtility::flushInternalRuntimeCaches();
}

/**
* @test
*/
Expand Down
5 changes: 5 additions & 0 deletions typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1550,6 +1551,7 @@ static public function hostnameAndPortDataProvider() {
* @dataProvider hostnameAndPortDataProvider
*/
public function getIndpEnvTypo3HostOnlyParsesHostnamesAndIpAdresses($httpHost, $expectedIp) {
GeneralUtility::flushInternalRuntimeCaches();
$_SERVER['HTTP_HOST'] = $httpHost;
$this->assertEquals($expectedIp, GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY'));
}
Expand Down Expand Up @@ -1714,8 +1716,11 @@ public function isAllowedHostHeaderValueWorksCorrectlyWithWithServerNamePattern(
*/
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ public function buildBackendUriRespectsSection() {
* @test
*/
public function buildBackendUriCreatesAbsoluteUrisIfSpecified() {
GeneralUtility::flushInternalRuntimeCaches();
GeneralUtility::_GETset(array('M' => 'moduleKey'));
$_SERVER['HTTP_HOST'] = 'baseuri';
$_SERVER['SCRIPT_NAME'] = '/typo3/index.php';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* The TYPO3 project - inspiring people to share!
*/

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
Expand Down Expand Up @@ -58,6 +59,7 @@ protected function setUp() {
* 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* The TYPO3 project - inspiring people to share!
*/

use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Test case
*/
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,29 @@
* 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;
}

Expand Down Expand Up @@ -59,11 +66,11 @@ public function INTincScript_processCallback() {
* 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',
Expand Down Expand Up @@ -138,9 +145,9 @@ public function getSysDomainCacheReturnsCurrentDomainRecord($currentDomain) {
'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],
);
Expand Down Expand Up @@ -174,9 +181,9 @@ public function getSysDomainCacheReturnsForcedDomainRecord($currentDomain) {
'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'],
);
Expand Down

0 comments on commit 3c7802e

Please sign in to comment.