Skip to:
Content

bbPress.org

Changeset 6698


Ignore:
Timestamp:
09/13/2017 09:33:06 PM (4 years ago)
Author:
johnjamesjacoby
Message:

Common: prime last-active post caches.

This change introduces a performance tweak to bbp_has_topics(), bbp_has_forums(), and bbp_has_search(), pre-cache'ing the last-active post objects for what is known to be their imminent usage in the current template loops.

See #3163.

Location:
trunk/src/includes
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/common/functions.php

    r6643 r6698  
    16531653}
    16541654
     1655/**
     1656 * Prime last active post caches
     1657 *
     1658 * This function uses _prime_post_caches() to prepare the object cache for
     1659 * imminent requests to post objects that aren't naturally cached by the primary
     1660 * WP_Query calls themselves.
     1661 *
     1662 * This is triggered when a `prime_last_active_cache` argument is set to true.
     1663 *
     1664 * @since 2.6.0 bbPress (r6696)
     1665 *
     1666 * @param array $objects Array of objects, fresh from a query
     1667 *
     1668 * @return bool True if some IDs were cached
     1669 */
     1670function bbp_prime_last_active_post_caches( $objects = array() ) {
     1671
     1672    // Bail if no posts
     1673    if ( empty( $objects ) ) {
     1674        return false;
     1675    }
     1676
     1677    // Default value
     1678    $prime_last_active_ids = array();
     1679
     1680    // Filter the types of IDs to prime
     1681    $ids = apply_filters( 'bbp_prime_last_active_post_caches', array(
     1682        '_bbp_last_active_id',
     1683        '_bbp_last_reply_id',
     1684        '_bbp_last_topic_id'
     1685    ), $objects );
     1686
     1687    // Get the last active IDs
     1688    foreach ( $objects as $object ) {
     1689        foreach ( $ids as $key ) {
     1690            $prime_last_active_ids[] = get_post_meta( $object->ID, $key, true );
     1691        }
     1692    }
     1693
     1694    // Unique, non-zero values
     1695    $prime_last_active_ids = bbp_get_unique_array_values( $prime_last_active_ids );
     1696
     1697    // Bail if no active IDs to prime
     1698    if ( empty( $prime_last_active_ids ) ) {
     1699        return false;
     1700    }
     1701
     1702    // Try to prime post caches
     1703    _prime_post_caches( $prime_last_active_ids, true, true );
     1704
     1705    // Return
     1706    return true;
     1707}
     1708
    16551709/** Globals *******************************************************************/
    16561710
  • trunk/src/includes/forums/template.php

    r6692 r6698  
    129129    }
    130130
    131     // Parse arguments with default forum query for most circumstances
    132     $bbp_f = bbp_parse_args( $args, array(
     131    // Default argument array
     132    $default = array(
    133133        'post_type'           => bbp_get_forum_post_type(),
    134134        'post_parent'         => $default_post_parent,
     
    138138        'order'               => 'ASC',
    139139        'no_found_rows'       => true,
    140         'ignore_sticky_posts' => true
    141     ), 'has_forums' );
     140        'ignore_sticky_posts' => true,
     141
     142        // Conditionally prime the cache for last active posts
     143        'prime_last_active_cache' => true
     144    );
     145
     146    // Parse arguments with default forum query for most circumstances
     147    $r = bbp_parse_args( $args, $default, 'has_forums' );
    142148
    143149    // Run the query
    144150    $bbp              = bbpress();
    145     $bbp->forum_query = new WP_Query( $bbp_f );
     151    $bbp->forum_query = new WP_Query( $r );
     152
     153    // Maybe prime last active posts
     154    if ( ! empty( $r['prime_last_active_cache'] ) ) {
     155        bbp_prime_last_active_post_caches( $bbp->forum_query->posts );
     156    }
    146157
    147158    // Filter & return
  • trunk/src/includes/search/template.php

    r6680 r6698  
    3535        'orderby'             => 'date',                     // Sorted by date
    3636        'order'               => 'DESC',                     // Most recent first
    37         'ignore_sticky_posts' => true                        // Stickies not supported
     37        'ignore_sticky_posts' => true,                       // Stickies not supported,
     38
     39        // Conditionally prime the cache for last active posts
     40        'prime_last_active_cache' => true
    3841    );
    3942
     
    7477    if ( ! empty( $r['s'] ) ) {
    7578        $bbp->search_query = new WP_Query( $r );
     79    }
     80
     81    // Maybe prime last active posts
     82    if ( ! empty( $r['prime_last_active_cache'] ) ) {
     83        bbp_prime_last_active_post_caches( $bbp->search_query->posts );
    7684    }
    7785
  • trunk/src/includes/topics/template.php

    r6692 r6698  
    152152        'show_stickies'  => $default_show_stickies,    // Ignore sticky topics?
    153153        'max_num_pages'  => false,                     // Maximum number of pages to show
     154
     155        // Conditionally prime the cache for last active posts
     156        'prime_last_active_cache' => true
    154157    );
    155158
     
    195198    // Call the query
    196199    $bbp->topic_query = new WP_Query( $r );
     200
     201    // Maybe prime last active posts
     202    if ( ! empty( $r['prime_last_active_cache'] ) ) {
     203        bbp_prime_last_active_post_caches( $bbp->topic_query->posts );
     204    }
    197205
    198206    // Set post_parent back to 0 if originally set to 'any'
Note: See TracChangeset for help on using the changeset viewer.