Skip to content

Commit c8b43bb

Browse files
jankonasf3l1x
authored andcommitted
Add option to turn off second level cache
1 parent df44812 commit c8b43bb

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

.docs/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ nettrine.orm.cache:
102102
secondLevelCache: @cacheConfigurationFactory::create('bar')
103103
```
104104

105+
You can turn off `secondLevelCache` by setting it to `false`:
106+
107+
```neon
108+
nettrine.orm.cache:
109+
secondLevelCache: false
110+
```
105111

106112
### `symfony/console`
107113

src/DI/OrmCacheExtension.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function getConfigSchema(): Schema
2626
'hydrationCache' => $this->getServiceSchema(),
2727
'metadataCache' => $this->getServiceSchema(),
2828
'resultCache' => $this->getServiceSchema(),
29-
'secondLevelCache' => $this->getServiceSchema(),
29+
'secondLevelCache' => Expect::anyOf($this->getServiceSchema(), false),
3030
]);
3131
}
3232

@@ -93,8 +93,13 @@ private function loadMetadataCacheConfiguration(): void
9393

9494
private function loadSecondLevelCacheConfiguration(): void
9595
{
96-
$builder = $this->getContainerBuilder();
9796
$config = $this->config;
97+
98+
if ($config->secondLevelCache === false) {
99+
return;
100+
}
101+
102+
$builder = $this->getContainerBuilder();
98103
$configurationDef = $this->getConfigurationDef();
99104

100105
if ($config->secondLevelCache !== null) {

tests/Cases/DI/OrmCacheExtension.phpt

+29
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Toolkit::test(function (): void {
3030
Assert::type(PhpFileCache::class, $em->getConfiguration()->getMetadataCacheImpl());
3131
Assert::type(PhpFileCache::class, $em->getConfiguration()->getQueryCacheImpl());
3232
Assert::type(PhpFileCache::class, $em->getConfiguration()->getResultCacheImpl());
33+
Assert::true($em->getConfiguration()->isSecondLevelCacheEnabled());
34+
Assert::notNull($em->getConfiguration()->getSecondLevelCacheConfiguration());
3335
});
3436

3537
// Provide cache drivers
@@ -57,4 +59,31 @@ Toolkit::test(function (): void {
5759
Assert::type(ArrayCache::class, $em->getConfiguration()->getMetadataCacheImpl());
5860
Assert::type(ApcuCache::class, $em->getConfiguration()->getQueryCacheImpl());
5961
Assert::type(ArrayCache::class, $em->getConfiguration()->getResultCacheImpl());
62+
Assert::true($em->getConfiguration()->isSecondLevelCacheEnabled());
63+
Assert::notNull($em->getConfiguration()->getSecondLevelCacheConfiguration());
64+
});
65+
66+
// Turn off second level cache
67+
Toolkit::test(function (): void {
68+
$container = Container::of()
69+
->withDefaults()
70+
->withCompiler(function (Compiler $compiler): void {
71+
$compiler->addExtension('nettrine.orm.cache', new OrmCacheExtension());
72+
$compiler->addConfig([
73+
'nettrine.orm.cache' => [
74+
'secondLevelCache' => false,
75+
],
76+
]);
77+
})
78+
->build();
79+
80+
/** @var EntityManagerDecorator $em */
81+
$em = $container->getByType(EntityManagerDecorator::class);
82+
83+
Assert::false($em->getConfiguration()->isSecondLevelCacheEnabled());
84+
Assert::null($em->getConfiguration()->getSecondLevelCacheConfiguration());
85+
Assert::type(PhpFileCache::class, $em->getConfiguration()->getHydrationCacheImpl());
86+
Assert::type(PhpFileCache::class, $em->getConfiguration()->getMetadataCacheImpl());
87+
Assert::type(PhpFileCache::class, $em->getConfiguration()->getQueryCacheImpl());
88+
Assert::type(PhpFileCache::class, $em->getConfiguration()->getResultCacheImpl());
6089
});

0 commit comments

Comments
 (0)