[wp-trac] [WordPress Trac] #37178: Add $cache_only parameter to get_option
WordPress Trac
noreply at wordpress.org
Sun Jun 26 12:29:22 UTC 2016
#37178: Add $cache_only parameter to get_option
------------------------------------+------------------------------
Reporter: joostdevalk | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version:
Severity: normal | Resolution:
Keywords: has-patch dev-feedback | Focuses: performance
------------------------------------+------------------------------
Comment (by SergeyBiryukov):
Replying to [ticket:37178 joostdevalk]:
> The "workaround" currently would be to set an empty option, but that
means adding unnecessary data to the database.
Another workaround would be adding a `pre_option_*` filter for that option
that returns an empty string or `null` to short-circuit the function and
avoid the SQL query:
{{{
function wp37178_get_autoloaded_option( $option, $default = false ) {
// prevent non-existent options from triggering multiple queries
$notoptions = wp_cache_get( 'notoptions', 'options' );
if ( isset( $notoptions[ $option ] ) ) {
return $default;
}
$alloptions = wp_load_alloptions();
if ( isset( $alloptions[$option] ) ) {
$value = $alloptions[$option];
} else {
$value = wp_cache_get( $option, 'options' );
}
if ( ! $value ) {
$value = null;
}
return maybe_unserialize( $value );
}
add_filter( 'pre_option_myoption', 'wp37178_get_autoloaded_option' );
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/37178#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list