diff --git a/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php b/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php index 53d43117ef3..27e05d88267 100644 --- a/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php +++ b/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php @@ -82,19 +82,8 @@ public function __construct(ResultStatement $stmt, Cache $resultCache, $cacheKey public function closeCursor() { $this->statement->closeCursor(); - if (! $this->emptied || $this->data === null) { - return true; - } - - $data = $this->resultCache->fetch($this->cacheKey); - if (! $data) { - $data = []; - } - $data[$this->realKey] = $this->data; - - $this->resultCache->save($this->cacheKey, $data, $this->lifetime); - unset($this->data); + $this->data = null; return true; } @@ -169,6 +158,7 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX } $this->emptied = true; + $this->saveToCache(); return false; } @@ -184,6 +174,7 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n $this->data = $data; $this->emptied = true; + $this->saveToCache(); if ($fetchMode === FetchMode::NUMERIC) { foreach ($data as $i => $row) { @@ -327,6 +318,7 @@ private function doFetch() } $this->emptied = true; + $this->saveToCache(); return false; } @@ -339,4 +331,20 @@ private function store(array $data) : void $this->data = $data; $this->emptied = true; } + + private function saveToCache() : void + { + if ($this->data === null) { + return; + } + + $data = $this->resultCache->fetch($this->cacheKey); + if (! $data) { + $data = []; + } + + $data[$this->realKey] = $this->data; + + $this->resultCache->save($this->cacheKey, $data, $this->lifetime); + } } diff --git a/tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php b/tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php index 1683f154bea..3623beb9db1 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php @@ -133,7 +133,7 @@ private function assertStandardAndIteratorFetchAreEqual(int $fetchMode) : void self::assertEquals($data, $dataIterator); } - public function testDontCloseNoCache() : void + public function testFetchAndFinishSavesCache() : void { $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey')); @@ -151,7 +151,7 @@ public function testDontCloseNoCache() : void $data[] = $row; } - self::assertCount(2, $this->sqlLogger->queries); + self::assertCount(1, $this->sqlLogger->queries); } public function testDontFinishNoCache() : void @@ -159,7 +159,6 @@ public function testDontFinishNoCache() : void $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey')); $stmt->fetch(FetchMode::ASSOCIATIVE); - $stmt->closeCursor(); $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey')); @@ -168,12 +167,11 @@ public function testDontFinishNoCache() : void self::assertCount(2, $this->sqlLogger->queries); } - public function testFetchAllAndFinishSavesCache() : void + public function testFetchAllSavesCache() : void { $layerCache = new ArrayCache(); $stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'testcachekey', $layerCache)); $stmt->fetchAll(); - $stmt->closeCursor(); self::assertCount(1, $layerCache->fetch('testcachekey')); } @@ -187,7 +185,6 @@ public function testFetchAllColumn() : void $stmt = $this->connection->executeCacheQuery($query, [], [], $qcp); $stmt->fetchAll(FetchMode::COLUMN); - $stmt->closeCursor(); $stmt = $this->connection->executeCacheQuery($query, [], [], $qcp); @@ -249,8 +246,6 @@ private function hydrateStmt(ResultStatement $stmt, int $fetchMode = FetchMode:: $data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row; } - $stmt->closeCursor(); - return $data; } @@ -265,8 +260,6 @@ private function hydrateStmtIterator(ResultStatement $stmt, int $fetchMode = Fet $data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row; } - $stmt->closeCursor(); - return $data; } }