Introduce `wp_using_ext_object_cache()` - mimic `wp_suspend_cache_invalidation()` and discourage direct access to `$_wp_using_ext_object_cache`, cleaning up importing of globals in functions and provides function to modify that global. Loads the packaged object cache when an external cache hasn't been loaded or doesn't contain `wp_cache_init()`.

Fixes #21401.


Built from https://develop.svn.wordpress.org/trunk@25289


git-svn-id: http://core.svn.wordpress.org/trunk@25253 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2013-09-06 18:10:09 +00:00
parent 74092c3960
commit d7cc1f506a
5 changed files with 41 additions and 35 deletions

View File

@ -28,10 +28,10 @@ if ( !defined('ABSPATH') ) {
// Uncached doing_cron transient fetch
function _get_cron_lock() {
global $_wp_using_ext_object_cache, $wpdb;
global $wpdb;
$value = 0;
if ( $_wp_using_ext_object_cache ) {
if ( wp_using_ext_object_cache() ) {
// Skip local cache and force refetch of doing_cron transient in case
// another processs updated the cache
$value = wp_cache_get( 'doing_cron', 'transient', true );

View File

@ -369,6 +369,24 @@ function wp_set_wpdb_vars() {
}
}
/**
* Access/Modify private global variable $_wp_using_ext_object_cache
*
* Toggle $_wp_using_ext_object_cache on and off without directly touching global
*
* @since 3.7.0
*
* @param bool $using Whether external object cache is being used
* @return bool The current 'using' setting
*/
function wp_using_ext_object_cache( $using = null ) {
global $_wp_using_ext_object_cache;
$current_using = $_wp_using_ext_object_cache;
if ( null !== $using )
$_wp_using_ext_object_cache = $using;
return $current_using;
}
/**
* Starts the WordPress object cache.
*
@ -379,31 +397,33 @@ function wp_set_wpdb_vars() {
* @since 3.0.0
*/
function wp_start_object_cache() {
global $_wp_using_ext_object_cache, $blog_id;
global $blog_id;
$first_init = false;
if ( ! function_exists( 'wp_cache_init' ) ) {
if ( file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
require_once ( WP_CONTENT_DIR . '/object-cache.php' );
$_wp_using_ext_object_cache = true;
} else {
require_once ( ABSPATH . WPINC . '/cache.php' );
$_wp_using_ext_object_cache = false;
if ( function_exists( 'wp_cache_init' ) )
wp_using_ext_object_cache( true );
}
$first_init = true;
} else if ( !$_wp_using_ext_object_cache && file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
} else if ( ! wp_using_ext_object_cache() && file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
// Sometimes advanced-cache.php can load object-cache.php before it is loaded here.
// This breaks the function_exists check above and can result in $_wp_using_ext_object_cache
// being set incorrectly. Double check if an external cache exists.
$_wp_using_ext_object_cache = true;
wp_using_ext_object_cache( true );
}
if ( ! wp_using_ext_object_cache() )
require_once ( ABSPATH . WPINC . '/cache.php' );
// If cache supports reset, reset instead of init if already initialized.
// Reset signals to the cache that global IDs have changed and it may need to update keys
// and cleanup caches.
if ( ! $first_init && function_exists( 'wp_cache_switch_to_blog' ) )
wp_cache_switch_to_blog( $blog_id );
else
elseif ( function_exists( 'wp_cache_init' ) )
wp_cache_init();
if ( function_exists( 'wp_cache_add_global_groups' ) ) {

View File

@ -476,8 +476,6 @@ function _is_valid_nav_menu_item( $item ) {
* @return mixed $items array of menu items, else false.
*/
function wp_get_nav_menu_items( $menu, $args = array() ) {
global $_wp_using_ext_object_cache;
$menu = wp_get_nav_menu_object( $menu );
if ( ! $menu )
@ -504,7 +502,7 @@ function wp_get_nav_menu_items( $menu, $args = array() ) {
return false;
// Get all posts and terms at once to prime the caches
if ( empty( $fetched[$menu->term_id] ) || $_wp_using_ext_object_cache ) {
if ( empty( $fetched[$menu->term_id] ) || wp_using_ext_object_cache() ) {
$fetched[$menu->term_id] = true;
$posts = array();
$terms = array();

View File

@ -165,9 +165,9 @@ function wp_load_alloptions() {
* @param int $site_id Optional site ID for which to query the options. Defaults to the current site.
*/
function wp_load_core_site_options( $site_id = null ) {
global $wpdb, $_wp_using_ext_object_cache;
global $wpdb;
if ( !is_multisite() || $_wp_using_ext_object_cache || defined( 'WP_INSTALLING' ) )
if ( !is_multisite() || wp_using_ext_object_cache() || defined( 'WP_INSTALLING' ) )
return;
if ( empty($site_id) )
@ -404,11 +404,9 @@ function delete_option( $option ) {
* @return bool true if successful, false otherwise
*/
function delete_transient( $transient ) {
global $_wp_using_ext_object_cache;
do_action( 'delete_transient_' . $transient, $transient );
if ( $_wp_using_ext_object_cache ) {
if ( wp_using_ext_object_cache() ) {
$result = wp_cache_delete( $transient, 'transient' );
} else {
$option_timeout = '_transient_timeout_' . $transient;
@ -443,13 +441,11 @@ function delete_transient( $transient ) {
* @return mixed Value of transient
*/
function get_transient( $transient ) {
global $_wp_using_ext_object_cache;
$pre = apply_filters( 'pre_transient_' . $transient, false );
if ( false !== $pre )
return $pre;
if ( $_wp_using_ext_object_cache ) {
if ( wp_using_ext_object_cache() ) {
$value = wp_cache_get( $transient, 'transient' );
} else {
$transient_option = '_transient_' . $transient;
@ -493,11 +489,9 @@ function get_transient( $transient ) {
* @return bool False if value was not set and true if value was set.
*/
function set_transient( $transient, $value, $expiration = 0 ) {
global $_wp_using_ext_object_cache;
$value = apply_filters( 'pre_set_transient_' . $transient, $value );
if ( $_wp_using_ext_object_cache ) {
if ( wp_using_ext_object_cache() ) {
$result = wp_cache_set( $transient, $value, 'transient', $expiration );
} else {
$transient_timeout = '_transient_timeout_' . $transient;
@ -970,10 +964,8 @@ function update_site_option( $option, $value ) {
* @return bool True if successful, false otherwise
*/
function delete_site_transient( $transient ) {
global $_wp_using_ext_object_cache;
do_action( 'delete_site_transient_' . $transient, $transient );
if ( $_wp_using_ext_object_cache ) {
if ( wp_using_ext_object_cache() ) {
$result = wp_cache_delete( $transient, 'site-transient' );
} else {
$option_timeout = '_site_transient_timeout_' . $transient;
@ -1008,13 +1000,11 @@ function delete_site_transient( $transient ) {
* @return mixed Value of transient
*/
function get_site_transient( $transient ) {
global $_wp_using_ext_object_cache;
$pre = apply_filters( 'pre_site_transient_' . $transient, false );
if ( false !== $pre )
return $pre;
if ( $_wp_using_ext_object_cache ) {
if ( wp_using_ext_object_cache() ) {
$value = wp_cache_get( $transient, 'site-transient' );
} else {
// Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
@ -1058,11 +1048,9 @@ function get_site_transient( $transient ) {
* @return bool False if value was not set and true if value was set.
*/
function set_site_transient( $transient, $value, $expiration = 0 ) {
global $_wp_using_ext_object_cache;
$value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
if ( $_wp_using_ext_object_cache ) {
if ( wp_using_ext_object_cache() ) {
$result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
} else {
$transient_timeout = '_site_transient_timeout_' . $transient;

View File

@ -1946,7 +1946,7 @@ class WP_Query {
* @return array List of posts.
*/
function get_posts() {
global $wpdb, $user_ID, $_wp_using_ext_object_cache;
global $wpdb, $user_ID;
$this->parse_query();
@ -1996,7 +1996,7 @@ class WP_Query {
$q['suppress_filters'] = false;
if ( !isset($q['cache_results']) ) {
if ( $_wp_using_ext_object_cache )
if ( wp_using_ext_object_cache() )
$q['cache_results'] = false;
else
$q['cache_results'] = true;