WordPress.org

Make WordPress Core

Changeset 42723


Ignore:
Timestamp:
02/21/2018 02:58:14 PM (5 months ago)
Author:
markjaquith
Message:

Cache API: Allow external object caches to gracefully degrade to the default object cache.

Rework logic for how external object caches are detected, so that if
an external cache does not define a wp_cache_init(), the built-in
object cache will be used.

Object caches can now wrap their entire contents in logic checks. So a
Redis caching backend could make sure that the Redis PHP class is
available before defining all the caching functions. And if Redis is
not available, the site doesn't break or throw errors or think it is
using caching when it isn't. This is particularly useful for doing
local development, where you might want to develop on a site without
running Memcache or Redis like you are in production.

  • Accounts for multisite, which may re-initialize the object cache

multiple times.

  • Accounts for object caches that may include object-cache.php during

advanced-cache.php (before WP loads it).

Props jtsternberg, markjaquith.
Fixes #22661.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/load.php

    r42710 r42723  
    531531function wp_start_object_cache() {
    532532    global $wp_filter;
    533 
    534     $first_init = false;
    535     if ( ! function_exists( 'wp_cache_init' ) ) {
    536         if ( file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
    537             require_once( WP_CONTENT_DIR . '/object-cache.php' );
    538             if ( function_exists( 'wp_cache_init' ) ) {
    539                 wp_using_ext_object_cache( true );
     533    static $first_init = true;
     534
     535    // Only perform the following checks once.
     536    if ( $first_init ) {
     537        if ( ! function_exists( 'wp_cache_init' ) ) {
     538            /*
     539             * This is the normal situation. First-run of this function. No
     540             * caching backend has been loaded.
     541             *
     542             * We try to load a custom caching backend, and then, if it
     543             * results in a wp_cache_init() function existing, we note
     544             * that an external object cache is being used.
     545             */
     546            if ( file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
     547                require_once( WP_CONTENT_DIR . '/object-cache.php' );
     548                if ( function_exists( 'wp_cache_init' ) ) {
     549                    wp_using_ext_object_cache( true );
     550                }
     551
     552                // Re-initialize any hooks added manually by object-cache.php
     553                if ( $wp_filter ) {
     554                    $wp_filter = WP_Hook::build_preinitialized_hooks( $wp_filter );
     555                }
    540556            }
    541 
    542             // Re-initialize any hooks added manually by object-cache.php
    543             if ( $wp_filter ) {
    544                 $wp_filter = WP_Hook::build_preinitialized_hooks( $wp_filter );
    545             }
    546         }
    547 
    548         $first_init = true;
    549     } elseif ( ! wp_using_ext_object_cache() && file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
    550         /*
    551          * Sometimes advanced-cache.php can load object-cache.php before
    552          * it is loaded here. This breaks the function_exists check above
    553          * and can result in `$_wp_using_ext_object_cache` being set
    554          * incorrectly. Double check if an external cache exists.
    555          */
    556         wp_using_ext_object_cache( true );
     557        } elseif ( ! wp_using_ext_object_cache() && file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
     558            /*
     559             * Sometimes advanced-cache.php can load object-cache.php before
     560             * this function is run. This breaks the function_exists() check
     561             * above and can result in wp_using_ext_object_cache() returning
     562             * false when actually an external cache is in use.
     563             */
     564            wp_using_ext_object_cache( true );
     565        }
    557566    }
    558567
     
    576585        wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
    577586    }
     587
     588    $first_init = false;
    578589}
    579590
Note: See TracChangeset for help on using the changeset viewer.