Make WordPress Core


Ignore:
Timestamp:
09/15/2023 04:13:52 PM (7 months ago)
Author:
spacedmonkey
Message:

Options, Meta APIs: Optimize get_option by relocating notoptions cache lookup.

In the get_option function, a cache lookup for the notoptions key is performed, which stores an array of keys for options known not to exist. This optimization prevents repeated database queries when certain options are requested. However, the cache lookup for notoptions was conducted before checking if the requested option exists in the cache. Given that it's more likely that the option does exist, this commit reorders the checks to first verify the option's existence in the cache before confirming its absence. This adjustment reduces redundant queries and also eliminates an unnecessary cache lookup, improving overall performance.

Props spacedmonkey, costdev, flixos90, azaozz.
Fixes #58277.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/option/option.php

    r54147 r56595  
    102102
    103103    /**
     104     * @ticket 58277
     105     *
     106     * @covers ::get_option
     107     */
     108    public function test_get_option_notoptions_cache() {
     109        $notoptions = array(
     110            'invalid' => true,
     111        );
     112        wp_cache_set( 'notoptions', $notoptions, 'options' );
     113
     114        $before = get_num_queries();
     115        $value  = get_option( 'invalid' );
     116        $after  = get_num_queries();
     117
     118        $this->assertSame( 0, $after - $before );
     119    }
     120
     121    /**
     122     * @ticket 58277
     123     *
     124     * @covers ::get_option
     125     */
     126    public function test_get_option_notoptions_set_cache() {
     127        get_option( 'invalid' );
     128
     129        $before = get_num_queries();
     130        $value  = get_option( 'invalid' );
     131        $after  = get_num_queries();
     132
     133        $notoptions = wp_cache_get( 'notoptions', 'options' );
     134
     135        $this->assertSame( 0, $after - $before, 'The notoptions cache was not hit on the second call to `get_option()`.' );
     136        $this->assertIsArray( $notoptions, 'The notoptions cache should be set.' );
     137        $this->assertArrayHasKey( 'invalid', $notoptions, 'The "invalid" option should be in the notoptions cache.' );
     138    }
     139
     140    /**
     141     * @ticket 58277
     142     *
     143     * @covers ::get_option
     144     */
     145    public function test_get_option_notoptions_do_not_load_cache() {
     146        add_option( 'foo', 'bar', '', 'no' );
     147        wp_cache_delete( 'notoptions', 'options' );
     148
     149        $before = get_num_queries();
     150        $value  = get_option( 'foo' );
     151        $after  = get_num_queries();
     152
     153        $notoptions = wp_cache_get( 'notoptions', 'options' );
     154
     155        $this->assertSame( 0, $after - $before, 'The options cache was not hit on the second call to `get_option()`.' );
     156        $this->assertFalse( $notoptions, 'The notoptions cache should not be set.' );
     157    }
     158
     159    /**
    104160     * @covers ::get_option
    105161     * @covers ::add_option
Note: See TracChangeset for help on using the changeset viewer.