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

GH-63: Implement wp_cache_get_multiple() #88

Merged
merged 8 commits into from
Jul 7, 2022
Merged

GH-63: Implement wp_cache_get_multiple() #88

merged 8 commits into from
Jul 7, 2022

Conversation

sjinks
Copy link
Member

@sjinks sjinks commented Jun 4, 2022

This PR adds support for the wp_cache_get_multiple() function introduced in WordPress 5.5.

Fixes #63

@codecov
Copy link

codecov bot commented Jun 4, 2022

Codecov Report

Merging #88 (1d00012) into master (d8084f4) will increase coverage by 1.62%.
The diff coverage is 100.00%.

@@             Coverage Diff              @@
##             master      #88      +/-   ##
============================================
+ Coverage     77.75%   79.38%   +1.62%     
- Complexity      145      158      +13     
============================================
  Files             1        1              
  Lines           454      485      +31     
============================================
+ Hits            353      385      +32     
+ Misses          101      100       -1     
Flag Coverage Δ
unittests 79.38% <100.00%> (+1.62%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
object-cache.php 79.38% <100.00%> (+1.62%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d8084f4...1d00012. Read the comment docs.

$this->group_ops_stats( 'get_multiple', $uncached_keys_list, $group, null, $elapsed );

foreach ( $uncached_keys as $id => $key ) {
$found = array_key_exists( $key, $values );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memcache::get() will return the stored value which may be null. isset() returns false for null values.

$return[ $id ] = $value;
$return_cache[ $key ] = [
'value' => $value,
'found' => $found,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false !== $value will produce an incorrect result if we have stored false for the given key.

$key = $this->key( $id, $group );

if ( isset( $this->cache[ $key ] ) && ( ! $force || $no_mc ) ) {
$value = $this->cache[ $key ]['found'] ? $this->cache[ $key ]['value'] : false;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a nice feature: when set() fails, we may end up with some value and found set to false.

I chose to return false because the value is not in Memcached. Returning something else may break add/increment scenarios.

@rinatkhaziev
Copy link
Contributor

I just tagged some folks to whom this may be relevant.

@WPprodigy
Copy link

Wanted to mention that last time I played around with the "send an array of keys to get()" functionality, it turned out it handled some edge cases differently - particularly null: #69 (comment)

So in wp_cache_get_multiple() we just need to make sure the exact same values are returned for single keys as they would be for wp_cache_get(). So null, empty string/array, false, etc.

@sjinks
Copy link
Member Author

sjinks commented Jun 7, 2022

My tests show that we have issues with null only in PHP 8; PHP 7.4 returns null as null; this happens only when we are trying to get a single key. The multikey version is not affected by the bug.

@sjinks
Copy link
Member Author

sjinks commented Jun 7, 2022

object-cache.php Show resolved Hide resolved
Copy link
Contributor

@rinatkhaziev rinatkhaziev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments of not too much substance. The only part that makes me apprehensive is the $flags handling, but maybe I'm just not fully understanding the implications.

Another thing is is tests we only use small values which is not indicative of what users actually do. Maybe it can be a subject of a separate PR though.

object-cache.php Outdated Show resolved Hide resolved
object-cache.php Show resolved Hide resolved
object-cache.php Outdated Show resolved Hide resolved
object-cache.php Show resolved Hide resolved
Co-authored-by: Rinat K <rinat@automattic.com>
@sonarcloud
Copy link

sonarcloud bot commented Jul 6, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

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

Successfully merging this pull request may close these issues.

Implement wp_cache_get_multiple function.
4 participants