Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGS-7148] Handle duplicate keys in get_multiple function #448

Conversation

Souptik2001
Copy link
Contributor

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. 🙂

Signed-off-by: Souptik Datta <souptikdatta2001@gmail.com>
@Souptik2001 Souptik2001 requested review from a team as code owners November 13, 2023 19:13
@Souptik2001
Copy link
Contributor Author

Hi @jazzsequence any thoughts or suggestion on this one?

@pwtyler pwtyler changed the title Handle duplicate keys in get_multiple function [BUGS-7148] Handle duplicate keys in get_multiple function Nov 15, 2023
@pwtyler
Copy link
Member

pwtyler commented Nov 15, 2023

Thanks for the PR! Love to see a PR with tests 🎊. Tracking this internally as BUGS-7148. We'll get this queued for review with our team.

@Souptik2001
Copy link
Contributor Author

Thank you! 😃 🙇

@jazzsequence
Copy link
Contributor

Merging this. We'll get this in the next release, likely sometime next week. Thanks @Souptik2001

@jazzsequence jazzsequence merged commit 35b3c76 into pantheon-systems:main Nov 21, 2023
10 checks passed
@Souptik2001
Copy link
Contributor Author

Thanks @jazzsequence!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants