Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d1abcf7

Browse filesBrowse files
[Cache] Fix invalidating on save failures with Array|ApcuAdapter
1 parent eba02ab commit d1abcf7
Copy full SHA for d1abcf7

File tree

Expand file treeCollapse file tree

5 files changed

+25
-27
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+25
-27
lines changed

‎Adapter/ApcuAdapter.php

Copy file name to clipboardExpand all lines: Adapter/ApcuAdapter.php
+4-13Lines changed: 4 additions & 13 deletions
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
}

‎Adapter/ArrayAdapter.php

Copy file name to clipboardExpand all lines: Adapter/ArrayAdapter.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,9 @@ 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]);
315+
if (!isset($this->expiries[$key])) {
316+
unset($this->values[$key]);
317+
}
316318
$type = get_debug_type($value);
317319
$message = sprintf('Failed to save key "{key}" of type %s: %s', $type, $e->getMessage());
318320
CacheItem::log($this->logger, $message, ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);

‎Tests/Adapter/AdapterTestCase.php

Copy file name to clipboardExpand all lines: Tests/Adapter/AdapterTestCase.php
+17Lines changed: 17 additions & 0 deletions
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

‎Tests/Adapter/ArrayAdapterTest.php

Copy file name to clipboardExpand all lines: Tests/Adapter/ArrayAdapterTest.php
-13Lines changed: 0 additions & 13 deletions
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
}

‎Tests/Adapter/PhpArrayAdapterTest.php

Copy file name to clipboardExpand all lines: Tests/Adapter/PhpArrayAdapterTest.php
+1Lines changed: 1 addition & 0 deletions
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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.