rhubarbgroup / redis-cache

A persistent object cache backend for WordPress powered by Redis. Supports Predis, PhpRedis, Relay, replication, sentinels, clustering and WP-CLI.
https://wordpress.org/plugins/redis-cache/
GNU General Public License v3.0
434 stars 151 forks source link

wp_cache_get_multiple() after upgrading to WP 5.5. #243

Closed lesterchan closed 4 years ago

lesterchan commented 4 years ago

I have been getting cache misses for wp_cache_get_multiple() after upgrading to WP 5.5.

It seems to be returning false all the time as after priming the cache, it doesn't read back from the cache.

Setting the cache

[12-Aug-2020 14:19:51 UTC] START wp_cache_add
[12-Aug-2020 14:19:51 UTC] {"gamerz_wp:category_relationships:4333":[1461]}
[12-Aug-2020 14:19:51 UTC] END wp_cache_add

Using get_multiple to read (notice that it returns false for gamerz_wp:category_relationships:4333)

[12-Aug-2020 14:20:23 UTC] START get_multiple
[12-Aug-2020 14:20:23 UTC] {"derived_keys":{"4285":"gamerz_wp:category_relationships:4285","4293":"gamerz_wp:category_relationships:4293","4324":"gamerz_wp:category_relationships:4324","4325":"gamerz_wp:category_relationships:4325","4333":"gamerz_wp:category_relationships:4333","4337":"gamerz_wp:category_relationships:4337","4340":"gamerz_wp:category_relationships:4340","4344":"gamerz_wp:category_relationships:4344","4348":"gamerz_wp:category_relationships:4348","4349":"gamerz_wp:category_relationships:4349"}}
[12-Aug-2020 14:20:23 UTC] {"cache":{"4285":false,"4293":false,"4324":false,"4325":false,"4333":false,"4337":false,"4340":false,"4344":false,"4348":false,"4349":false}}
[12-Aug-2020 14:20:23 UTC] END get_multiple

Might be due to the remaining keys

Line 1371 of object-cache.php

        $remaining_keys = array_filter( $keys, function ( $key ) use ( $cache ) {
            return ! isset( $cache[ $key ] );
        } );

This change works for me. $cache[ $key ] will always be set and be false if the cache fails.

        $remaining_keys = array_filter( $keys, function ( $key ) use ( $cache ) {
            return isset( $cache[ $key ] ) && $cache[ $key ] === false;
        } );
tillkruss commented 4 years ago

@lesterchan I'm not sure I follow. Can you write some dummy code for me to reproduce this?

lesterchan commented 4 years ago

After upgrading to WP 5.5 most of the prime caches uses wp_cache_get_multiple(). Because of the upgrade, it seems that the caches are added but they are not being read. MySQL queries on my home page jump from 2 queries to 40 queries every time.

I am trying to find an easy way to reproduce it, but it is too nested and deep.

So basically, whenever wp_cache_get_multiple() is being called, the value returned by each key is always false. You can do a var_dump on the output of wp_cache_get_multiple()

tillkruss commented 4 years ago

Alright, can you open a PR? Or at least some dummy code for me to produce this.

lesterchan commented 4 years ago

Are you able to reproduce the problem? I don't have enough context for a PR. It seems my "fix" is not working as intended (there are some side effects where it will write empty values to my postmeta).

I also tried WP-Redis as a comparison and it seems to work fine.

tillkruss commented 4 years ago

@lesterchan: No, I at least need some dummy code to look into it. You logs aren't enough.

tillkruss commented 4 years ago

@naxvog: Have you had any issues with 5.5 and get_multiple()?

lesterchan commented 4 years ago

Let's see if you can reproduce this. 4347 is any post ID.

_prime_post_caches( 4347 );
var_dump(wp_cache_get_multiple([4347], 'posts'));

redis-cache returns

array(1) {
  [4347]=>
  bool(false)
}

wp-redis returns


array(1) {
--
  | [4347]=>
  | object(stdClass)#1158 (23) {
.....
tillkruss commented 4 years ago

Fixed in 4b1e2aee7c6360bc612b97a847fff3c5501f9163.

lesterchan commented 4 years ago

Thanks @tillkruss! Verified working!