8000 [FrameworkBundle] Default to Apcu+Filesystem cache chain by nicolas-grekas · Pull Request #18715 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Default to Apcu+Filesystem cache chain #18715

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

Merged
merged 1 commit into from
May 13, 2016
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 8000
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions UPGRADE-3.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,8 @@ FrameworkBundle
cache service. If you are using `serializer.mapping.cache.apc`, use
`serializer.mapping.cache.doctrine.apc` instead.

* The `framework.serializer.cache` option has been deprecated. Configure the
`cache.serializer` service under `framework.cache.pools` instead.

Before:

```yaml
framework:
serializer:
cache: serializer.mapping.cache.apc
```

After:

```yaml
framework:
cache:
pools:
cache.serializer:
adapter: cache.adapter.apcu
* The `framework.serializer.cache` option has been deprecated. APCu should now
be automatically used when available so you can remove this configuration key.

HttpKernel
----------
Expand Down
22 changes: 2 additions & 20 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,8 @@ FrameworkBundle
* The service `serializer.mapping.cache.apc` has been removed; use
`serializer.mapping.cache.doctrine.apc` instead.

* The `framework.serializer.cache` option has been removed. Configure the
`cache.serializer` service under `framework.cache.pools` instead.

Before:

```yaml
framework:
serializer:
cache: serializer.mapping.cache.apc
```

After:

```yaml
framework:
cache:
pools:
cache.serializer:
adapter: cache.adapter.apcu
```
* The `framework.serializer.cache` option has been removed. APCu should now
be automatically used when available so you can remove this configuration key.

HttpKernel
----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ private function addCacheSection(ArrayNodeDefinition $rootNode)
->end()
->scalarNode('system')
->info('System related cache pools configuration')
->defaultValue('cache.adapter.filesystem')
->defaultValue('cache.adapter.system')
->end()
->scalarNode('directory')->defaultValue('%kernel.cache_dir%/pools')->end()
->scalarNode('default_doctrine_provider')->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,7 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
{
$nonce = substr(str_replace('/', '-', base64_encode(md5(uniqid(mt_rand(), true), true))), 0, -2);
$container->getDefinition('cache.adapter.apcu')->replaceArgument(2, $nonce);
$container->getDefinition('cache.adapter.system')->replaceArgument(2, $nonce);
$container->getDefinition('cache.adapter.filesystem')->replaceArgument(2, $config['directory']);

foreach (array('doctrine', 'psr6', 'redis') as $name) {
Expand Down
13 changes: 12 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<tag name="cache.pool" />
</service>

<service id="cache.system" parent="cache.adapter.filesystem">
<service id="cache.system" parent="cache.adapter.system">
<tag name="cache.pool" />
</service>

Expand All @@ -22,6 +22,17 @@
<tag name="cache.pool" />
</service>

<service id="cache.adapter.system" class="Symfony\Component\Cache\Adapter\AdapterInterface" abstract="true">
<factory class="Symfony\Component\Cache\Adapter\AbstractAdapter" method="createSystemCache" />
<tag name="cache.pool" clearer="cache.default_clearer" />
<tag name="monolog.logger" channel="cache" />
<argument /> <!-- namespace -->
<argument /> <!-- default lifetime -->
<argument /> <!-- nonce -->
<argument>%kernel.cache_dir%/pools</argument>
<argument type="service" id="logger" on-invalid="ignore" />
</service>

<service id="cache.adapter.apcu" class="Symfony\Component\Cache\Adapter\ApcuAdapter" abstract="true">
<tag name="cache.pool" clearer="cache.default_clearer" />
<tag name="monolog.logger" channel="cache" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ protected static function getBundleDefaultConfig()
'cache' => array(
'pools' => array(),
'app' => 'cache.adapter.filesystem',
'system' => 'cache.adapter.filesystem',
'system' => 'cache.adapter.system',
'directory' => '%kernel.cache_dir%/pools',
'default_redis_provider' => 'redis://localhost',
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Cache\Adapter\ChainAdapter;
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\ProxyAdapter;
Expand Down Expand Up @@ -728,6 +729,9 @@ private function assertCachePoolServiceDefinitionIsCreated(ContainerBuilder $con
$this->assertSame(DoctrineAdapter::class, $parentDefinition->getClass());
break;
case 'cache.app':
if (ChainAdapter::class === $parentDefinition->getClass()) {
break;
}
case 'cache.adapter.filesystem':
$this->assertSame(FilesystemAdapter::class, $parentDefinition->getClass());
break;
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"phpdocumentor/reflection-docblock": "<3.0"
},
"suggest": {
"ext-apcu": "For best performance of the system caches",
"symfony/console": "For using the console commands",
"symfony/form": "For using forms",
"symfony/serializer": "For using the serializer service",
Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Psr\Cache\CacheItemInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Symfony\Component\Cache\CacheItem;

/**
Expand Down Expand Up @@ -67,6 +68,24 @@ function ($deferred, $namespace, &$expiredIds) {
);
}

public static function createSystemCache($namespace, $defaultLifetime, $nonce, $directory, LoggerInterface $logger = null)
{
$fs = new FilesystemAdapter($namespace, $defaultLifetime, $directory);
if (null !== $logger) {
$fs->setLogger($logger);
}
if (!ApcuAdapter::isSupported()) {
return $fs;
}

$apcu = new ApcuAdapter($namespace, $defaultLifetime / 5, $nonce);
if (null !== $logger) {
$apcu->setLogger($logger);
}

return new ChainAdapter(array($apcu, $fs));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if creation of the default adapter belongs here. Imo we should deal with this in the FrameworkBundle extension.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

factory moved to FrameworkBundle (I don't like creating/loading yet another utility class just for the factory)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

factory back on the component following comment from @fabpot

}

/**
* Fetches several cache items.
*
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/Cache/Adapter/ApcuAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
*/
class ApcuAdapter extends AbstractAdapter
{
public static function isSupported()
{
return function_exists('apcu_fetch') && ini_get('apc.enabled') && !('cli' === PHP_SAPI && !ini_get('apc.enable_cli'));
}

public function __construct($namespace = '', $defaultLifetime = 0, $nonce = null)
{
if (!function_exists('apcu_fetch') || !ini_get('apc.enabled') || ('cli' === PHP_SAPI && !ini_get('apc.enable_cli'))) {
if (!static::isSupported()) {
throw new CacheException('APCu is not enabled');
}
if ('cli' === PHP_SAPI) {
Expand Down
0