Skip to content

Commit 84753fc

Browse files
bmckay959timacdonaldtaylorotwell
authored
Bugfix for Cache::memo()->many() returning the wrong value with an integer key type (#55503)
* Update MemoizedStore.php to maintain key types Bugfix for #55500 * Styling Fix * Style Fix * Add tests * Remove redundant `all` call * Make fall back strict * Improve tests * Style fix * Update tests/Integration/Cache/MemoizedStoreTest.php * Update MemoizedStore.php --------- Co-authored-by: Tim MacDonald <hello@timacdonald.me> Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent b6a2af2 commit 84753fc

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/Illuminate/Cache/MemoizedStore.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,17 @@ public function many(array $keys)
7575
});
7676
}
7777

78-
$result = [
79-
...$memoized,
80-
...$retrieved,
81-
];
78+
$result = [];
8279

83-
return array_replace(array_flip($keys), $result);
80+
foreach ($keys as $key) {
81+
if (array_key_exists($key, $memoized)) {
82+
$result[$key] = $memoized[$key];
83+
} else {
84+
$result[$key] = $retrieved[$key];
85+
}
86+
}
87+
88+
return $result;
8489
}
8590

8691
/**

tests/Integration/Cache/MemoizedStoreTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,33 @@ public function test_it_can_memoize_when_retrieving_mulitple_values()
8989
$this->assertSame(['name.0' => 'Tim', 'name.1' => 'Taylor'], $memoized);
9090
}
9191

92+
public function test_it_uses_correct_keys_for_getMultiple()
93+
{
94+
$data = [
95+
'a' => 'string-value',
96+
'1.1' => 'float-value',
97+
'1' => 'integer-value-as-string',
98+
2 => 'integer-value',
99+
];
100+
Cache::putMany($data);
101+
102+
$memoValue = Cache::memo()->many(['a', '1.1', '1', 2]);
103+
$cacheValue = Cache::many(['a', '1.1', '1', 2]);
104+
105+
$this->assertSame([
106+
'a' => 'string-value',
107+
'1.1' => 'float-value',
108+
'1' => 'integer-value-as-string',
109+
2 => 'integer-value',
110+
], $cacheValue);
111+
$this->assertSame($cacheValue, $memoValue);
112+
113+
// ensure correct on the second memoized retrieval
114+
$memoValue = Cache::memo()->many(['a', '1.1', '1', 2]);
115+
116+
$this->assertSame($cacheValue, $memoValue);
117+
}
118+
92119
public function test_null_values_are_memoized_when_retrieving_mulitple_values()
93120
{
94121
$live = Cache::getMultiple(['name.0', 'name.1']);

0 commit comments

Comments
 (0)