8000 Reserve keys when using numeric ones · symfony/cache@678f317 · GitHub
[go: up one dir, main page]

Skip to content

Commit 678f317

Browse files
committed
Reserve keys when using numeric ones
1 parent f59de59 commit 678f317
10000

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

Tests/Adapter/AdapterTestCase.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,19 @@ public function testNullByteInKey()
331331

332332
$this->assertSame(123, $cache->getItem("a\0b")->get());
333333
}
334+
335+
public function testNumericKeysWorkAfterMemoryLeakPrevention()
336+
{
337+
$cache = $this->createCachePool(0, __FUNCTION__);
338+
339+
for ($i = 0; $i < 1001; ++$i) {
340+
$cacheItem = $cache->getItem((string) $i);
341+
$cacheItem->set('value-'.$i);
342+
$cache->save($cacheItem);
343+
}
344+
345+
$this->assertEquals('value-50', $cache->getItem((string) 50)->get());
346+
}
334347
}
335348

336349
class NotUnserializable

Traits/AbstractTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ private function getId($key): string
290290
$this->ids[$key] = $key;
291291

292292
if (\count($this->ids) > 1000) {
293-
array_splice($this->ids, 0, 500); // stop memory leak if there are many keys
293+
$this->ids = \array_slice($this->ids, 500, null, true); // stop memory leak if there are many keys
294294
}
295295

296296
if (null === $this->maxIdLength) {

0 commit comments

Comments
 (0)
0