Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/23/2017 05:32:04 PM (9 years 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.

File:
1 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 );
Note: See TracChangeset for help on using the changeset viewer.