From 47020c4904d4d01d39b5166097efc1ea24d8945a Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 4 Jul 2017 17:41:55 +0300 Subject: [PATCH] [Cache] Handle APCu failures gracefully --- src/Symfony/Component/Cache/Adapter/AbstractAdapter.php | 5 ++++- src/Symfony/Component/Cache/Adapter/ApcuAdapter.php | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php b/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php index b5f6a61b56b03..84bb841d85df2 100644 --- a/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php @@ -15,6 +15,7 @@ use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerAwareTrait; use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; use Symfony\Component\Cache\CacheItem; use Symfony\Component\Cache\Exception\InvalidArgumentException; @@ -108,7 +109,9 @@ public static function createSystemCache($namespace, $defaultLifetime, $version, } $apcu = new ApcuAdapter($namespace, (int) $defaultLifetime / 5, $version); - if (null !== $logger) { + if ('cli' === PHP_SAPI && !ini_get('apc.enable_cli')) { + $apcu->setLogger(new NullLogger()); + } elseif (null !== $logger) { $apcu->setLogger($logger); } diff --git a/src/Symfony/Component/Cache/Adapter/ApcuAdapter.php b/src/Symfony/Component/Cache/Adapter/ApcuAdapter.php index 6687bd4272d1d..ad3aadf1a5257 100644 --- a/src/Symfony/Component/Cache/Adapter/ApcuAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/ApcuAdapter.php @@ -50,7 +50,7 @@ public function __construct($namespace = '', $defaultLifetime = 0, $version = nu protected function doFetch(array $ids) { try { - return apcu_fetch($ids); + return apcu_fetch($ids) ?: array(); } catch (\Error $e) { throw new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine()); } @@ -92,7 +92,11 @@ protected function doDelete(array $ids) protected function doSave(array $values, $lifetime) { try { - return array_keys(apcu_store($values, null, $lifetime)); + if (false === $failures = apcu_store($values, null, $lifetime)) { + $failures = $values; + } + + return array_keys($failures); } catch (\Error $e) { } catch (\Exception $e) { }