8000 [Cache] Fix invalidating on save failures with ArrayAdapter · symfony/symfony@6868cea · GitHub
[go: up one dir, main page]

Skip to content

Commit 6868cea

Browse files
[Cache] Fix invalidating on save failures with ArrayAdapter
1 parent 196c99e commit 6868cea

File tree

5 files changed

+22
-27
lines changed

5 files changed

+22
-27
lines changed

src/Symfony/Component/Cache/Adapter/ApcuAdapter.php

+4-13
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,10 @@ protected function doSave(array $values, int $lifetime): array|bool
101101
return $failed;
102102
}
103103

104-
try {
105-
if (false === $failures = apcu_store($values, null, $lifetime)) {
106-
$failures = $values;
107-
}
108-
109-
return array_keys($failures);
110-
} catch (\Throwable $e) {
111-
if (1 === \count($values)) {
112-
// Workaround https://github.com/krakjoe/apcu/issues/170
113-
apcu_delete(array_key_first($values));
114-
}
115-
116-
throw $e;
104+
if (false === $failures = apcu_store($values, null, $lifetime)) {
105+
$failures = $values;
117106
}
107+
108+
return array_keys($failures);
118109
}
119110
}

src/Symfony/Component/Cache/Adapter/ArrayAdapter.php

-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@ private function freeze($value, string $key): string|int|float|bool|array|\UnitE
312312
try {
313313
$serialized = serialize($value);
314314
} catch (\Exception $e) {
315-
unset($this->values[$key], $this->expiries[$key], $this->tags[$key]);
316315
$type = get_debug_type($value);
317316
$message = sprintf('Failed to save key "{key}" of type %s: %s', $type, $e->getMessage());
318317
CacheItem::log($this->logger, $message, ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);

src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php

+17
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,23 @@ public function testNumericKeysWorkAfterMemoryLeakPrevention()
352352

353353
$this->assertEquals('value-50', $cache->getItem((string) 50)->get());
354354
}
355+
356+
public function testErrorsDontInvalidate()
357+
{
358+
if (isset($this->skippedTests[__FUNCTION__])) {
359+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
360+
}
361+
362+
$cache = $this->createCachePool(0, __FUNCTION__);
363+
364+
$item = $cache->getItem('foo');
365+
$this->assertTrue($cache->save($item->set('bar')));
366+
$this->assertTrue($cache->hasItem('foo'));
367+
368+
$item->set(static fn () => null);
369+
$this->assertFalse($cache->save($item));
370+
$this->assertSame('bar', $cache->getItem('foo')->get());
371+
}
355372
}
356373

357374
class NotUnserializable

src/Symfony/Component/Cache/Tests/Adapter/ArrayAdapterTest.php

-13
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,4 @@ public function testEnum()
102102

103103
$this->assertSame(TestEnum::Foo, $cache->getItem('foo')->get());
104104
}
105-
106-
public function testExpiryCleanupOnError()
107-
{
108-
$cache = new ArrayAdapter();
109-
110-
$item = $cache->getItem('foo');
111-
$this->assertTrue($cache->save($item->set('bar')));
112-
$this->assertTrue($cache->hasItem('foo'));
113-
114-
$item->set(static fn () => null);
115-
$this->assertFalse($cache->save($item));
116-
$this->assertFalse($cache->hasItem('foo'));
117-
}
118105
}

src/Symfony/Component/Cache/Tests/Adapter/PhpArrayAdapterTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class PhpArrayAdapterTest extends AdapterTestCase
4242
'testSaveDeferredWhenChangingValues' => 'PhpArrayAdapter is read-only.',
4343
'testSaveDeferredOverwrite' => 'PhpArrayAdapter is read-only.',
4444
'testIsHitDeferred' => 'PhpArrayAdapter is read-only.',
45+
'testErrorsDontInvalidate' => 'PhpArrayAdapter is read-only.',
4546

4647
'testExpiresAt' => 'PhpArrayAdapter does not support expiration.',
4748
'testExpiresAtWithNull' => 'PhpArrayAdapter does not support expiration.',

0 commit comments

Comments
 (0)
0