Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch cache configuration to PSR-6 #9002

Merged
merged 1 commit into from
Sep 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions lib/Doctrine/ORM/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,15 +543,15 @@ public function setHydrationCacheProfile(?QueryCacheProfile $profile = null)
// DBAL < 3.2
if (! method_exists(QueryCacheProfile::class, 'setResultCache')) {
if (! $profile->getResultCacheDriver()) {
$defaultHydrationCacheImpl = $this->_em->getConfiguration()->getHydrationCacheImpl();
$defaultHydrationCacheImpl = $this->_em->getConfiguration()->getHydrationCache();
if ($defaultHydrationCacheImpl) {
$profile = $profile->setResultCacheDriver($defaultHydrationCacheImpl);
$profile = $profile->setResultCacheDriver(DoctrineProvider::wrap($defaultHydrationCacheImpl));
}
}
} elseif (! $profile->getResultCache()) {
$defaultHydrationCacheImpl = $this->_em->getConfiguration()->getHydrationCacheImpl();
$defaultHydrationCacheImpl = $this->_em->getConfiguration()->getHydrationCache();
if ($defaultHydrationCacheImpl) {
$profile = $profile->setResultCache(CacheAdapter::wrap($defaultHydrationCacheImpl));
$profile = $profile->setResultCache($defaultHydrationCacheImpl);
}
}

Expand Down Expand Up @@ -587,9 +587,9 @@ public function setResultCacheProfile(?QueryCacheProfile $profile = null)
// DBAL < 3.2
if (! method_exists(QueryCacheProfile::class, 'setResultCache')) {
if (! $profile->getResultCacheDriver()) {
$defaultResultCacheDriver = $this->_em->getConfiguration()->getResultCacheImpl();
$defaultResultCacheDriver = $this->_em->getConfiguration()->getResultCache();
if ($defaultResultCacheDriver) {
$profile = $profile->setResultCacheDriver($defaultResultCacheDriver);
$profile = $profile->setResultCacheDriver(DoctrineProvider::wrap($defaultResultCacheDriver));
}
}
} elseif (! $profile->getResultCache()) {
Expand Down Expand Up @@ -727,15 +727,33 @@ public function disableResultCache(): self
*
* @param int|null $lifetime How long the cache entry is valid, in seconds.
*
* @return static This query instance.
* @return $this
*/
public function setResultCacheLifetime($lifetime)
{
$lifetime = $lifetime !== null ? (int) $lifetime : 0;
$lifetime = (int) $lifetime;

$this->_queryCacheProfile = $this->_queryCacheProfile
? $this->_queryCacheProfile->setLifetime($lifetime)
: new QueryCacheProfile($lifetime, null, $this->_em->getConfiguration()->getResultCacheImpl());
if ($this->_queryCacheProfile) {
$this->_queryCacheProfile = $this->_queryCacheProfile->setLifetime($lifetime);

return $this;
}

$this->_queryCacheProfile = new QueryCacheProfile($lifetime);

$cache = $this->_em->getConfiguration()->getResultCache();
if (! $cache) {
return $this;
}

// Compatibility for DBAL < 3.2
if (! method_exists($this->_queryCacheProfile, 'setResultCache')) {
$this->_queryCacheProfile = $this->_queryCacheProfile->setResultCacheDriver(DoctrineProvider::wrap($cache));

return $this;
}

$this->_queryCacheProfile = $this->_queryCacheProfile->setResultCache($cache);

return $this;
}
Expand Down
98 changes: 97 additions & 1 deletion lib/Doctrine/ORM/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Doctrine\ORM\Exception\InvalidEntityRepository;
use Doctrine\ORM\Exception\NamedNativeQueryNotFound;
use Doctrine\ORM\Exception\NamedQueryNotFound;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\Exception\ProxyClassesAlwaysRegenerating;
use Doctrine\ORM\Exception\UnknownEntityNamespace;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
Expand All @@ -43,6 +42,7 @@
use ReflectionClass;

use function class_exists;
use function method_exists;
use function strtolower;
use function trim;

Expand Down Expand Up @@ -251,46 +251,142 @@ public function getMetadataDriverImpl()
return $this->_attributes['metadataDriverImpl'] ?? null;
}

