[FrameworkBundle] Default to Apcu+Filesystem cache chain

This commit is contained in:
Nicolas Grekas 2016-05-05 17:37:21 +02:00
parent 209fb450da
commit b9b57f93cb
10 changed files with 49 additions and 43 deletions

View File

@ -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
----------

View File

@ -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
----------

View File

@ -564,7 +564,7 @@ class Configuration implements ConfigurationInterface
->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()

View File

@ -1039,6 +1039,7 @@ class FrameworkExtension extends Extension
{
$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) {

View File

@ -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>
@ -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" />

View File

@ -269,7 +269,7 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
'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',
),

View File

@ -14,6 +14,7 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection;
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;
@ -728,6 +729,9 @@ abstract class FrameworkExtensionTest extends TestCase
$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;

View File

@ -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",

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Cache\Adapter;
use Psr\Cache\CacheItemInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Symfony\Component\Cache\CacheItem;
/**
@ -67,6 +68,24 @@ abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface
);
}
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));
}
/**
* Fetches several cache items.
*

View File

@ -19,9 +19,14 @@ use Symfony\Component\Cache\Exception\CacheException;
*/
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) {