[wp-trac] [WordPress Trac] #23290: When using switch_to_blog() with a persistent object cache that lacks wp_cache_switch_to_blog() support, non-persistent groups are not maintained

WordPress Trac noreply at wordpress.org
Thu Feb 19 10:29:27 UTC 2026


#23290: When using switch_to_blog() with a persistent object cache that lacks
wp_cache_switch_to_blog() support, non-persistent groups are not maintained
-------------------------------------+------------------------------
 Reporter:  markjaquith              |       Owner:  johnjamesjacoby
     Type:  defect (bug)             |      Status:  assigned
 Priority:  low                      |   Milestone:  7.0
Component:  Cache API                |     Version:  3.0
 Severity:  normal                   |  Resolution:
 Keywords:  has-patch needs-testing  |     Focuses:  multisite
-------------------------------------+------------------------------

Comment (by ozgursar):

 I only tested one the patches (`23290-3.patch`) that @johnjamesjacoby
 submitted.

 == Patch Testing Report

 Patch Tested:
 https://core.trac.wordpress.org/attachment/ticket/23290/23290-3.patch

 === Environment
 - WordPress: 7.0-alpha-61215-src
 - PHP: 8.2.29
 - Server: nginx/1.29.4
 - Database: mysqli (Server: 8.4.7 / Client: mysqlnd 8.2.29)
 - Browser: Chrome 145.0.0.0
 - OS: macOS
 - Theme: Twenty Twenty-Five 1.4
 - MU Plugins: None activated
 - Plugins:
   * Code Snippets 3.9.5
   * SQLite Object Cache 1.6.1
   * Test Reports 1.2.1

 === Steps taken
 1. Enable multisite by adding the following values to the wp-config.php
 and add a second site to the network.


 {{{
 define( 'WP_ALLOW_MULTISITE', true );
 define( 'MULTISITE', true );
 define( 'SUBDOMAIN_INSTALL', false );
 define( 'DOMAIN_CURRENT_SITE', 'localhost:8889' );
 define( 'PATH_CURRENT_SITE', '/' );
 define( 'SITE_ID_CURRENT_SITE', 1 );
 define( 'BLOG_ID_CURRENT_SITE', 1 );
 }}}


 2. Install and activate `SQLite Object Cache` plugin
 3. Edit `/wp-content/object-cache.php` and comment out the function
 `wp_cache_switch_to_blog()` between the lines `3084-3088` which implements
 the `switch_to_blog()`


 {{{
 function wp_cache_switch_to_blog( $blog_id ) {
 //   global $wp_object_cache;

 //   $wp_object_cache->switch_to_blog( $blog_id );
 }
 }}}

 4. Add the test snippet below via `Code Snippets` plugin or via
 `functions.php`
 5. Visit any page of Dashboard to view the test results
 6. ✅ Patch is solving the problem

 === Expected result
 After applying the patch, when `switch_to_blog()` is called on a multisite
 installation using a persistent object cache drop-in that does not
 implement `wp_cache_switch_to_blog()`, custom non-persistent cache groups
 should be preserved.

 Specifically, a value stored in a custom non-persistent group
 (`my_test_group`) before the blog switch should still be retrievable after
 `switch_to_blog()` and `restore_current_blog()` are called. The admin
 notice should display a green success message confirming that the value
 `test_value` survived the switch — matching the value stored before the
 switch.

 === Additional Notes
 Without the patch, the value is lost (returns `false`) because
 `wp_cache_init()` wipes the cache and non-persistent groups are not
 restored. With the patch, the fallback `wp_cache_switch_to_blog()`
 function added to `cache-compat.php` correctly saves and restores non-
 persistent groups, so the value survives.

 === Screenshots/Screencast with results
 Before patch:
 [[Image(https://i.imgur.com/xgVlXqY.png)]]

 After patch:
 [[Image(https://i.imgur.com/kKyPtG9.png)]]

 === Support Content
 - This test snippet is created with the help of `Claude.ai`


 {{{
 add_action( 'admin_notices', function () {
     if ( ! is_multisite() ) {
         echo '<div class="notice notice-warning"><p><strong>Trac #23290
 Test:</strong> Multisite is required.</p></div>';
         return;
     }

     // Check a second site exists
     $sites = get_sites( [ 'number' => 2 ] );
     if ( count( $sites ) < 2 ) {
         echo '<div class="notice notice-warning"><p><strong>Trac #23290
 Test:</strong> A second site is required. Please create one under My Sites
 → Network Admin → Sites → Add New.</p></div>';
         return;
     }

     $other_blog_id = (int) $sites[1]->blog_id;

     wp_cache_add_non_persistent_groups( [ 'my_test_group' ] );
     wp_cache_set( 'test_key', 'test_value', 'my_test_group' );

     $before = wp_cache_get( 'test_key', 'my_test_group' );

     switch_to_blog( $other_blog_id ); // switch to a DIFFERENT blog
     restore_current_blog();

     $after = wp_cache_get( 'test_key', 'my_test_group' );

     $survived = ( 'test_value' === $after );
     $type     = $survived ? 'notice-success' : 'notice-error';
     $status   = $survived
         ? '✅ PASS — Non-persistent group survived switch_to_blog() (patch
 working).'
         : '❌ FAIL — Non-persistent group was LOST after switch_to_blog()
 (bug confirmed).';

     echo '<div class="notice ' . $type . '"><p>'
         . '<strong>Trac #23290 — Non-Persistent Cache Groups
 Test</strong><br>'
         . $status . '<br>'
         . '<small>'
         . 'Switched to blog ID: <code>' . $other_blog_id . '</code><br>'
         . 'Value before switch: <code>' . var_export( $before, true ) .
 '</code><br>'
         . 'Value after switch: <code>' . var_export( $after, true ) .
 '</code>'
         . '</small>'
         . '</p></div>';
 } );
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/23290#comment:22>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list