Skip to content

Commit 196c99e

Browse files
bug #60122 [Cache] ArrayAdapter serialization exception clean $expiries (bastien-wink)
This PR was submitted for the 7.3 branch but it was merged into the 6.4 branch instead. Discussion ---------- [Cache] ArrayAdapter serialization exception clean $expiries | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #60121 | License | MIT In [ArrayAdapter.php:339](https://github.com/symfony/symfony/blame/7.3/src/Symfony/Component/Cache/Adapter/ArrayAdapter.php#L339), if $value contains a Closure, an Exception is triggered and $this->values get cleaned. ``` try { $serialized = serialize($value); } catch (\Exception $e) { unset($this->values[$key], $this->expiries[$key], $this->tags[$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)]); return null; } ``` $this->expiries is used in [ArrayAdapter.php:300](https://github.com/symfony/symfony/blame/7.3/src/Symfony/Component/Cache/Adapter/ArrayAdapter.php#L300) to determine cache hit. If hash is found in $this->expiries but missing in $this->keys it generate a NOTICE. On serialize($value); catch Exception, clean expiries with unset. Commits ------- 91331e1 Add tests a4bfce3 bug #60121[Cache] ArrayAdapter serialization exception clean $expiries
2 parents 31135c7 + 91331e1 commit 196c99e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ 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->tags[$key]);
315+
unset($this->values[$key], $this->expiries[$key], $this->tags[$key]);
316316
$type = get_debug_type($value);
317317
$message = sprintf('Failed to save key "{key}" of type %s: %s', $type, $e->getMessage());
318318
CacheItem::log($this->logger, $message, ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);

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

+13
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,17 @@ 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+
}
105118
}

0 commit comments

Comments
 (0)