8000 [Cache] Unconditionally use PhpFilesAdapter for system pools · symfony/symfony@cc1edab · GitHub
[go: up one dir, main page]

Skip to content

Commit cc1edab

Browse files
[Cache] Unconditionally use PhpFilesAdapter for system pools
1 parent 1b2bd8f commit cc1edab

File tree

12 files changed

+61
-34
lines changed

12 files changed

+61
-34
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,6 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
15381538
{
15391539
$version = new Parameter('container.build_id');
15401540
$container->getDefinition('cache.adapter.apcu')->replaceArgument(2, $version);
1541-
$container->getDefinition('cache.adapter.system')->replaceArgument(2, $version);
15421541
$container->getDefinition('cache.adapter.filesystem')->replaceArgument(2, $config['directory']);
15431542

15441543
if (isset($config['prefix_seed'])) {

src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@
3535
<tag name="cache.pool" />
3636
</service>
3737

38-
<service id="cache.adapter.system" class="Symfony\Component\Cache\Adapter\AdapterInterface" abstract="true">
39-
<factory class="Symfony\Component\Cache\Adapter\AbstractAdapter" method="createSystemCache" />
38+
<service id="cache.adapter.system" class="Symfony\Component\Cache\Adapter\PhpFilesAdapter" abstract="true">
4039
<tag name="cache.pool" clearer="cache.system_clearer" />
4140
<tag name="monolog.logger" channel="cache" />
4241
<argument /> <!-- namespace -->
4342
<argument>0</argument> <!-- default lifetime -->
44-
<argument /> <!-- version -->
4543
<argument>%kernel.cache_dir%/pools</argument>
46-
<argument type="service" id="logger" on-invalid="ignore" />
44+
<call method="setLogger">
45+
<argument type="service" id="logger" on-invalid="ignore" />
46+
</call>
4747
</service>
4848

4949
<service id="cache.adapter.apcu" class="Symfony\Component\Cache\Adapter\ApcuAdapter" abstract="true">

src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,13 @@ function ($deferred, $namespace, &$expiredIds) use ($getId) {
8787
* @param LoggerInterface|null $logger
8888
*
8989
* @return AdapterInterface
90+
*
91+
* @deprecated since Symfony 4.2.
9092
*/
9193
public static function createSystemCache($namespace, $defaultLifetime, $version, $directory, LoggerInterface $logger = null)
9294
{
95+
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
96+
9397
if (null === self::$apcuSupported) {
9498
self::$apcuSupported = ApcuAdapter::isSupported();
9599
}

src/Symfony/Component/Cache/Adapter/FilesystemAdapter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
namespace Symfony\Component\Cache\Adapter;
1313

1414
use Symfony\Component\Cache\PruneableInterface;
15+
use Symfony\Component\Cache\ResettableInterface;
1516
use Symfony\Component\Cache\Traits\FilesystemTrait;
1617

17-
class FilesystemAdapter extends AbstractAdapter implements PruneableInterface
18+
class FilesystemAdapter extends AbstractAdapter implements PruneableInterface, ResettableInterface
1819
{
1920
use FilesystemTrait;
2021

src/Symfony/Component/Cache/Adapter/PhpFilesAdapter.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313

1414
use Symfony\Component\Cache\Exception\CacheException;
1515
use Symfony\Component\Cache\PruneableInterface;
16+
use Symfony\Component\Cache\ResettableInterface;
1617
use Symfony\Component\Cache\Traits\PhpFilesTrait;
1718

18-
class PhpFilesAdapter extends AbstractAdapter implements PruneableInterface
19+
class PhpFilesAdapter extends AbstractAdapter implements PruneableInterface, ResettableInterface
1920
{
2021
use PhpFilesTrait;
2122

@@ -24,9 +25,7 @@ class PhpFilesAdapter extends AbstractAdapter implements PruneableInterface
2425
*/
2526
public function __construct(string $namespace = '', int $defaultLifetime = 0, string $directory = null)
2627
{
27-
if (!static::isSupported()) {
28-
throw new CacheException('OPcache is not enabled');
29-
}
28+
self::$startTime = self::$startTime ?? $_SERVER['REQUEST_TIME'] ?? time();
3029
parent::__construct('', $defaultLifetime);
3130
$this->init($namespace, $directory);
3231

src/Symfony/Component/Cache/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* added `CacheInterface`, which should become the preferred way to use a cache
88
* throw `LogicException` when `CacheItem::tag()` is called on an item coming from a non tag-aware pool
9+
* deprecated the `AbstractAdapter::createSystemCache()` method
910

1011
3.4.0
1112
-----

src/Symfony/Component/Cache/Simple/FilesystemCache.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
namespace Symfony\Component\Cache\Simple;
1313

1414
use Symfony\Component\Cache\PruneableInterface;
15+
use Symfony\Component\Cache\ResettableInterface;
1516
use Symfony\Component\Cache\Traits\FilesystemTrait;
1617

17-
class FilesystemCache extends AbstractCache implements PruneableInterface
18+
class FilesystemCache extends AbstractCache implements PruneableInterface, ResettableInterface
1819
{
1920
use FilesystemTrait;
2021

src/Symfony/Component/Cache/Simple/PhpFilesCache.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313

1414
use Symfony\Component\Cache\Exception\CacheException;
1515
use Symfony\Component\Cache\PruneableInterface;
16+
use Symfony\Component\Cache\ResettableInterface;
1617
use Symfony\Component\Cache\Traits\PhpFilesTrait;
1718

18-
class PhpFilesCache extends AbstractCache implements PruneableInterface
19+
class PhpFilesCache extends AbstractCache implements PruneableInterface, ResettableInterface
1920
{
2021
use PhpFilesTrait;
2122

@@ -24,9 +25,7 @@ class PhpFilesCache extends AbstractCache implements PruneableInterface
2425
*/
2526
public function __construct(string $namespace = '', int $defaultLifetime = 0, string $directory = null)
2627
{
27-
if (!static::isSupported()) {
28-
throw new CacheException('OPcache is not enabled');
29-
}
28+
self::$startTime = self::$startTime ?? $_SERVER['REQUEST_TIME'] ?? time();
3029
parent::__construct('', $defaultLifetime);
3130
$this->init($namespace, $directory);
3231

src/Symfony/Component/Cache/Tests/Adapter/PhpFilesAdapterTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ class PhpFilesAdapterTest extends AdapterTestCase
2525

2626
public function createCachePool()
2727
{
28-
if (!PhpFilesAdapter::isSupported()) {
29-
$this->markTestSkipped('OPcache extension is not enabled.');
30-
}
31-
3228
return new PhpFilesAdapter('sf-cache');
3329
}
3430

src/Symfony/Component/Cache/Tests/Simple/PhpFilesCacheTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ class PhpFilesCacheTest extends CacheTestCase
2525

2626
public function createSimpleCache()
2727
{
28-
if (!PhpFilesCache::isSupported()) {
29-
$this->markTestSkipped('OPcache extension is not enabled.');
30-
}
31-
3228
return new PhpFilesCache('sf-cache');
3329
}
3430

src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ trait FilesystemCommonTrait
2222
{
2323
private $directory;
2424
private $tmp;
25+
private $hashes;
2526

2627
private function init($namespace, $directory)
2728
{
@@ -56,7 +57,7 @@ protected function doClear($namespace)
5657
$ok = true;
5758

5859
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory, \FilesystemIterator::SKIP_DOTS)) as $file) {
59-
$ok = ($file->isDir() || @unlink($file) || !file_exists($file)) && $ok;
60+
$ok = ($file->isDir() || $this->doUnlink($file) || !file_exists($file)) && $ok;
6061
}
6162

6263
return $ok;
@@ -71,7 +72,7 @@ protected function doDelete(array $ids)
7172

7273
foreach ($ids as $id) {
7374
$file = $this->getFile($id);
74-
$ok = (!file_exists($file) || @unlink($file) || !file_exists($file)) && $ok;
75+
$ok = (!file_exists($file) || $this->doUnlink($file) || !file_exists($file)) && $ok;
7576
}
7677

7778
return $ok;
@@ -98,14 +99,17 @@ private function write($file, $data, $expiresAt = null)
9899

99100
private function getFile($id, $mkdir = false)
100101
{
101-
$hash = str_replace('/', '-', base64_encode(hash('sha256', static::class.$id, true)));
102-
$dir = $this->directory.strtoupper($hash[0].DIRECTORY_SEPARATOR.$hash[1].DIRECTORY_SEPARATOR);
102+
if (!$hash = $this->hashes[$id] ?? null) {
103+
$hash = str_replace('/', '-', base64_encode(hash('md5', static::class.$id, true)));
104+
$hash = strtoupper($hash[0].DIRECTORY_SEPARATOR.$hash[1].DIRECTORY_SEPARATOR).substr($hash, 2, 20);
105+
$this->hashes[$id] = $hash;
106+
}
103107

104-
if ($mkdir && !file_exists($dir)) {
108+
if ($mkdir && !file_exists($dir = $this->directory.dirname($hash))) {
105109
@mkdir($dir, 0777, true);
106110
}
107111

108-
return $dir.substr($hash, 2, 20);
112+
return $this->directory.$hash;
109113
}
110114

111115
/**
@@ -122,7 +126,20 @@ public function __destruct()
122126
parent::__destruct();
123127
}
124128
if (null !== $this->tmp && file_exists($this->tmp)) {
125-
unlink($this->tmp);
129+
$this->doUnlink($this->tmp);
126130
}
127131
}
132+
133+
/**
134+
* {@inheritdoc}
135+
*/
136+
public function reset()
137+
{
138+
$this->hashes = array();
139+
}
140+
141+
protected function doUnlink($file)
142+
{
143+
return @unlink($file);
144+
}
128145
}

src/Symfony/Component/Cache/Traits/PhpFilesTrait.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ trait PhpFilesTrait
2828
private $includeHandler;
2929
private $zendDetectUnicode;
3030

31+
private static $startTime;
32+
3133
public static function isSupported()
3234
{
33-
return function_exists('opcache_invalidate') && ini_get('opcache.enable');
35+
self::$startTime = self::$startTime ?? $_SERVER['REQUEST_TIME'] ?? time();
36+
37+
return \function_exists('opcache_invalidate') && ini_get('opcache.enable') && ('cli' !== \PHP_SAPI || ini_get('opcache.enable_cli'));
3438
}
3539

3640
/**
@@ -40,7 +44,7 @@ public function prune()
4044
{
4145
$time = time();
4246
$pruned = true;
43-
$allowCompile = 'cli' !== PHP_SAPI || ini_get('opcache.enable_cli');
47+
$allowCompile = self::isSupported();
4448

4549
set_error_handler($this->includeHandler);
4650
try {
@@ -119,7 +123,7 @@ protected function doSave(array $values, $lifetime)
119123
{
120124
$ok = true;
121125
$data = array($lifetime ? time() + $lifetime : PHP_INT_MAX, '');
122-
$allowCompile = 'cli' !== PHP_SAPI || ini_get('opcache.enable_cli');
126+
$allowCompile = self::isSupported();
123127

124128
foreach ($values as $key => $value) {
125129
if (null === $value || \is_object($value)) {
@@ -142,7 +146,8 @@ protected function doSave(array $values, $lifetime)
142146

143147
$data[1] = $value;
144148
$file = $this->getFile($key, true);
145-
$ok = $this->write($file, '<?php return '.var_export($data, true).';') && $ok;
149+
// Since OPcache only compiles files older than the script execution start, set the file's mtime in the past
150+
$ok = $this->write($file, '<?php return '.var_export($data, true).';', self::$startTime - 10) && $ok;
146151

147152
if ($allowCompile) {
148153
@opcache_invalidate($file, true);
@@ -155,4 +160,13 @@ protected function doSave(array $values, $lifetime)
155160

156161
return $ok;
157162
}
163+
164+
protected function doUnlink($file)
165+
{
166+
if (self::isSupported()) {
167+
@opcache_invalidate($file, true);
168+
}
169+
170+
return @unlink($file);
171+
}
158172
}

0 commit comments

Comments
 (0)
0