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. Built from https://develop.svn.wordpress.org/trunk@56595 git-svn-id: http://core.svn.wordpress.org/trunk@56107 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
e458b35a96
commit
127209a9fe
|
@ -161,6 +161,14 @@ function get_option( $option, $default_value = false ) {
|
|||
$passed_default = func_num_args() > 1;
|
||||
|
||||
if ( ! wp_installing() ) {
|
||||
$alloptions = wp_load_alloptions();
|
||||
|
||||
if ( isset( $alloptions[ $option ] ) ) {
|
||||
$value = $alloptions[ $option ];
|
||||
} else {
|
||||
$value = wp_cache_get( $option, 'options' );
|
||||
|
||||
if ( false === $value ) {
|
||||
// Prevent non-existent options from triggering multiple queries.
|
||||
$notoptions = wp_cache_get( 'notoptions', 'options' );
|
||||
|
||||
|
@ -168,9 +176,7 @@ function get_option( $option, $default_value = false ) {
|
|||
if ( ! is_array( $notoptions ) ) {
|
||||
$notoptions = array();
|
||||
wp_cache_set( 'notoptions', $notoptions, 'options' );
|
||||
}
|
||||
|
||||
if ( isset( $notoptions[ $option ] ) ) {
|
||||
} elseif ( isset( $notoptions[ $option ] ) ) {
|
||||
/**
|
||||
* Filters the default value for an option.
|
||||
*
|
||||
|
@ -188,14 +194,6 @@ function get_option( $option, $default_value = false ) {
|
|||
return apply_filters( "default_option_{$option}", $default_value, $option, $passed_default );
|
||||
}
|
||||
|
||||
$alloptions = wp_load_alloptions();
|
||||
|
||||
if ( isset( $alloptions[ $option ] ) ) {
|
||||
$value = $alloptions[ $option ];
|
||||
} else {
|
||||
$value = wp_cache_get( $option, 'options' );
|
||||
|
||||
if ( false === $value ) {
|
||||
$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
|
||||
|
||||
// Has to be get_row() instead of get_var() because of funkiness with 0, false, null values.
|
||||
|
@ -203,10 +201,6 @@ function get_option( $option, $default_value = false ) {
|
|||
$value = $row->option_value;
|
||||
wp_cache_add( $option, $value, 'options' );
|
||||
} else { // Option does not exist, so we must cache its non-existence.
|
||||
if ( ! is_array( $notoptions ) ) {
|
||||
$notoptions = array();
|
||||
}
|
||||
|
||||
$notoptions[ $option ] = true;
|
||||
wp_cache_set( 'notoptions', $notoptions, 'options' );
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '6.4-alpha-56594';
|
||||
$wp_version = '6.4-alpha-56595';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
Loading…
Reference in New Issue