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 dbbf46c

Browse filesBrowse files
bug #47884 [Cache] Reserve numeric keys when doing memory leak prevention (simoheinonen)
This PR was merged into the 4.4 branch. Discussion ---------- [Cache] Reserve numeric keys when doing memory leak prevention | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | <!-- Replace this notice by a short README for your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> `getItem()` started to return invalid entries after 1000 calls because `array_slice()` doesn't preserver numeric keys ```php <?php require_once 'vendor/autoload.php'; use Symfony\Component\Cache\Adapter\FilesystemAdapter; // works: $cache = new FilesystemAdapter('foo-namespace'); $keys = range(0, 999); foreach ($keys as $key) { $cacheItem = $cache->getItem((string) $key); $cacheItem->set('value: ' . $key); $cache->save($cacheItem); } echo $cache->getItem('25')->get() . PHP_EOL; // "value: 25" // doesn't work: $cache = new FilesystemAdapter('xyz-namespace'); $keys = range(0, 1000); foreach ($keys as $key) { $cacheItem = $cache->getItem((string) $key); $cacheItem->set('value: ' . $key); $cache->save($cacheItem); } echo $cache->getItem('25')->get() . PHP_EOL; // "value: 525" ``` after: ```php // works: $cache = new FilesystemAdapter('xyz-namespace'); $keys = range(0, 1000); foreach ($keys as $key) { $cacheItem = $cache->getItem((string) $key); $cacheItem->set('value: ' . $key); $cache->save($cacheItem); } echo $cache->getItem('25')->get() . PHP_EOL; // "value: 25" ``` Commits ------- 5c61350 Reserve keys when using numeric ones
2 parents 44abca3 + 5c61350 commit dbbf46c
Copy full SHA for dbbf46c

File tree

Expand file treeCollapse file tree

2 files changed

+14
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+14
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php
+13Lines 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

‎src/Symfony/Component/Cache/Traits/AbstractTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Traits/AbstractTrait.php
+1-1Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.