Skip to:
Content

Changeset 11611


Ignore:
Timestamp:
06/23/17 17:32:04 (7 months ago)
Author:
r-a-y
Message:

Members: Cache invalid users in bp_core_get_core_userdata().

This commit caches non-existent user attempts for the
bp_core_get_core_userdata() function, so subsequent calls for the same
user ID will not ping the database again.

See #7523.

Location:
trunk
Files:
2 edited

Legend:

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

    r11447 r11611  
    207207 * @since 1.2.0 
    208208 * 
    209  * @param int $user_id The ID of the user. 
    210  * @return array 
     209 * @param  int $user_id The ID of the user. 
     210 * @return array|bool Array of data on success, boolean false on failure. 
    211211 */ 
    212212function bp_core_get_core_userdata( $user_id = 0 ) { 
     
    215215    } 
    216216 
    217     if ( !$userdata = wp_cache_get( 'bp_core_userdata_' . $user_id, 'bp' ) ) { 
     217    $userdata = wp_cache_get( 'bp_core_userdata_' . $user_id, 'bp' ); 
     218 
     219    // No cache. 
     220    if ( false === $userdata ) { 
    218221        $userdata = BP_Core_User::get_core_userdata( $user_id ); 
    219         wp_cache_set( 'bp_core_userdata_' . $user_id, $userdata, 'bp' ); 
     222 
     223        // Cache data; no-result is cached as integer 0. 
     224        wp_cache_set( 'bp_core_userdata_' . $user_id, false === $userdata ? 0 : $userdata, 'bp' ); 
     225 
     226    // Cached no-result, so set return value as false as expected. 
     227    } elseif ( 0 === $userdata ) { 
     228        $userdata = false; 
    220229    } 
    221230 
     
    225234     * @since 1.2.0 
    226235     * 
    227      * @param array $userdata Array of user data for a passed user. 
     236     * @param array|bool $userdata Array of user data for a passed user on success, boolean false on failure. 
    228237     */ 
    229238    return apply_filters( 'bp_core_get_core_userdata', $userdata ); 
  • trunk/tests/phpunit/testcases/members/cache.php

    r11101 r11611  
    7979        $this->assertSame( $num_queries, $wpdb->num_queries ); 
    8080    } 
     81 
     82    /** 
     83     * @group bp_core_get_core_userdata 
     84     */ 
     85    public function test_bp_core_get_core_userdata_should_cache_no_results() { 
     86        // Get the userdata for a user that doesn't exist. 
     87        bp_core_get_core_userdata( PHP_INT_MAX ); 
     88 
     89        // Assert that the non-existent user's attempt was cached. 
     90        $this->assertNotFalse( wp_cache_get( 'bp_core_userdata_' . PHP_INT_MAX, 'bp' ) ); 
     91    } 
    8192} 
    8293 
Note: See TracChangeset for help on using the changeset viewer.