Skip to content

Commit

Permalink
Merge branch '3.2.x' into 4.0.x
Browse files Browse the repository at this point in the history
* 3.2.x:
  PHPStan 1.2.0, Psalm 4.13.0
  Refactor query caching tests
  Always cache the full result
  Fix getServerVersion for OCI8 when assertions are disabled
  • Loading branch information
derrabus committed Nov 21, 2021
2 parents bf784c7 + 263ddfa commit fc4b0c4
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 640 deletions.
6 changes: 3 additions & 3 deletions composer.json
Expand Up @@ -40,15 +40,15 @@
"require-dev": {
"doctrine/coding-standard": "9.0.0",
"jetbrains/phpstorm-stubs": "2021.1",
"phpstan/phpstan": "1.1.1",
"phpstan/phpstan": "1.2.0",
"phpstan/phpstan-phpunit": "1.0.0",
"phpstan/phpstan-strict-rules": "1.0.0",
"phpstan/phpstan-strict-rules": "1.1.0",
"phpunit/phpunit": "9.5.10",
"psalm/plugin-phpunit": "0.16.1",
"squizlabs/php_codesniffer": "3.6.1",
"symfony/cache": "^5.2|^6.0",
"symfony/console": "^2.0.5|^3.0|^4.0|^5.0|^6.0",
"vimeo/psalm": "4.12.0"
"vimeo/psalm": "4.13.0"
},
"suggest": {
"symfony/console": "For helpful console commands such as SQL execution and import of files."
Expand Down
24 changes: 1 addition & 23 deletions docs/en/reference/caching.rst
@@ -1,15 +1,14 @@
Caching
=======

A ``Doctrine\DBAL\Statement`` can automatically cache result sets. The
A ``Doctrine\DBAL\Connection`` can automatically cache result sets. The
feature is optional though, and by default, no result set is cached.

To use the result cache, there are three mandatory steps:

1. Configure a global result cache, or provide one at query time.
2. Provide a cache profile for the result set you want to cache when
making a query.
3. Read the entire result set from the database.

Configuring the result cache
----------------------------
Expand Down Expand Up @@ -53,24 +52,3 @@ default cache instance:
<?php
$cache = new \Symfony\Component\Cache\Adapter\FilesystemAdapter();
new QueryCacheProfile(0, "some key", $cache);

Reading the entire result set
-----------------------------

Caching half a result set would cause bugs if a subsequent caller needed
more rows from that same result sets. To be able to cache the entire
result set, it must be fetched entirely from the database, and not all
APIs do that. The easiest way to ensure that is to use one of the
``fetchAll*()`` methods:

::

<?php
$stmt = $conn->executeCacheQuery($query, $params, $types, new QueryCacheProfile(0, "some key"));
$data = $stmt->fetchAllAssociative();

.. warning::

When using the cache layer not all fetch modes are supported. See
the code of the ``Doctrine\DBAL\Cache\CachingResult`` for
details.
6 changes: 0 additions & 6 deletions psalm.xml.dist
Expand Up @@ -195,12 +195,6 @@
<file name="tests/Functional/Schema/SchemaManagerFunctionalTestCase.php"/>
</errorLevel>
</TypeDoesNotContainNull>
<TypeDoesNotContainType>
<errorLevel type="suppress">
<!-- See https://github.com/vimeo/psalm/issues/4274 -->
<file name="src/Schema/Index.php"/>
</errorLevel>
</TypeDoesNotContainType>
<UndefinedConstant>
<errorLevel type="suppress">
<!--
Expand Down
182 changes: 0 additions & 182 deletions src/Cache/CachingResult.php

This file was deleted.

35 changes: 17 additions & 18 deletions src/Connection.php
Expand Up @@ -8,7 +8,6 @@
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Cache\ArrayResult;
use Doctrine\DBAL\Cache\CacheException;
use Doctrine\DBAL\Cache\CachingResult;
use Doctrine\DBAL\Cache\Exception\NoResultDriverConfigured;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Connection\StaticServerVersionProvider;
Expand All @@ -35,7 +34,6 @@
use Throwable;
use Traversable;

use function array_key_exists;
use function assert;
use function count;
use function implode;
Expand Down Expand Up @@ -906,30 +904,31 @@ public function executeCacheQuery(string $sql, array $params, array $types, Quer

[$cacheKey, $realKey] = $qcp->generateCacheKeys($sql, $params, $types, $connectionParams);

// fetch the row pointers entry
$item = $resultCache->getItem($cacheKey);

if ($item->isHit()) {
$data = $item->get();
// is the real key part of this row pointers map or is the cache only pointing to other cache keys?
if (isset($data[$realKey])) {
$result = new ArrayResult($data[$realKey]);
} elseif (array_key_exists($realKey, $data)) {
$result = new ArrayResult([]);
$value = $item->get();
if (isset($value[$realKey])) {
return new Result(new ArrayResult($value[$realKey]), $this);
}
} else {
$value = [];
}

if (! isset($result)) {
$result = new CachingResult(
$this->executeQuery($sql, $params, $types),
$resultCache,
$cacheKey,
$realKey,
$qcp->getLifetime()
);
$data = $this->fetchAllAssociative($sql, $params, $types);

$value[$realKey] = $data;

$item->set($value);

$lifetime = $qcp->getLifetime();
if ($lifetime > 0) {
$item->expiresAfter($lifetime);
}

return new Result($result, $this);
$resultCache->save($item);

return new Result(new ArrayResult($data), $this);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Driver/OCI8/Connection.php
Expand Up @@ -49,7 +49,8 @@ public function getServerVersion(): string
throw Error::new($this->connection);
}

assert(preg_match('/\s+(\d+\.\d+\.\d+\.\d+\.\d+)\s+/', $version, $matches) === 1);
$result = preg_match('/\s+(\d+\.\d+\.\d+\.\d+\.\d+)\s+/', $version, $matches);
assert($result === 1);

return $matches[1];
}
Expand Down

0 comments on commit fc4b0c4

Please sign in to comment.