diff --git a/src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php b/src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php index 01f17fdb8a72..60449e1d24bc 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php @@ -331,6 +331,19 @@ public function testNullByteInKey() $this->assertSame(123, $cache->getItem("a\0b")->get()); } + + public function testNumericKeysWorkAfterMemoryLeakPrevention() + { + $cache = $this->createCachePool(0, __FUNCTION__); + + for ($i = 0; $i < 1001; ++$i) { + $cacheItem = $cache->getItem((string) $i); + $cacheItem->set('value-'.$i); + $cache->save($cacheItem); + } + + $this->assertEquals('value-50', $cache->getItem((string) 50)->get()); + } } class NotUnserializable diff --git a/src/Symfony/Component/Cache/Traits/AbstractTrait.php b/src/Symfony/Component/Cache/Traits/AbstractTrait.php index f4902917d38b..b047880661ff 100644 --- a/src/Symfony/Component/Cache/Traits/AbstractTrait.php +++ b/src/Symfony/Component/Cache/Traits/AbstractTrait.php @@ -290,7 +290,7 @@ private function getId($key): string $this->ids[$key] = $key; if (\count($this->ids) > 1000) { - array_splice($this->ids, 0, 500); // stop memory leak if there are many keys + $this->ids = \array_slice($this->ids, 500, null, true); // stop memory leak if there are many keys } if (null === $this->maxIdLength) {