-
-
Notifications
You must be signed in to change notification settings - Fork 455
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
Add PhpArrayAdapter to cache metadata #1196
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\CacheWarmer; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\Mapping\ClassMetadataFactory; | ||
use Symfony\Bundle\FrameworkBundle\CacheWarmer\AbstractPhpFileCacheWarmer; | ||
use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
use Symfony\Component\Cache\DoctrineProvider; | ||
|
||
class DoctrineMetadataCacheWarmer extends AbstractPhpFileCacheWarmer | ||
{ | ||
/** @var EntityManagerInterface */ | ||
private $entityManager; | ||
|
||
public function __construct(EntityManagerInterface $entityManager, string $phpArrayFile) | ||
{ | ||
$this->entityManager = $entityManager; | ||
|
||
parent::__construct($phpArrayFile); | ||
} | ||
|
||
/** | ||
* @param string $cacheDir | ||
*/ | ||
protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter): bool | ||
{ | ||
$metadataFactory = new ClassMetadataFactory(); | ||
$metadataFactory->setEntityManager($this->entityManager); | ||
$metadataFactory->setCacheDriver(new DoctrineProvider($arrayAdapter)); | ||
$metadataFactory->getAllMetadata(); | ||
|
||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection; | ||
|
||
use Doctrine\Bundle\DoctrineBundle\CacheWarmer\DoctrineMetadataCacheWarmer; | ||
use Doctrine\Bundle\DoctrineBundle\Dbal\ManagerRegistryAwareConnectionProvider; | ||
use Doctrine\Bundle\DoctrineBundle\Dbal\RegexSchemaAssetFilter; | ||
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass; | ||
|
@@ -21,6 +22,8 @@ | |
use Symfony\Bridge\Doctrine\SchemaListener\PdoCacheAdapterDoctrineSchemaSubscriber; | ||
use Symfony\Bridge\Doctrine\Validator\DoctrineLoader; | ||
use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
use Symfony\Component\Cache\Adapter\DoctrineAdapter; | ||
use Symfony\Component\Cache\Adapter\PhpArrayAdapter; | ||
use Symfony\Component\Cache\DoctrineProvider; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\Alias; | ||
|
@@ -814,6 +817,12 @@ protected function loadOrmCacheDrivers(array $entityManager, ContainerBuilder $c | |
$this->loadCacheDriver('metadata_cache', $entityManager['name'], $entityManager['metadata_cache_driver'], $container); | ||
$this->loadCacheDriver('result_cache', $entityManager['name'], $entityManager['result_cache_driver'], $container); | ||
$this->loadCacheDriver('query_cache', $entityManager['name'], $entityManager['query_cache_driver'], $container); | ||
|
||
if ($container->getParameter('kernel.debug')) { | ||
return; | ||
} | ||
|
||
$this->registerMetadataPhpArrayCaching($entityManager['name'], $container); | ||
} | ||
|
||
/** | ||
|
@@ -927,4 +936,25 @@ private function createArrayAdapterCachePool(ContainerBuilder $container, string | |
|
||
return $id; | ||
} | ||
|
||
private function registerMetadataPhpArrayCaching(string $entityManagerName, ContainerBuilder $container): void | ||
{ | ||
$metadataCacheAlias = $this->getObjectManagerElementName($entityManagerName . '_metadata_cache'); | ||
$decoratedMetadataCacheServiceId = (string) $container->getAlias($metadataCacheAlias); | ||
$phpArrayCacheDecoratorServiceId = $decoratedMetadataCacheServiceId . '.php_array'; | ||
greg0ire marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$phpArrayFile = '%kernel.cache_dir%' . sprintf('/doctrine/orm/%s_metadata.php', $entityManagerName); | ||
|
||
$container->register(DoctrineMetadataCacheWarmer::class) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ossinkine actually this is not working properly for multiple entity managers. I just tried it on one of my bigger projects that uses 2 managers. Since |
||
->setArguments([new Reference(sprintf('doctrine.orm.%s_entity_manager', $entityManagerName)), $phpArrayFile]) | ||
->addTag('kernel.cache_warmer'); | ||
|
||
$container->setAlias($metadataCacheAlias, $phpArrayCacheDecoratorServiceId); | ||
$container->register($phpArrayCacheDecoratorServiceId, DoctrineProvider::class) | ||
->addArgument( | ||
new Definition(PhpArrayAdapter::class, [ | ||
$phpArrayFile, | ||
new Definition(DoctrineAdapter::class, [new Reference($decoratedMetadataCacheServiceId)]), | ||
]) | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this means we also need to create a new recipe for 2.2?
See https://github.com/symfony/recipes/blob/master/doctrine/doctrine-bundle/1.12/config/packages/prod/doctrine.yaml#L4 (it's used for 2.x currently as well via symlink)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should yes