Skip to content

Commit

Permalink
Added keep-in-memory option for cache adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
pborreli committed Apr 7, 2019
1 parent 47e571b commit 7177a42
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
Expand Up @@ -995,6 +995,7 @@ private function addCacheSection(ArrayNodeDefinition $rootNode)
->info('Overwrite the setting from the default provider for this adapter.')
->end()
->scalarNode('clearer')->end()
->booleanNode('keep_in_memory')->defaultFalse()->end()
->end()
->end()
->validate()
Expand Down
Expand Up @@ -28,6 +28,7 @@
use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\ChainAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
use Symfony\Component\Cache\DependencyInjection\CachePoolPass;
use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
Expand Down Expand Up @@ -1797,6 +1798,17 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
$name = '.'.$name.'.inner';
}
$definition->setPublic($pool['public']);

if ($config['pools'][$name]['keep_in_memory'] ?? false) {
$container->register($name.'.keep_in_memory', ChainAdapter::class)
->setDecoratedService($name)
->addArgument([
new Definition(ArrayAdapter::class),
new Reference($name.'.keep_in_memory.inner'),
])
->setPublic(true);
}

unset($pool['adapter'], $pool['public'], $pool['tags']);

$definition->addTag('cache.pool', $pool);
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Cache/DependencyInjection/CachePoolPass.php
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\ChainAdapter;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -104,6 +105,19 @@ public function process(ContainerBuilder $container)
}
unset($tags[0][$attr]);
}

if ($tags[0]['keep_in_memory'] ?? false) {
$container->register($id.'.keep_in_memory', ChainAdapter::class)
->setDecoratedService($id)
->addArgument([
new Definition(ArrayAdapter::class),
new Reference($id.'.keep_in_memory.inner'),
])
->setPublic(true);
}

unset($tags[0]['keep_in_memory']);

if (!empty($tags[0])) {
throw new InvalidArgumentException(sprintf('Invalid "%s" tag for service "%s": accepted attributes are "clearer", "provider", "name", "namespace", "default_lifetime" and "reset", found "%s".', $this->cachePoolTag, $id, implode('", "', array_keys($tags[0]))));
}
Expand Down

0 comments on commit 7177a42

Please sign in to comment.