[BUGS-7148] Handle duplicate keys in get_multiple
function
#448
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi 👋 ,
Seems like the
WP_Object_Cache:get_multiple
function breaks when we pass duplicate keys in the$keys
array argument.When any duplicate key is passed in the array all the values of the keys coming after that duplicate gets messed up. Basically they gets shifted by one after a duplicate. Again after another duplicate is there the values after that second duplicate gets shifted by two, and similarly.
This basically happens due to the way we get the cache values from the two arrays -
$remaining_keys
and$results
. In the results array we don't get the value twice, we just get once. But we iterate through all the$remaining_keys
and just access the corresponding index of the$results
array. That's why this is happening.So, the easiest solution for this would be to just fetch the unique values in the beginning of the function only. This will not only fix the bug, but also reduce the minor redundant extra computations of the duplicate keys.
I have also added a test case, which will break if you run it by removing the fix from the
object-cache.php
.I think this edge case should be handled here, because this function doesn't deny you to pass duplicate keys, but also doesn't handle duplicate keys.
Thanks! Please let me know if any other info is needed. 🙂