[Cache] Handle APCu failures gracefully

This commit is contained in:
Nicolas Grekas 2017-07-04 17:41:55 +03:00
parent ddc9d2e313
commit 47020c4904
2 changed files with 10 additions and 3 deletions

View File

@ -15,6 +15,7 @@ use Psr\Cache\CacheItemInterface;
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 @@ abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface
}
$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);
}

View File

@ -50,7 +50,7 @@ class ApcuAdapter extends AbstractAdapter
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 @@ class ApcuAdapter extends AbstractAdapter
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) {
}