WordPress.org

Make WordPress Core

Changeset 42833


Ignore:
Timestamp:
03/13/2018 03:36:14 PM (4 years ago)
Author:
flixos90
Message:

Multisite: Ensure the {$network_id}:notoptions array is set in cache in get_network_option().

Prior to this change, the {$network_id}:notoptions cache would only be fetched, but not set, unless the actual database lookup would be unsuccessful. This enhancement slightly improves performance by preventing unnecessary external object cache lookups if one is used.

Fixes #43506.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/option.php

    r42655 r42833  
    12481248    $notoptions     = wp_cache_get( $notoptions_key, 'site-options' );
    12491249
    1250     if ( isset( $notoptions[ $option ] ) ) {
     1250    if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
    12511251
    12521252        /**
     
    12941294            }
    12951295        }
     1296    }
     1297
     1298    if ( ! is_array( $notoptions ) ) {
     1299        $notoptions = array();
     1300        wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
    12961301    }
    12971302
  • trunk/tests/phpunit/tests/option/networkOption.php

    r42343 r42833  
    122122        );
    123123    }
     124
     125    /**
     126     * @ticket 43506
     127     */
     128    public function test_get_network_option_sets_notoptions_if_option_found() {
     129        $network_id     = get_current_network_id();
     130        $notoptions_key = "$network_id:notoptions";
     131
     132        $original_cache = wp_cache_get( $notoptions_key, 'site-options' );
     133        if ( false !== $original_cache ) {
     134            wp_cache_delete( $notoptions_key, 'site-options' );
     135        }
     136
     137        // Retrieve any existing option.
     138        get_network_option( $network_id, 'site_name' );
     139
     140        $cache = wp_cache_get( $notoptions_key, 'site-options' );
     141        if ( false !== $original_cache ) {
     142            wp_cache_set( $notoptions_key, $original_cache, 'site-options' );
     143        }
     144
     145        $this->assertSame( array(), $cache );
     146    }
     147
     148    /**
     149     * @ticket 43506
     150     */
     151    public function test_get_network_option_sets_notoptions_if_option_not_found() {
     152        $network_id     = get_current_network_id();
     153        $notoptions_key = "$network_id:notoptions";
     154
     155        $original_cache = wp_cache_get( $notoptions_key, 'site-options' );
     156        if ( false !== $original_cache ) {
     157            wp_cache_delete( $notoptions_key, 'site-options' );
     158        }
     159
     160        // Retrieve any non-existing option.
     161        get_network_option( $network_id, 'this_does_not_exist' );
     162
     163        $cache = wp_cache_get( $notoptions_key, 'site-options' );
     164        if ( false !== $original_cache ) {
     165            wp_cache_set( $notoptions_key, $original_cache, 'site-options' );
     166        }
     167
     168        $this->assertSame( array( 'this_does_not_exist' => true ), $cache );
     169    }
    124170}
Note: See TracChangeset for help on using the changeset viewer.