/**
* Gets the cache driver implementation that is used for query result caching.
*/
public function getResultCache(): ?CacheItemPoolInterface
{
// Compatibility with DBAL < 3.2
if (! method_exists(parent::class, 'getResultCache')) {
$cacheImpl = $this->getResultCacheImpl();

return $cacheImpl ? CacheAdapter::wrap($cacheImpl) : null;
}

return parent::getResultCache();
}

/**
* Sets the cache driver implementation that is used for query result caching.
*/
public function setResultCache(CacheItemPoolInterface $cache): void
{
// Compatibility with DBAL < 3.2
if (! method_exists(parent::class, 'setResultCache')) {
$this->setResultCacheImpl(DoctrineProvider::wrap($cache));

return;
}

parent::setResultCache($cache);
}

/**
* Gets the cache driver implementation that is used for the query cache (SQL cache).
*
* @deprecated Call {@see getQueryCache()} instead.
*
* @return CacheDriver|null
*/
public function getQueryCacheImpl()
{
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/9002',
'Method %s() is deprecated and will be removed in Doctrine ORM 3.0. Use getQueryCache() instead.',
__METHOD__
);

return $this->_attributes['queryCacheImpl'] ?? null;
}

/**
* Sets the cache driver implementation that is used for the query cache (SQL cache).
*
* @deprecated Call {@see setQueryCache()} instead.
*
* @return void
*/
public function setQueryCacheImpl(CacheDriver $cacheImpl)
{
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/9002',
'Method %s() is deprecated and will be removed in Doctrine ORM 3.0. Use setQueryCache() instead.',
__METHOD__
);

$this->_attributes['queryCache'] = CacheAdapter::wrap($cacheImpl);
$this->_attributes['queryCacheImpl'] = $cacheImpl;
}

/**
* Gets the cache driver implementation that is used for the query cache (SQL cache).
*/
public function getQueryCache(): ?CacheItemPoolInterface
{
return $this->_attributes['queryCache'] ?? null;
}

/**
* Sets the cache driver implementation that is used for the query cache (SQL cache).
*/
public function setQueryCache(CacheItemPoolInterface $cache): void
{
$this->_attributes['queryCache'] = $cache;
$this->_attributes['queryCacheImpl'] = DoctrineProvider::wrap($cache);
}

/**
* Gets the cache driver implementation that is used for the hydration cache (SQL cache).
*
* @deprecated Call {@see getHydrationCache()} instead.
*
* @return CacheDriver|null
*/
public function getHydrationCacheImpl()
{
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/9002',
'Method %s() is deprecated and will be removed in Doctrine ORM 3.0. Use getHydrationCache() instead.',
__METHOD__
);

return $this->_attributes['hydrationCacheImpl'] ?? null;
}

/**
* Sets the cache driver implementation that is used for the hydration cache (SQL cache).
*
* @deprecated Call {@see setHydrationCache()} instead.
*
* @return void
*/
public function setHydrationCacheImpl(CacheDriver $cacheImpl)
{
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/9002',
'Method %s() is deprecated and will be removed in Doctrine ORM 3.0. Use setHydrationCache() instead.',
__METHOD__
);

$this->_attributes['hydrationCache'] = CacheAdapter::wrap($cacheImpl);
$this->_attributes['hydrationCacheImpl'] = $cacheImpl;
}

public function getHydrationCache(): ?CacheItemPoolInterface
{
return $this->_attributes['hydrationCache'] ?? null;
}

public function setHydrationCache(CacheItemPoolInterface $cache): void
{
$this->_attributes['hydrationCache'] = $cache;
$this->_attributes['hydrationCacheImpl'] = DoctrineProvider::wrap($cache);
}

