Make WordPress Core

Changeset 13066


Ignore:
Timestamp:
02/12/2010 05:06:43 PM (17 years ago)
Author:
ryan
Message:

Preload commonly loaded site options when running multisite without a persistent cache. Introduce wp_cache_reset() and call it instead of wp_cache_init() when re-initing after the blog ID chanages to avoid throwing out the entire cache. Pass cached site options through the site option filter when fetching.

Location:
trunk/wp-includes
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/cache.php

    r13054 r13066  
    150150 */
    151151function wp_cache_add_global_groups( $groups ) {
    152         // Default cache doesn't persist so nothing to do here.
    153         return;
     152        global $wp_object_cache;
     153
     154        return $wp_object_cache->add_global_groups($groups);
    154155}
    155156
    … …  
    164165        // Default cache doesn't persist so nothing to do here.
    165166        return;
     167}
     168
     169/**
     170 * Reset internal cache keys and structures.  If the cache backend uses global blog or site IDs as part of its cache keys,
     171 * this function instructs the backend to reset those keys and perform any cleanup since blog or site IDs have changed since cache init.
     172 *
     173 * @since 2.6.0
     174 *
     175 * @param string|array $groups A group or an array of groups to add
     176 */
     177function wp_cache_reset() {
     178        global $wp_object_cache;
     179
     180        return $wp_object_cache->reset();
    166181}
    167182
    … …  
    219234         */
    220235        var $cache_misses = 0;
     236
     237        /**
     238         * List of global groups
     239         *
     240         * @var array
     241         * @access protected
     242         * @since 3.0.0
     243         */
     244        var $global_groups = array();
    221245
    222246        /**
    … …  
    235259         * @return bool False if cache ID and group already exists, true on success
    236260         */
    237         function add($id, $data, $group = 'default', $expire = '') {
    238                 if (empty ($group))
     261        function add( $id, $data, $group = 'default', $expire = '' ) {
     262                if ( empty ($group) )
    239263                        $group = 'default';
    240264
    … …  
    243267
    244268                return $this->set($id, $data, $group, $expire);
     269        }
     270
     271        /**
     272         * Sets the list of global groups.
     273         *
     274         * @since 3.0.0
     275         *
     276         * @param array $groups List of groups that are global.
     277         */
     278        function add_global_groups( $groups ) {
     279                $groups = (array) $groups;
     280
     281                $this->global_groups = array_merge($this->global_groups, $groups);
     282                $this->global_groups = array_unique($this->global_groups);
    245283        }
    246284
    … …  
    309347         */
    310348        function get($id, $group = 'default') {
    311                 if (empty ($group))
     349                if ( empty ($group) )
    312350                        $group = 'default';
    313351
    314                 if (isset ($this->cache[$group][$id])) {
     352                if ( isset ($this->cache[$group][$id]) ) {
    315353                        $this->cache_hits += 1;
    316354                        if ( is_object($this->cache[$group][$id]) )
    … …  
    344382                        $group = 'default';
    345383
    346                 if (false === $this->get($id, $group))
     384                if ( false === $this->get($id, $group) )
    347385                        return false;
    348386
    349387                return $this->set($id, $data, $group, $expire);
     388        }
     389
     390        /**
     391         * Reset keys
     392         *
     393         * @since 3.0.0
     394         */
     395        function reset() {
     396                // Clear out non-global caches since the blog ID has changed.
     397                foreach ( array_keys($this->cache) as $group ) {
     398                        if ( !in_array($group, $this->global_groups) )
     399                                unset($this->cache[$group]);
     400                }
    350401        }
    351402
    … …  
    371422         */
    372423        function set($id, $data, $group = 'default', $expire = '') {
    373                 if (empty ($group))
     424                if ( empty ($group) )
    374425                        $group = 'default';
    375426
    376                 if (NULL === $data)
     427                if ( NULL === $data )
    377428                        $data = '';
    378429
    … …  
    382433                $this->cache[$group][$id] = $data;
    383434
    384                 if(isset($this->non_existent_objects[$group][$id]))
     435                if ( isset($this->non_existent_objects[$group][$id]) )
    385436                        unset ($this->non_existent_objects[$group][$id]);
    386437
  • trunk/wp-includes/functions.php

    r13056 r13066  
    436436
    437437        return $alloptions;
     438}
     439
     440function wp_load_core_site_options( $site_id = null ) {
     441        global $wpdb, $_wp_using_ext_object_cache;
     442
     443        if ( !is_multisite() || $_wp_using_ext_object_cache || defined( 'WP_INSTALLING' ) )
     444                return;
     445
     446        if ( empty($site_id) )
     447                $site_id = $wpdb->siteid;
     448
     449        $core_options = array('site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'dashboard_blog');
     450
     451        $core_options_in = "'" . implode("', '", $core_options) . "'";
     452        $options = $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE meta_key IN ($core_options_in) AND site_id = %d", $site_id) );
     453
     454        foreach ( $options as $option ) {
     455                $key = $option->meta_key;
     456                $cache_key = "{$site_id}:$key";
     457                $option->meta_value = maybe_unserialize( $option->meta_value );
     458
     459                wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
     460        }
    438461}
    439462
    … …  
    33213344                $value = get_option($key, $default);
    33223345        } else {
    3323                 $cache_key = "{$wpdb->siteid}:$key";
    3324 
    3325                 if ( $use_cache == true && $value = wp_cache_get($cache_key, 'site-options') )
    3326                         return $value;
    3327 
    3328                 $value = $wpdb->get_var( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $key, $wpdb->siteid) );
    3329 
    3330                 if ( is_null($value) )
    3331                         $value = $default;
    3332 
    3333                 $value = maybe_unserialize( $value );
    3334 
    3335                 wp_cache_set( $cache_key, $value, 'site-options' );
     3346                $cache_key = "$wpdb->siteid:$key";
     3347                if ( $use_cache )
     3348                        $value = wp_cache_get($cache_key, 'site-options');
     3349
     3350                if ( false === $value ) {
     3351                        $value = $wpdb->get_var( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $key, $wpdb->siteid) );
     3352
     3353                        if ( is_null($value) )
     3354                                $value = $default;
     3355
     3356                        $value = maybe_unserialize( $value );
     3357
     3358                        wp_cache_set( $cache_key, $value, 'site-options' );
     3359                }
    33363360        }
    33373361
  • trunk/wp-includes/load.php

    r13064 r13066  
    319319 */
    320320function wp_start_object_cache() {
    321         if ( ! function_exists( 'wp_cache_init' ) ) {
     321        $first_init = false;
     322        if ( ! function_exists( 'wp_cache_init' ) ) {
    322323                global $_wp_using_ext_object_cache;
    323324                if ( file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
    … …  
    328329                        $_wp_using_ext_object_cache = false;
    329330                }
    330         }
    331 
    332         wp_cache_init();
     331                $first_init = true;
     332        }
     333
     334        // If cache supports reset, reset instead of init if already initialized.
     335        // Reset signals to the cache that global IDs have changed and it may need to update keys
     336        // and cleanup caches.
     337        if ( !$first_init && function_exists('wp_cache_reset') )
     338                wp_cache_reset();
     339        else
     340                wp_cache_init();
     341
    333342        if ( function_exists( 'wp_cache_add_global_groups' ) ) {
    334343                wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss' ) );
  • trunk/wp-includes/ms-load.php

    r13025 r13066  
    7676function get_current_site_name( $current_site ) {
    7777        global $wpdb;
    78         $current_site->site_name = wp_cache_get( $current_site->id . ':current_site_name', "site-options" );
     78        $current_site->site_name = wp_cache_get( $current_site->id . ':current_site_name', 'site-options' );
    7979        if ( ! $current_site->site_name ) {
    80                 $current_site->site_name = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE site_id = %d AND meta_key = 'site_name'", $current_site->id ) );
    81                 if ( ! $current_site->site_name )
    82                         $current_site->site_name = ucfirst( $current_site->domain );
     80                $current_site->site_name = wp_cache_get( $current_site->id . ':site_name', 'site-options' );
     81                if ( ! $current_site->site_name ) {
     82                        $current_site->site_name = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE site_id = %d AND meta_key = 'site_name'", $current_site->id ) );
     83                        if ( ! $current_site->site_name )
     84                                $current_site->site_name = ucfirst( $current_site->domain );
     85                }
    8386                wp_cache_set( $current_site->id . ':current_site_name', $current_site->site_name, 'site-options' );
    8487        }
    … …  
    108111                        $current_site->cookie_domain = $current_site->domain;
    109112
     113                wp_load_core_site_options($current_site->id);
     114
    110115                return $current_site;
    111116        }
    … …  
    117122        $sites = $wpdb->get_results( "SELECT * FROM $wpdb->site" ); // usually only one site
    118123        if ( 1 == count( $sites ) ) {
     124                wp_load_core_site_options($current_site->id);
    119125                $current_site = $sites[0];
    120126                $path = $current_site->path;
Note: See TracChangeset for help on using the changeset viewer.