Skip to:
Content

BuddyPress.org

Changeset 9231


Ignore:
Timestamp:
12/15/2014 08:38:11 PM (10 years ago)
Author:
r-a-y
Message:

Check cache for count functions that return zero.

Some of our count functions were previously setting counts of zero
correctly, but the functions themselves were not referencing the cache
properly when the count returned zero. This led to unnecessary database
queries and we hate extra queries!

This commit addresses the problem and adds unit tests.

Props r-a-y, boonebgorges.

Fixes #6012.

Location:
trunk
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-blogs/bp-blogs-functions.php

    r9218 r9231  
    11451145 */
    11461146function bp_blogs_total_blogs() {
    1147     if ( !$count = wp_cache_get( 'bp_total_blogs', 'bp' ) ) {
     1147    $count = wp_cache_get( 'bp_total_blogs', 'bp' );
     1148
     1149    if ( false === $count ) {
    11481150        $blogs = BP_Blogs_Blog::get_all();
    11491151        $count = $blogs['total'];
  • trunk/src/bp-groups/bp-groups-functions.php

    r9226 r9231  
    645645 */
    646646function groups_get_total_group_count() {
    647     if ( !$count = wp_cache_get( 'bp_total_group_count', 'bp' ) ) {
     647    $count = wp_cache_get( 'bp_total_group_count', 'bp' );
     648
     649    if ( false === $count ) {
    648650        $count = BP_Groups_Group::get_total_group_count();
    649651        wp_cache_set( 'bp_total_group_count', $count, 'bp' );
     
    685687        $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
    686688
    687     if ( !$count = wp_cache_get( 'bp_total_groups_for_user_' . $user_id, 'bp' ) ) {
     689    $count = wp_cache_get( 'bp_total_groups_for_user_' . $user_id, 'bp' );
     690
     691    if ( false === $count ) {
    688692        $count = BP_Groups_Member::total_group_count( $user_id );
    689693        wp_cache_set( 'bp_total_groups_for_user_' . $user_id, $count, 'bp' );
  • trunk/src/bp-members/bp-members-functions.php

    r9210 r9231  
    598598    global $wpdb;
    599599
    600     if ( !$count = wp_cache_get( 'bp_total_member_count', 'bp' ) ) {
     600    $count = wp_cache_get( 'bp_total_member_count', 'bp' );
     601
     602    if ( false === $count ) {
    601603        $status_sql = bp_core_get_status_sql();
    602604        $count = $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->users} WHERE {$status_sql}" );
  • trunk/tests/phpunit/testcases/blogs/cache.php

    r9139 r9231  
    232232        $this->set_current_user( $old_user );
    233233    }
     234
     235    /**
     236     * @group bp_blogs_total_blogs
     237     * @group counts
     238     */
     239    public function test_bp_blogs_total_count_should_respect_cached_value_of_0() {
     240        if ( ! is_multisite() ) {
     241            return;
     242        }
     243
     244        global $wpdb;
     245
     246        // prime cache
     247        // no blogs are created by default, so count is zero
     248        bp_blogs_total_blogs();
     249        $first_query_count = $wpdb->num_queries;
     250
     251        // run function again
     252        bp_blogs_total_blogs();
     253
     254        // check if function references cache or hits the DB by comparing query count
     255        $this->assertEquals( $first_query_count, $wpdb->num_queries );
     256    }
    234257}
  • trunk/tests/phpunit/testcases/groups/cache.php

    r9156 r9231  
    201201        $this->assertEquals( 2, count( groups_get_group_admins( $g ) ) );
    202202    }
     203
     204    /**
     205     * @group groups_get_total_group_count
     206     * @group counts
     207     */
     208    public function test_groups_get_total_group_count_should_respect_cached_value_of_0() {
     209        global $wpdb;
     210
     211        // prime cache
     212        // no groups are created by default, so count is zero
     213        groups_get_total_group_count();
     214        $first_query_count = $wpdb->num_queries;
     215
     216        // run function again
     217        groups_get_total_group_count();
     218
     219        // check if function references cache or hits the DB by comparing query count
     220        $this->assertEquals( $first_query_count, $wpdb->num_queries );
     221    }
    203222}
Note: See TracChangeset for help on using the changeset viewer.