From f49df4ab0559a11c45a952a7fe976438d15a3f3d Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 14 Mar 2019 22:54:25 +0100 Subject: [PATCH] [Cache] fix LockRegistry --- src/Symfony/Component/Cache/LockRegistry.php | 19 +++++++++++++++---- .../Component/Cache/Traits/ContractsTrait.php | 18 +++++++++++------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Cache/LockRegistry.php b/src/Symfony/Component/Cache/LockRegistry.php index e0318e900c4d5..ef49805ea0928 100644 --- a/src/Symfony/Component/Cache/LockRegistry.php +++ b/src/Symfony/Component/Cache/LockRegistry.php @@ -23,7 +23,7 @@ * * @author Nicolas Grekas */ -class LockRegistry +final class LockRegistry { private static $openedFiles = []; private static $lockedFiles = []; @@ -74,7 +74,7 @@ public static function setFiles(array $files): array return $previousFiles; } - public static function compute(callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool) + public static function compute(callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool, \Closure $setMetadata = null) { $key = self::$files ? crc32($item->getKey()) % \count(self::$files) : -1; @@ -88,7 +88,18 @@ public static function compute(callable $callback, ItemInterface $item, bool &$s if (flock($lock, LOCK_EX | LOCK_NB)) { self::$lockedFiles[$key] = true; - return $callback($item, $save); + $value = $callback($item, $save); + + if ($save) { + if ($setMetadata) { + $setMetadata($item); + } + + $pool->save($item->set($value)); + $save = false; + } + + return $value; } // if we failed the race, retry locking in blocking mode to wait for the winner flock($lock, LOCK_SH); @@ -125,6 +136,6 @@ private static function open(int $key) restore_error_handler(); } - self::$openedFiles[$key] = $h ?: @fopen(self::$files[$key], 'r'); + return self::$openedFiles[$key] = $h ?: @fopen(self::$files[$key], 'r'); } } diff --git a/src/Symfony/Component/Cache/Traits/ContractsTrait.php b/src/Symfony/Component/Cache/Traits/ContractsTrait.php index 8b1eb5a24427f..bd7be08dd5e8e 100644 --- a/src/Symfony/Component/Cache/Traits/ContractsTrait.php +++ b/src/Symfony/Component/Cache/Traits/ContractsTrait.php @@ -40,7 +40,7 @@ trait ContractsTrait public function setCallbackWrapper(?callable $callbackWrapper): callable { $previousWrapper = $this->callbackWrapper; - $this->callbackWrapper = $callbackWrapper ?? function (callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool) { + $this->callbackWrapper = $callbackWrapper ?? function (callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool, \Closure $setMetadata) { return $callback($item, $save); }; @@ -56,17 +56,19 @@ private function doGet(AdapterInterface $pool, string $key, callable $callback, static $setMetadata; $setMetadata = $setMetadata ?? \Closure::bind( - function (AdapterInterface $pool, ItemInterface $item, float $startTime) { + function (CacheItem $item, float $startTime, ?array &$metadata) { if ($item->expiry > $endTime = microtime(true)) { - $item->newMetadata[ItemInterface::METADATA_EXPIRY] = $item->expiry; - $item->newMetadata[ItemInterface::METADATA_CTIME] = 1000 * (int) ($endTime - $startTime); + $item->newMetadata[CacheItem::METADATA_EXPIRY] = $metadata[CacheItem::METADATA_EXPIRY] = $item->expiry; + $item->newMetadata[CacheItem::METADATA_CTIME] = $metadata[CacheItem::METADATA_CTIME] = 1000 * (int) ($endTime - $startTime); + } else { + unset($metadata[CacheItem::METADATA_EXPIRY], $metadata[CacheItem::METADATA_CTIME]); } }, null, CacheItem::class ); - return $this->contractsGet($pool, $key, function (CacheItem $item, bool &$save) use ($pool, $callback, $setMetadata) { + return $this->contractsGet($pool, $key, function (CacheItem $item, bool &$save) use ($pool, $callback, $setMetadata, &$metadata) { // don't wrap nor save recursive calls if (null === $callbackWrapper = $this->callbackWrapper) { $value = $callback($item, $save); @@ -78,8 +80,10 @@ function (AdapterInterface $pool, ItemInterface $item, float $startTime) { $startTime = microtime(true); try { - $value = $callbackWrapper($callback, $item, $save, $pool); - $setMetadata($pool, $item, $startTime); + $value = $callbackWrapper($callback, $item, $save, $pool, function (CacheItem $item) use ($setMetadata, $startTime, &$metadata) { + $setMetadata($item, $startTime, $metadata); + }); + $setMetadata($item, $startTime, $metadata); return $value; } finally {