Skip to content

Commit

Permalink
Deprecate using doctrine/cache for metadata caching (#2253)
Browse files Browse the repository at this point in the history
* Deprecate using doctrine/cache for metadata caching

* Use common syntax for multiple dependency versions

* Add todo comment for removing doctrine/persistence compat code
  • Loading branch information
alcaeus authored Apr 23, 2021
1 parent b0989bc commit 8e8df77
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 21 deletions.
8 changes: 8 additions & 0 deletions UPGRADE-2.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ Using `@Index` annotation(s) on a class level is a preferred way for defining
indexes for documents. Using `@Index` in the `@Indexes` annotation or an `indexes`
property of other annotations was deprecated and will be removed in ODM 3.0.

## DocumentManager configuration

Using doctrine/cache to cache metadata is deprecated in favor of using PSR-6.
The `getMetadataCacheImpl` and `setMetadataCacheImpl` methods in
`Doctrine\ODM\MongoDB\Configuration` have been deprecated. Please use
`getMetadataCache`and `setMetadataCache` with a PSR-6 implementation instead.
Note that even after switching to PSR-6, `getMetadataCacheImpl` will return a
cache instance that wraps the PSR-6 cache.
4 changes: 2 additions & 2 deletions benchmark/BaseBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Doctrine\ODM\MongoDB\Benchmark;

use Doctrine\Common\Cache\ArrayCache;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use MongoDB\Client;
use MongoDB\Model\DatabaseInfo;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

use function array_map;
use function getenv;
Expand Down Expand Up @@ -48,7 +48,7 @@ public function initDocumentManager()
$config->setPersistentCollectionNamespace('PersistentCollections');
$config->setDefaultDB(self::DATABASE_NAME);
$config->setMetadataDriverImpl(self::createMetadataDriverImpl());
$config->setMetadataCacheImpl(new ArrayCache());
$config->setMetadataCache(new ArrayAdapter());

$client = new Client(
getenv('DOCTRINE_MONGODB_SERVER') ?: self::DEFAULT_MONGODB_SERVER,
Expand Down
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@
"php": "^7.2 || ^8.0",
"ext-mongodb": "^1.5",
"doctrine/annotations": "^1.6",
"doctrine/cache": "^1.7",
"doctrine/cache": "^1.11 || ^2.0",
"doctrine/collections": "^1.5",
"doctrine/event-manager": "^1.0",
"doctrine/instantiator": "^1.1",
"doctrine/persistence": "^1.3.5|^2.0",
"doctrine/persistence": "^1.3.5 || ^2.0",
"friendsofphp/proxy-manager-lts": "^1.0",
"jean85/pretty-package-versions": "^1.3.0 || ^2.0.1",
"mongodb/mongodb": "^1.2.0",
"symfony/console": "^3.4|^4.1|^5.0",
"psr/cache": "^1.0 || ^2.0 || ^3.0",
"symfony/console": "^3.4 || ^4.1 || ^5.0",
"symfony/deprecation-contracts": "^2.2",
"symfony/var-dumper": "^3.4|^4.1|^5.0"
"symfony/var-dumper": "^3.4 || ^4.1 || ^5.0"
},
"require-dev": {
"ext-bcmath": "*",
Expand All @@ -45,6 +46,7 @@
"phpstan/phpstan": "^0.12.32",
"phpunit/phpunit": "^8.5 || ^9",
"squizlabs/php_codesniffer": "^3.5",
"symfony/cache": "^4.4 || ^5.0",
"vimeo/psalm": "^4.2.1"
},
"suggest": {
Expand Down
35 changes: 35 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\Psr6\CacheAdapter;
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use Doctrine\ODM\MongoDB\PersistentCollection\DefaultPersistentCollectionFactory;
Expand All @@ -25,9 +27,11 @@
use ProxyManager\Factory\LazyLoadingGhostFactory;
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy;
use Psr\Cache\CacheItemPoolInterface;
use ReflectionClass;

use function interface_exists;
use function trigger_deprecation;
use function trim;

/**
Expand Down Expand Up @@ -80,6 +84,9 @@ class Configuration
*/
private $attributes = [];

/** @var CacheItemPoolInterface|null */
private $metadataCache;

/** @var ProxyManagerConfiguration */
private $proxyManagerConfiguration;

Expand Down Expand Up @@ -161,12 +168,40 @@ public function getMetadataDriverImpl(): ?MappingDriver

public function getMetadataCacheImpl(): ?Cache
{
trigger_deprecation(
'doctrine/mongodb-odm',
'2.2',
'Using "%s" is deprecated. Please use "%s::getMetadataCache" instead.',
__METHOD__,
self::class
);

return $this->attributes['metadataCacheImpl'] ?? null;
}

public function setMetadataCacheImpl(Cache $cacheImpl): void
{
trigger_deprecation(
'doctrine/mongodb-odm',
'2.2',
'Using "%s" is deprecated. Please use "%s::setMetadataCache" instead.',
__METHOD__,
self::class
);

$this->attributes['metadataCacheImpl'] = $cacheImpl;
$this->metadataCache = CacheAdapter::wrap($cacheImpl);
}

public function getMetadataCache(): ?CacheItemPoolInterface
{
return $this->metadataCache;
}

public function setMetadataCache(CacheItemPoolInterface $cache): void
{
$this->metadataCache = $cache;
$this->attributes['metadataCacheImpl'] = DoctrineProvider::wrap($cache);
}

/**
Expand Down
11 changes: 9 additions & 2 deletions lib/Doctrine/ODM/MongoDB/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ODM\MongoDB;

use Doctrine\Common\Cache\Psr6\DoctrineProvider;
use Doctrine\Common\EventManager;
use Doctrine\ODM\MongoDB\Hydrator\HydratorFactory;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
Expand Down Expand Up @@ -35,6 +36,7 @@
use function gettype;
use function is_object;
use function ltrim;
use function method_exists;
use function sprintf;

/**
Expand Down Expand Up @@ -178,9 +180,14 @@ protected function __construct(?Client $client = null, ?Configuration $config =
$this->metadataFactory->setDocumentManager($this);
$this->metadataFactory->setConfiguration($this->config);

$cacheDriver = $this->config->getMetadataCacheImpl();
$cacheDriver = $this->config->getMetadataCache();
if ($cacheDriver) {
$this->metadataFactory->setCacheDriver($cacheDriver);
// Todo: check can be removed when doctrine/persistence 2.2 is required
if (method_exists($this->metadataFactory, 'setCache')) {
$this->metadataFactory->setCache($cacheDriver);
} else {
$this->metadataFactory->setCacheDriver(DoctrineProvider::wrap($cacheDriver));
}
}

$hydratorDir = $this->config->getHydratorDir();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

namespace Doctrine\ODM\MongoDB\Tools\Console\Command\ClearCache;

use Doctrine\Common\Cache\ApcCache;
use Doctrine\ODM\MongoDB\DocumentManager;
use InvalidArgumentException;
use LogicException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use function assert;

use const PHP_EOL;

/**
Expand Down Expand Up @@ -38,20 +39,18 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$dm = $this->getHelper('documentManager')->getDocumentManager();
$cacheDriver = $dm->getConfiguration()->getMetadataCacheImpl();
$dm = $this->getHelper('documentManager')->getDocumentManager();
assert($dm instanceof DocumentManager);

$cacheDriver = $dm->getConfiguration()->getMetadataCache();

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

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.');
}

$output->write('Clearing ALL Metadata cache entries' . PHP_EOL);

$success = $cacheDriver->deleteAll();
$success = $cacheDriver->clear();

if ($success) {
$output->write('The cache entries were successfully deleted.' . PHP_EOL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Doctrine\ODM\MongoDB\Tools\Console\Command\Schema;

use Doctrine\Common\Cache\VoidCache;
use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -40,7 +39,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$dm = $this->getHelper('documentManager')->getDocumentManager();
assert($dm instanceof DocumentManager);
$metadataFactory = $dm->getMetadataFactory();
$metadataFactory->setCacheDriver(new VoidCache());

$errors = 0;
foreach ($metadataFactory->getAllMetadata() as $meta) {
Expand Down
4 changes: 2 additions & 2 deletions tools/sandbox/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use Symfony\Component\Cache\Adapter\ApcuAdapter;

$file = __DIR__ . '/../../vendor/autoload.php';

Expand All @@ -23,8 +24,7 @@
$config->setHydratorDir(__DIR__ . '/Hydrators');
$config->setHydratorNamespace('Hydrators');
$config->setDefaultDB('doctrine_odm_sandbox');
// $config->setLoggerCallable(function(array $log) { print_r($log); });
// $config->setMetadataCacheImpl(new Doctrine\Common\Cache\ApcCache());
$config->setMetadataCache(new ApcuAdapter());
$config->setMetadataDriverImpl(AnnotationDriver::create(__DIR__ . '/Documents'));

$dm = DocumentManager::create(null, $config);

0 comments on commit 8e8df77

Please sign in to comment.