diff --git a/src/Symfony/Component/Cache/Adapter/ApcuAdapter.php b/src/Symfony/Component/Cache/Adapter/ApcuAdapter.php index 621899d904efc..96f76ac05ed4c 100644 --- a/src/Symfony/Component/Cache/Adapter/ApcuAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/ApcuAdapter.php @@ -72,6 +72,17 @@ protected function doDelete(array $ids) */ protected function doSave(array $values, $lifetime) { - return array_keys(apcu_store($values, null, $lifetime)); + try { + return array_keys(apcu_store($values, null, $lifetime)); + } catch (\Error $e) { + } catch (\Exception $e) { + } + + if (1 === count($values)) { + // Workaround https://github.com/krakjoe/apcu/issues/170 + apcu_delete(key($values)); + } + + throw $e; } } diff --git a/src/Symfony/Component/Cache/Tests/Adapter/ApcuAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/ApcuAdapterTest.php index b6ce21e1efe06..8b6493be1b12f 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/ApcuAdapterTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/ApcuAdapterTest.php @@ -30,4 +30,17 @@ public function createCachePool() return new ApcuAdapter(str_replace('\\', '.', __CLASS__)); } + + public function testUnserializable() + { + $pool = $this->createCachePool(); + + $item = $pool->getItem('foo'); + $item->set(function() {}); + + $this->assertFalse($pool->save($item)); + + $item = $pool->getItem('foo'); + $this->assertFalse($item->isHit()); + } }