/**
* Gets the cache driver implementation that is used for metadata caching.
*
Expand Down
5 changes: 4 additions & 1 deletion lib/Doctrine/ORM/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ORM;

use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\LockMode;
Expand Down Expand Up @@ -497,7 +498,9 @@ public function getQueryCacheDriver(): ?Cache
return $this->queryCache;
}

return $this->_em->getConfiguration()->getQueryCacheImpl();
$queryCache = $this->_em->getConfiguration()->getQueryCache();

return $queryCache ? DoctrineProvider::wrap($queryCache) : null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\ORM\Tools\Console\Command\AbstractEntityManagerCommand;
use InvalidArgumentException;
use LogicException;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -65,18 +66,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
$ui = new SymfonyStyle($input, $output);

$em = $this->getEntityManager($input);
$cache = $em->getConfiguration()->getQueryCache();
$cacheDriver = $em->getConfiguration()->getQueryCacheImpl();

if (! $cacheDriver) {
throw new InvalidArgumentException('No Query cache driver is configured on given EntityManager.');
}

if ($cacheDriver instanceof ApcCache) {
throw new LogicException('Cannot clear APC Cache from Console, its shared in the Webserver memory and not accessible from the CLI.');
if ($cacheDriver instanceof ApcCache || $cache instanceof ApcuAdapter) {
throw new LogicException('Cannot clear APCu Cache from Console, it\'s shared in the Webserver memory and not accessible from the CLI.');
}

if ($cacheDriver instanceof XcacheCache) {
throw new LogicException('Cannot clear XCache Cache from Console, its shared in the Webserver memory and not accessible from the CLI.');
throw new LogicException('Cannot clear XCache Cache from Console, it\'s shared in the Webserver memory and not accessible from the CLI.');
}

if (! ($cacheDriver instanceof ClearableCache)) {
Expand All @@ -88,10 +90,10 @@ protected function execute(InputInterface $input, OutputInterface $output)

$ui->comment('Clearing <info>all</info> Query cache entries');

$result = $cacheDriver->deleteAll();
$result = $cache ? $cache->clear() : $cacheDriver->deleteAll();
$message = $result ? 'Successfully deleted cache entries.' : 'No cache entries were deleted.';

if ($input->getOption('flush') === true) {
if ($input->getOption('flush') === true && ! $cache) {
if (! ($cacheDriver instanceof FlushableCache)) {
throw new LogicException(sprintf(
'Can only clear cache when FlushableCache interface is implemented, %s does not implement.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$ui = new SymfonyStyle($input, $output);

$em = $this->getEntityManager($input);
$cache = method_exists(Configuration::class, 'getResultCache') ? $em->getConfiguration()->getResultCache() : null;
$cache = $em->getConfiguration()->getResultCache();
$cacheDriver = method_exists(Configuration::class, 'getResultCacheImpl') ? $em->getConfiguration()->getResultCacheImpl() : null;

if (! $cacheDriver && ! $cache) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Tools/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ public static function createConfiguration($isDevMode = false, $proxyDir = null,
$config = new Configuration();

$config->setMetadataCache(CacheAdapter::wrap($cache));
$config->setQueryCacheImpl($cache);
$config->setResultCacheImpl($cache);
$config->setQueryCache(CacheAdapter::wrap($cache));
$config->setResultCache(CacheAdapter::wrap($cache));
$config->setProxyDir($proxyDir);
$config->setProxyNamespace('DoctrineProxies');
$config->setAutoGenerateProxyClasses($isDevMode);
Expand Down
5 changes: 4 additions & 1 deletion phpstan-dbal2.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ parameters:
- '/Class Doctrine\\DBAL\\Platforms\\PostgreSqlPlatform referenced with incorrect case: Doctrine\\DBAL\\Platforms\\PostgreSQLPlatform\./'

# Forward compatibility for DBAL 3.2
- '/^Call to an undefined method Doctrine\\.*::[gs]etResultCache\(\)\.$/'
- '/^Call to an undefined method Doctrine\\DBAL\\Cache\\QueryCacheProfile::[gs]etResultCache\(\)\.$/'
-
message: '/^Call to an undefined static method Doctrine\\DBAL\\Configuration::[gs]etResultCache\(\)\.$/'
path: lib/Doctrine/ORM/Configuration.php
-
message: '/^Parameter #3 \$resultCache of class Doctrine\\DBAL\\Cache\\QueryCacheProfile constructor/'
path: lib/Doctrine/ORM/AbstractQuery.php
Expand Down
5 changes: 4 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ parameters:
path: lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php

# Forward compatibility for DBAL 3.2
- '/^Call to an undefined method Doctrine\\.*::[gs]etResultCache\(\)\.$/'
- '/^Call to an undefined method Doctrine\\DBAL\\Cache\\QueryCacheProfile::[gs]etResultCache\(\)\.$/'
-
message: '/^Call to an undefined static method Doctrine\\DBAL\\Configuration::[gs]etResultCache\(\)\.$/'
path: lib/Doctrine/ORM/Configuration.php
-
message: '/^Parameter #3 \$resultCache of class Doctrine\\DBAL\\Cache\\QueryCacheProfile constructor/'
path: lib/Doctrine/ORM/AbstractQuery.php
Expand Down
Loading