Skip to content
Snippets Groups Projects
Commit 4cd7c504 authored by Mathias Brodala's avatar Mathias Brodala Committed by Markus Klein
Browse files

[BUGFIX] 1st level cache for ``QueryResult::count()``

Counting the results of the ``QueryResultInterface`` proxy returned by
Repository finder methods always executed a ``COUNT query`` even if
the results had been fetched or counted already.

With this patch the number of results is cached as soon as the result
is initialized and/or counted once.

Resolves: #67837
Releases: master, 6.2
Change-Id: I98f680372b845f992ad3d436647b0cf5e460b606
Reviewed-on: http://review.typo3.org/40750


Reviewed-by: default avatarStephan Großberndt <stephan@grossberndt.de>
Reviewed-by: default avatarWouter Wolters <typo3@wouterwolters.nl>
Tested-by: default avatarWouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: default avatarMarkus Klein <markus.klein@typo3.org>
Tested-by: default avatarMarkus Klein <markus.klein@typo3.org>
parent e62326e1
Branches
Tags
No related merge requests found
......@@ -35,6 +35,11 @@ class QueryResult implements QueryResultInterface {
*/
protected $persistenceManager;
/**
* @var int|NULL
*/
protected $numberOfResults;
/**
* @var \TYPO3\CMS\Extbase\Persistence\QueryInterface
*/
......@@ -105,11 +110,14 @@ class QueryResult implements QueryResultInterface {
* @api
*/
public function count() {
if (is_array($this->queryResult)) {
return count($this->queryResult);
} else {
return $this->persistenceManager->getObjectCountByQuery($this->query);
if ($this->numberOfResults === NULL) {
if (is_array($this->queryResult)) {
$this->numberOfResults = count($this->queryResult);
} else {
$this->numberOfResults = $this->persistenceManager->getObjectCountByQuery($this->query);
}
}
return $this->numberOfResults;
}
/**
......
......@@ -129,6 +129,24 @@ class QueryResultTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
$this->assertEquals(2, $queryResult->count());
}
/**
* @test
*/
public function countCountsQueryResultDirectlyIfAlreadyInitialized() {
$this->mockPersistenceManager->expects($this->never())->method('getObjectCountByQuery');
$this->queryResult->toArray();
$this->assertEquals(2, $this->queryResult->count());
}
/**
* @test
*/
public function countOnlyCallsGetObjectCountByQueryOnPersistenceManagerOnce() {
$this->mockPersistenceManager->expects($this->once())->method('getObjectCountByQuery')->will($this->returnValue(2));
$this->queryResult->count();
$this->assertEquals(2, $this->queryResult->count());
}
/**
* @test
*/
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment