Skip to:
Content

BuddyPress.org

Changeset 7819


Ignore:
Timestamp:
02/07/2014 01:54:29 AM (11 years ago)
Author:
boonebgorges
Message:

Add persistent caching for bp_core_get_root_options()

See #5362

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-cache.php

    r7816 r7819  
    9797}
    9898add_action( 'update_option', 'bp_core_clear_directory_pages_cache_settings_edit' );
     99
     100/**
     101 * Clear the root_blog_options cache when any of its options are updated.
     102 *
     103 * @since BuddyPress (2.0.0)
     104 *
     105 * @param string $option Option name.
     106 */
     107function bp_core_clear_root_options_cache( $option ) {
     108    $keys = array_keys( bp_get_default_options() );
     109    $keys = array_merge( $keys, array(
     110        'registration',
     111        'avatar_default',
     112        'tags_blog_id',
     113        'sitewide_tags_blog',
     114        'registration',
     115        'fileupload_mask',
     116    ) );
     117
     118    if ( in_array( $option, $keys ) ) {
     119        wp_cache_delete( 'root_blog_options', 'bp' );
     120    }
     121}
     122add_action( 'update_option', 'bp_core_clear_root_options_cache' );
     123add_action( 'update_site_option', 'bp_core_clear_root_options_cache' );
     124add_action( 'add_option', 'bp_core_clear_root_options_cache' );
     125add_action( 'add_site_option', 'bp_core_clear_root_options_cache' );
    99126
    100127/**
  • trunk/bp-core/bp-core-options.php

    r7451 r7819  
    305305
    306306    // Do some magic to get all the root blog options in 1 swoop
    307     $blog_options_keys      = "'" . join( "', '", (array) $root_blog_option_keys ) . "'";
    308     $blog_options_table     = bp_is_multiblog_mode() ? $wpdb->options : $wpdb->get_blog_prefix( bp_get_root_blog_id() ) . 'options';
    309     $blog_options_query     = "SELECT option_name AS name, option_value AS value FROM {$blog_options_table} WHERE option_name IN ( {$blog_options_keys} )";
    310     $root_blog_options_meta = $wpdb->get_results( $blog_options_query );
    311 
    312     // On Multisite installations, some options must always be fetched from sitemeta
    313     if ( is_multisite() ) {
    314         $network_options = apply_filters( 'bp_core_network_options', array(
    315             'tags_blog_id'       => '0',
    316             'sitewide_tags_blog' => '',
    317             'registration'       => '0',
    318             'fileupload_maxk'    => '1500'
    319         ) );
    320 
    321         $current_site           = get_current_site();
    322         $network_option_keys    = array_keys( $network_options );
    323         $sitemeta_options_keys  = "'" . join( "', '", (array) $network_option_keys ) . "'";
    324         $sitemeta_options_query = $wpdb->prepare( "SELECT meta_key AS name, meta_value AS value FROM {$wpdb->sitemeta} WHERE meta_key IN ( {$sitemeta_options_keys} ) AND site_id = %d", $current_site->id );
    325         $network_options_meta   = $wpdb->get_results( $sitemeta_options_query );
    326 
    327         // Sitemeta comes second in the merge, so that network 'registration' value wins
    328         $root_blog_options_meta = array_merge( $root_blog_options_meta, $network_options_meta );
    329     }
    330 
    331     // Missing some options, so do some one-time fixing
    332     if ( empty( $root_blog_options_meta ) || ( count( $root_blog_options_meta ) < count( $root_blog_option_keys ) ) ) {
    333 
    334         // Get a list of the keys that are already populated
    335         $existing_options = array();
    336         foreach( $root_blog_options_meta as $already_option ) {
    337             $existing_options[$already_option->name] = $already_option->value;
     307    // Check cache first - We cache here instead of using the standard WP
     308    // settings cache because the current blog may not be the root blog,
     309    // and it's not practical to access the cache across blogs
     310    $root_blog_options_meta = wp_cache_get( 'root_blog_options', 'bp' );
     311
     312    if ( false === $root_blog_options_meta ) {
     313        $blog_options_keys      = "'" . join( "', '", (array) $root_blog_option_keys ) . "'";
     314        $blog_options_table     = bp_is_multiblog_mode() ? $wpdb->options : $wpdb->get_blog_prefix( bp_get_root_blog_id() ) . 'options';
     315        $blog_options_query     = "SELECT option_name AS name, option_value AS value FROM {$blog_options_table} WHERE option_name IN ( {$blog_options_keys} )";
     316        $root_blog_options_meta = $wpdb->get_results( $blog_options_query );
     317
     318        // On Multisite installations, some options must always be fetched from sitemeta
     319        if ( is_multisite() ) {
     320            $network_options = apply_filters( 'bp_core_network_options', array(
     321                'tags_blog_id'       => '0',
     322                'sitewide_tags_blog' => '',
     323                'registration'       => '0',
     324                'fileupload_maxk'    => '1500'
     325            ) );
     326
     327            $current_site           = get_current_site();
     328            $network_option_keys    = array_keys( $network_options );
     329            $sitemeta_options_keys  = "'" . join( "', '", (array) $network_option_keys ) . "'";
     330            $sitemeta_options_query = $wpdb->prepare( "SELECT meta_key AS name, meta_value AS value FROM {$wpdb->sitemeta} WHERE meta_key IN ( {$sitemeta_options_keys} ) AND site_id = %d", $current_site->id );
     331            $network_options_meta   = $wpdb->get_results( $sitemeta_options_query );
     332
     333            // Sitemeta comes second in the merge, so that network 'registration' value wins
     334            $root_blog_options_meta = array_merge( $root_blog_options_meta, $network_options_meta );
    338335        }
    339336
    340         // Unset the query - We'll be resetting it soon
    341         unset( $root_blog_options_meta );
    342 
    343         // Loop through options
    344         foreach ( $root_blog_options as $old_meta_key => $old_meta_default ) {
    345             // Clear out the value from the last time around
    346             unset( $old_meta_value );
    347 
    348             if ( isset( $existing_options[$old_meta_key] ) ) {
    349                 continue;
     337        // Missing some options, so do some one-time fixing
     338        if ( empty( $root_blog_options_meta ) || ( count( $root_blog_options_meta ) < count( $root_blog_option_keys ) ) ) {
     339
     340            // Get a list of the keys that are already populated
     341            $existing_options = array();
     342            foreach( $root_blog_options_meta as $already_option ) {
     343                $existing_options[$already_option->name] = $already_option->value;
    350344            }
    351345
    352             // Get old site option
    353             if ( is_multisite() )
    354                 $old_meta_value = get_site_option( $old_meta_key );
    355 
    356             // No site option so look in root blog
    357             if ( empty( $old_meta_value ) )
    358                 $old_meta_value = bp_get_option( $old_meta_key, $old_meta_default );
    359 
    360             // Update the root blog option
    361             bp_update_option( $old_meta_key, $old_meta_value );
    362 
    363             // Update the global array
    364             $root_blog_options_meta[$old_meta_key] = $old_meta_value;
     346            // Unset the query - We'll be resetting it soon
     347            unset( $root_blog_options_meta );
     348
     349            // Loop through options
     350            foreach ( $root_blog_options as $old_meta_key => $old_meta_default ) {
     351                // Clear out the value from the last time around
     352                unset( $old_meta_value );
     353
     354                if ( isset( $existing_options[$old_meta_key] ) ) {
     355                    continue;
     356                }
     357
     358                // Get old site option
     359                if ( is_multisite() )
     360                    $old_meta_value = get_site_option( $old_meta_key );
     361
     362                // No site option so look in root blog
     363                if ( empty( $old_meta_value ) )
     364                    $old_meta_value = bp_get_option( $old_meta_key, $old_meta_default );
     365
     366                // Update the root blog option
     367                bp_update_option( $old_meta_key, $old_meta_value );
     368
     369                // Update the global array
     370                $root_blog_options_meta[$old_meta_key] = $old_meta_value;
     371            }
     372
     373            $root_blog_options_meta = array_merge( $root_blog_options_meta, $existing_options );
     374            unset( $existing_options );
     375
     376        // We're all matched up
     377        } else {
     378            // Loop through our results and make them usable
     379            foreach ( $root_blog_options_meta as $root_blog_option )
     380                $root_blog_options[$root_blog_option->name] = $root_blog_option->value;
     381
     382            // Copy the options no the return val
     383            $root_blog_options_meta = $root_blog_options;
     384
     385            // Clean up our temporary copy
     386            unset( $root_blog_options );
    365387        }
    366388
    367         $root_blog_options_meta = array_merge( $root_blog_options_meta, $existing_options );
    368         unset( $existing_options );
    369 
    370     // We're all matched up
    371     } else {
    372         // Loop through our results and make them usable
    373         foreach ( $root_blog_options_meta as $root_blog_option )
    374             $root_blog_options[$root_blog_option->name] = $root_blog_option->value;
    375 
    376         // Copy the options no the return val
    377         $root_blog_options_meta = $root_blog_options;
    378 
    379         // Clean up our temporary copy
    380         unset( $root_blog_options );
     389        wp_cache_set( 'root_blog_options', $root_blog_options_meta, 'bp' );
    381390    }
    382391
  • trunk/tests/testcases/core/functions.php

    r7809 r7819  
    297297    }
    298298
     299    /**
     300     * @group bp_core_get_root_options
     301     */
     302    public function test_bp_core_get_root_options_cache_invalidate() {
     303        $keys = array_keys( bp_get_default_options() );
     304        $keys[] = 'registration';
     305        $keys[] = 'avatar_default';
     306
     307        foreach ( $keys as $key ) {
     308            // prime cache
     309            $root_options = bp_core_get_root_options();
     310
     311            bp_update_option( $key, 'foo' );
     312
     313            $this->assertFalse( wp_cache_get( 'root_blog_options', 'bp' ), 'Cache not invalidated after updating "' . $key . '"' );
     314        }
     315
     316        if ( is_multisite() ) {
     317            $ms_keys = array(
     318                'tags_blog_id',
     319                'sitewide_tags_blog',
     320                'registration',
     321                'fileupload_mask',
     322            );
     323
     324            foreach ( $ms_keys as $ms_key ) {
     325                $root_options = bp_core_get_root_options();
     326
     327                update_site_option( $ms_key, 'foooooooo' );
     328
     329                $this->assertFalse( wp_cache_get( 'root_blog_options', 'bp' ), 'Cache not invalidated after updating "' . $ms_key . '"' );
     330            }
     331        }
     332    }
    299333}
Note: See TracChangeset for help on using the changeset viewer.