Skip to content

Commit

Permalink
[Cache] fix memory leak when using PhpArrayAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Dec 5, 2019
1 parent 59126e0 commit 6c43f6a
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 13 deletions.
13 changes: 4 additions & 9 deletions src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,17 @@ static function ($key, $value, $isHit) {
* fallback pool with this adapter only if the current PHP version is supported.
*
* @param string $file The PHP file were values are cached
* @param CacheItemPoolInterface $fallbackPool Fallback for old PHP versions or opcache disabled
* @param CacheItemPoolInterface $fallbackPool A pool to fallback on when an item is not hit
*
* @return CacheItemPoolInterface
*/
public static function create($file, CacheItemPoolInterface $fallbackPool)
{
// Shared memory is available in PHP 7.0+ with OPCache enabled and in HHVM
if ((\PHP_VERSION_ID >= 70000 && filter_var(ini_get('opcache.enable'), FILTER_VALIDATE_BOOLEAN)) || \defined('HHVM_VERSION')) {
if (!$fallbackPool instanceof AdapterInterface) {
$fallbackPool = new ProxyAdapter($fallbackPool);
}

return new static($file, $fallbackPool);
if (!$fallbackPool instanceof AdapterInterface) {
$fallbackPool = new ProxyAdapter($fallbackPool);
}

return $fallbackPool;
return new static($file, $fallbackPool);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Cache/Simple/PhpArrayCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public function __construct($file, CacheInterface $fallbackPool)
* stores arrays in its latest versions. This factory method decorates the given
* fallback pool with this adapter only if the current PHP version is supported.
*
* @param string $file The PHP file were values are cached
* @param string $file The PHP file were values are cached
* @param Psr16CacheInterface $fallbackPool A pool to fallback on when an item is not hit
*
* @return CacheInterface
*/
public static function create($file, CacheInterface $fallbackPool)
{
// Shared memory is available in PHP 7.0+ with OPCache enabled and in HHVM
if ((\PHP_VERSION_ID >= 70000 && filter_var(ini_get('opcache.enable'), FILTER_VALIDATE_BOOLEAN)) || \defined('HHVM_VERSION')) {
if (\PHP_VERSION_ID >= 70000) {
return new static($file, $fallbackPool);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public static function setUpBeforeClass()

protected function tearDown()
{
$this->createCachePool()->clear();

if (file_exists(sys_get_temp_dir().'/symfony-cache')) {
FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public static function setUpBeforeClass()

protected function tearDown()
{
$this->createCachePool()->clear();

if (file_exists(sys_get_temp_dir().'/symfony-cache')) {
FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public static function setUpBeforeClass()

protected function tearDown()
{
$this->createSimpleCache()->clear();

if (file_exists(sys_get_temp_dir().'/symfony-cache')) {
FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public static function setUpBeforeClass()

protected function tearDown()
{
$this->createSimpleCache()->clear();

if (file_exists(sys_get_temp_dir().'/symfony-cache')) {
FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache');
}
Expand Down
12 changes: 11 additions & 1 deletion src/Symfony/Component/Cache/Traits/PhpArrayTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ trait PhpArrayTrait
private $values;
private $zendDetectUnicode;

private static $valuesCache = [];

/**
* Store an array of cached values.
*
Expand Down Expand Up @@ -107,6 +109,7 @@ public function warmUp(array $values)
unset($serialized, $unserialized, $value, $dump);

@rename($tmpFile, $this->file);
unset(self::$valuesCache[$this->file]);

$this->initialize();
}
Expand All @@ -119,6 +122,7 @@ public function clear()
$this->values = [];

$cleared = @unlink($this->file) || !file_exists($this->file);
unset(self::$valuesCache[$this->file]);

return $this->pool->clear() && $cleared;
}
Expand All @@ -128,11 +132,17 @@ public function clear()
*/
private function initialize()
{
if (isset(self::$valuesCache[$this->file])) {
$this->values = self::$valuesCache[$this->file];

return;
}

if ($this->zendDetectUnicode) {
$zmb = ini_set('zend.detect_unicode', 0);
}
try {
$this->values = file_exists($this->file) ? (include $this->file ?: []) : [];
$this->values = self::$valuesCache[$this->file] = file_exists($this->file) ? (include $this->file ?: []) : [];
} finally {
if ($this->zendDetectUnicode) {
ini_set('zend.detect_unicode', $zmb);
Expand Down

0 comments on commit 6c43f6a

Please sign in to comment.