8000 [Cache] Fix invalidating on save failures with Array|ApcuAdapter by nicolas-grekas · Pull Request #60172 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Fix invalidating on save failures with Array|ApcuAdapter #60172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Cache] Fix invalidating on save failures with Array|ApcuAdapter
  • Loading branch information
nicolas-grekas committed Apr 8, 2025
commit 30640b25157aca33b85a70cdf89a77a727b157fc
17 changes: 4 additions & 13 deletions src/Symfony/Component/Cache/Adapter/ApcuAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,10 @@ protected function doSave(array $values, int $lifetime): array|bool
return $failed;
}

try {
if (false === $failures = apcu_store($values, null, $lifetime)) {
$failures = $values;
}

return array_keys($failures);
} catch (\Throwable $e) {
if (1 === \count($values)) {
// Workaround https://github.com/krakjoe/apcu/issues/170
apcu_delete(array_key_first($values));
}

throw $e;
if (false === $failures = apcu_store($values, null, $lifetime)) {
$failures = $values;
}

return array_keys($failures);
}
}
4 changes: 3 additions & 1 deletion src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ private function freeze($value, string $key): string|int|float|bool|array|\UnitE
try {
$serialized = serialize($value);
} catch (\Exception $e) {
unset($this->values[$key], $this->expiries[$key], $this->tags[$key]);
if (!isset($this->expiries[$key])) {
unset($this->values[$key]);
}
$type = get_debug_type($value);
$message = sprintf('Failed to save key "{key}" of type %s: %s', $type, $e->getMessage());
CacheItem::log($this->logger, $message, ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,23 @@ public function testNumericKeysWorkAfterMemoryLeakPrevention()

$this->assertEquals('value-50', $cache->getItem((string) 50)->get());
}

public function testErrorsDontInvalidate()
{
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}

$cache = $this->createCachePool(0, __FUNCTION__);

$item = $cache->getItem('foo');
$this->assertTrue($cache->save($item->set('bar')));
$this->assertTrue($cache->hasItem('foo'));

$item->set(static fn () => null);
$this->assertFalse($cache->save($item));
$this->assertSame('bar', $cache->getItem('foo')->get());
}
}

class NotUnserializable
Expand Down
13 changes: 0 additions & 13 deletions src/Symfony/Component/Cache/Tests/Adapter/ArrayAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,4 @@ public function testEnum()

$this->assertSame(TestEnum::Foo, $cache->getItem('foo')->get());
}

public function testExpiryCleanupOnError()
{
$cache = new ArrayAdapter();

$item = $cache->getItem('foo');
$this->assertTrue($cache->save($item->set('bar')));
$this->assertTrue($cache->hasItem('foo'));

$item->set(static fn () => null);
$this->assertFalse($cache->save($item));
$this->assertFalse($cache->hasItem('foo'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class PhpArrayAdapterTest extends AdapterTestCase
'testSaveDeferredWhenChangingValues' => 'PhpArrayAdapter is read-only.',
'testSaveDeferredOverwrite' => 'PhpArrayAdapter is read-only.',
'testIsHitDeferred' => 'PhpArrayAdapter is read-only.',
'testErrorsDontInvalidate' => 'PhpArrayAdapter is read-only.',

'testExpiresAt' => 'PhpArrayAdapter does not support expiration.',
'testExpiresAtWithNull' => 'PhpArrayAdapter does not support expiration.',
Expand Down
Loading
0