From d7cc1f506ad3f5a4bac357b2b1b4bb073151438d Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 6 Sep 2013 18:10:09 +0000 Subject: [PATCH] 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 --- wp-cron.php | 4 ++-- wp-includes/load.php | 36 ++++++++++++++++++++++++++++-------- wp-includes/nav-menu.php | 4 +--- wp-includes/option.php | 28 ++++++++-------------------- wp-includes/query.php | 4 ++-- 5 files changed, 41 insertions(+), 35 deletions(-) diff --git a/wp-cron.php b/wp-cron.php index 04953c8d73..21706cbaf2 100644 --- a/wp-cron.php +++ b/wp-cron.php @@ -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 ); diff --git a/wp-includes/load.php b/wp-includes/load.php index e1eb15e4bb..6fb608b365 100644 --- a/wp-includes/load.php +++ b/wp-includes/load.php @@ -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' ) ) { diff --git a/wp-includes/nav-menu.php b/wp-includes/nav-menu.php index 6330af2604..cf9ac1427e 100644 --- a/wp-includes/nav-menu.php +++ b/wp-includes/nav-menu.php @@ -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(); diff --git a/wp-includes/option.php b/wp-includes/option.php index 2df45b75cf..6408303e22 100644 --- a/wp-includes/option.php +++ b/wp-includes/option.php @@ -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; diff --git a/wp-includes/query.php b/wp-includes/query.php index 07838ab8b6..8da7e68f09 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -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;