Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/06/2014 06:58:17 PM (11 years ago)
Author:
boonebgorges
Message:

Store directory_pages data in the persistent cache.

This information is needed during every bootstrap, yet rarely changes, so there
should be significant performance benefit from caching it.

Invalidation takes place when one of the pages is updated, or when the bp-pages
option is changed.

Note that this change will likely be moot after migrating to the WP rewrite
API. See #4954.

See #5362

Props Denis-de-Bernardy

File:
1 edited

Legend:

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

    r7800 r7809  
    358358    global $wpdb;
    359359
    360     // Set pages as standard class
    361     $pages = new stdClass;
    362 
    363     // Get pages and IDs
    364     $page_ids = bp_core_get_directory_page_ids();
    365     if ( !empty( $page_ids ) ) {
    366 
    367         // Always get page data from the root blog, except on multiblog mode, when it comes
    368         // from the current blog
    369         $posts_table_name = bp_is_multiblog_mode() ? $wpdb->posts : $wpdb->get_blog_prefix( bp_get_root_blog_id() ) . 'posts';
    370         $page_ids_sql     = implode( ',', wp_parse_id_list( $page_ids ) );
    371         $page_names       = $wpdb->get_results( "SELECT ID, post_name, post_parent, post_title FROM {$posts_table_name} WHERE ID IN ({$page_ids_sql}) AND post_status = 'publish' " );
    372 
    373         foreach ( (array) $page_ids as $component_id => $page_id ) {
    374             foreach ( (array) $page_names as $page_name ) {
    375                 if ( $page_name->ID == $page_id ) {
    376                     if ( !isset( $pages->{$component_id} ) || !is_object( $pages->{$component_id} ) ) {
    377                         $pages->{$component_id} = new stdClass;
     360    // Look in cache first
     361    $pages = wp_cache_get( 'directory_pages', 'bp' );
     362
     363    if ( false === $pages ) {
     364
     365        // Set pages as standard class
     366        $pages = new stdClass;
     367
     368        // Get pages and IDs
     369        $page_ids = bp_core_get_directory_page_ids();
     370        if ( !empty( $page_ids ) ) {
     371
     372            // Always get page data from the root blog, except on multiblog mode, when it comes
     373            // from the current blog
     374            $posts_table_name = bp_is_multiblog_mode() ? $wpdb->posts : $wpdb->get_blog_prefix( bp_get_root_blog_id() ) . 'posts';
     375            $page_ids_sql     = implode( ',', wp_parse_id_list( $page_ids ) );
     376            $page_names       = $wpdb->get_results( "SELECT ID, post_name, post_parent, post_title FROM {$posts_table_name} WHERE ID IN ({$page_ids_sql}) AND post_status = 'publish' " );
     377
     378            foreach ( (array) $page_ids as $component_id => $page_id ) {
     379                foreach ( (array) $page_names as $page_name ) {
     380                    if ( $page_name->ID == $page_id ) {
     381                        if ( !isset( $pages->{$component_id} ) || !is_object( $pages->{$component_id} ) ) {
     382                            $pages->{$component_id} = new stdClass;
     383                        }
     384
     385                        $pages->{$component_id}->name  = $page_name->post_name;
     386                        $pages->{$component_id}->id    = $page_name->ID;
     387                        $pages->{$component_id}->title = $page_name->post_title;
     388                        $slug[]                        = $page_name->post_name;
     389
     390                        // Get the slug
     391                        while ( $page_name->post_parent != 0 ) {
     392                            $parent                 = $wpdb->get_results( $wpdb->prepare( "SELECT post_name, post_parent FROM {$posts_table_name} WHERE ID = %d", $page_name->post_parent ) );
     393                            $slug[]                 = $parent[0]->post_name;
     394                            $page_name->post_parent = $parent[0]->post_parent;
     395                        }
     396
     397                        $pages->{$component_id}->slug = implode( '/', array_reverse( (array) $slug ) );
    378398                    }
    379399
    380                     $pages->{$component_id}->name  = $page_name->post_name;
    381                     $pages->{$component_id}->id    = $page_name->ID;
    382                     $pages->{$component_id}->title = $page_name->post_title;
    383                     $slug[]                        = $page_name->post_name;
    384 
    385                     // Get the slug
    386                     while ( $page_name->post_parent != 0 ) {
    387                         $parent                 = $wpdb->get_results( $wpdb->prepare( "SELECT post_name, post_parent FROM {$posts_table_name} WHERE ID = %d", $page_name->post_parent ) );
    388                         $slug[]                 = $parent[0]->post_name;
    389                         $page_name->post_parent = $parent[0]->post_parent;
    390                     }
    391 
    392                     $pages->{$component_id}->slug = implode( '/', array_reverse( (array) $slug ) );
     400                    unset( $slug );
    393401                }
    394 
    395                 unset( $slug );
    396402            }
    397403        }
     404
     405        wp_cache_set( 'directory_pages', $pages, 'bp' );
    398406    }
    399407
Note: See TracChangeset for help on using the changeset viewer.