Ticket #21434 (closed enhancement: fixed)

Opened 7 months ago

Last modified 4 months ago

Don't obliterate the object cache in switch_to_blog()

Reported by: ryan Owned by: ryan
Priority: normal Milestone: 3.5
Component: Cache Version: 3.4.1
Severity: normal Keywords:
Cc: scribu, danielc@…

Description

switch_to_blog() calls wp_cache_init() which destroys the current $wp_object_cache object and creates a new one. This can be really expensive, especially with the non-persistent default cache. Instead of reinitializing, let's introduce something like wp_cache_switch_to_blog(). The memcached backend already builds the blog id into keys not in global groups, making ::switch_to_blog() very easy to implement. The default cache can do the same (I think it was keyed by blog id once upon a time).

Attachments

21434.diff (6.2 KB) - added by ryan 7 months ago.
First draft, untested
21434.2.diff (7.9 KB) - added by ryan 7 months ago.
21434-ut.diff (2.0 KB) - added by ryan 7 months ago.
21434-ut.2.diff (2.6 KB) - added by ryan 7 months ago.
21434.3.diff (8.7 KB) - added by ryan 7 months ago.
Performance twaeks, wp_start_object_cache() changes
21434.4.diff (9.2 KB) - added by ryan 7 months ago.
Deprecate reset
21434.5.diff (9.6 KB) - added by ryan 6 months ago.
Turn global_groups into an associate array for faster isset checks
21434.6.diff (602 bytes) - added by SergeyBiryukov 5 months ago.
21434.7.diff (1.0 KB) - added by ryan 4 months ago.
Deprecation phpdoc for wp_cache_reset().
21434.8.diff (1020 bytes) - added by ryan 4 months ago.
Remove wp_cache_switch_to_blog() from _deprecated_function(). Note using wp_cache_init() in unit tests in phpdoc.

Change History

  • Cc scribu added

ryan7 months ago

First draft, untested

ryan7 months ago

ryan7 months ago

ryan7 months ago

ryan7 months ago

Performance twaeks, wp_start_object_cache() changes

ryan7 months ago

Deprecate reset

ryan6 months ago

Turn global_groups into an associate array for faster isset checks

  • Owner set to ryan
  • Status changed from new to closed
  • Resolution set to fixed

In [21403]:

Introduce wp_cache_switch_to_blog() and WP_Object_Cache::switch_to_blog() as a lighter/faster way to switch the cache to a new blog id.

Add the blog id to the cache keys for multisite installs.

Use wp_cache_switch_to_blog() instead of wp_cache_init() in switch_to_blog().

Use wp_cache_switch_to_blog() instead of wp_cache_reset() in wp_start_object_cache().

Deprecate wp_cache_reset().

This avoids the many queries needed to re-prime the cache after switch_to_blog() on multisite installs using the default cache backend.

fixes #21434

comment:4 follow-up: ↓ 7   convissor5 months ago

The new, efficient code for switching between blogs is an excellent improvement. Thanks.

Can you please elaborate on the reasoning behind deprecating wp_cache_reset() / WP_Object_Cache::reset() and why the deprecation message instructs people to use wp_cache_switch_to_blog() / switch_to_blog()? The reset() and switch_to_blog() methods serve two separate purposes. (Though some people may have used them in conjunction.)

May I suggest the deprecation message direct people to wp_cache_flush() / flush() instead or have the replacement parameter mention both the flush and switch methods (eg "flush() or switch_to_blog()").

The reason I bring this up is I use wp_cache_reset() in unit tests for my plugin to clean up the environment between tests so data is pulled from the database, not the cache. The existing deprecation message sent me on a wild goose chase in which I found this ticket, spent time composing this message (during which I thought to seek a similar method in the class, which led me to flush()). All in all, a good chunk of time, wasted. If the message had directed me to the similarly functioning flush method, I'd have been on my way in moments.

Oh, and can you please add the @since 3.5.0 tags to the new function / method?

  • Cc danielc@… added
  • Status changed from closed to reopened
  • Resolution fixed deleted

21434.6.diff adds @since for wp_cache_switch_to_blog() and WP_Object_Cache::switch_to_blog().

comment:7 in reply to: ↑ 4 ; follow-up: ↓ 9   ryan5 months ago

Replying to convissor:

The new, efficient code for switching between blogs is an excellent improvement. Thanks.

Can you please elaborate on the reasoning behind deprecating wp_cache_reset() / WP_Object_Cache::reset() and why the deprecation message instructs people to use wp_cache_switch_to_blog() / switch_to_blog()? The reset() and switch_to_blog() methods serve two separate purposes. (Though some people may have used them in conjunction.)

wp_cache_reset() was only ever implemented by the default, built-in cache. The other backends did not implement it. Now that we have switch_to_blog() it is even more useless since core never calls it.

  • Status changed from reopened to closed
  • Resolution set to fixed

In [22086]:

Add @since to wp_cache_switch_to_blog(). Props SergeyBiryukov. fixes #21434

comment:9 in reply to: ↑ 7   convissor5 months ago

Replying to ryan:

wp_cache_reset() was only ever implemented by the default, built-in cache. The other backends did not implement it. Now that we have switch_to_blog() it is even more useless since core never calls it.

Deprecating and removing the reset methods is fine. Even if the reset method was never used anywhere inside WP's core, the method was there for the general public to use. And they have, I'm living proof.

The existing deprecation messages point people to the switch_to_blog() method, but that method does not clear the cache.

The deprecation messages needs to be updated, please, to point people to a proper replacement: flush().

  • Status changed from closed to reopened
  • Resolution fixed deleted

flush() is not the proper replacement. It does nothing with some backends, especially for multisite.

We don't want to encourage flushing/resetting the cache, we want to encourage switch_to_blog(). For unit tests that use the default backend, consider overwriting the cache object. wp_cache_flush() is fine, though, when testing a specific backend that is known to support flushing for both single and multisite.

I'll add a note to the phpdoc. Patch coming up.

ryan4 months ago

Deprecation phpdoc for wp_cache_reset().

ryan4 months ago

Remove wp_cache_switch_to_blog() from _deprecated_function(). Note using wp_cache_init() in unit tests in phpdoc.

In [22111]:

Update phpdoc and deprecation for wp_cache_reset(). see #21434

  • Status changed from reopened to closed
  • Resolution set to fixed
Note: See TracTickets for help on using tickets.