Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/06/2014 01:39:48 AM (11 years ago)
Author:
boonebgorges
Message:

Implement persistent caching in BP_XProfile_ProfileData::get_value_byid()

This change should mean significant performance improvements, as the method
is used a number of places throughout BuddyPress:

  • bp_get_the_profile_field_options() (to check/select the saved values)
  • xprofile_get_field_data()
  • when swapping out blog commenter names with BP display names

Introduces unit tests for the method.

See #1332

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-xprofile/bp-xprofile-classes.php

    r7805 r7806  
    13341334    }
    13351335
     1336    /**
     1337     * Get profile field values by field ID and user IDs.
     1338     *
     1339     * Supports multiple user IDs.
     1340     *
     1341     * @param int $field_id ID of the field.
     1342     * @param int|array $user_ids ID or IDs of user(s).
     1343     * @return string|array Single value if a single user is queried,
     1344     *         otherwise an array of results.
     1345     */
    13361346    public static function get_value_byid( $field_id, $user_ids = null ) {
    13371347        global $wpdb, $bp;
    13381348
    1339         if ( empty( $user_ids ) )
     1349        if ( empty( $user_ids ) ) {
    13401350            $user_ids = bp_displayed_user_id();
    1341 
    1342         if ( is_array( $user_ids ) ) {
    1343             $user_ids = implode( ',', wp_parse_id_list( $user_ids ) );
    1344             $data = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, value FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id IN ({$user_ids})", $field_id ) );
    1345         } else {
    1346             $data = $wpdb->get_var( $wpdb->prepare( "SELECT value FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_ids ) );
    1347         }
    1348 
    1349         return $data;
     1351        }
     1352
     1353        $is_single = false;
     1354        if ( ! is_array( $user_ids ) ) {
     1355            $user_ids  = array( $user_ids );
     1356            $is_single = true;
     1357        }
     1358
     1359        // Assemble uncached IDs
     1360        $uncached_ids = array();
     1361        foreach ( $user_ids as $user_id ) {
     1362            if ( false === wp_cache_get( $field_id, 'bp_xprofile_data_' . $user_id ) ) {
     1363                $uncached_ids[] = $user_id;
     1364            }
     1365        }
     1366
     1367        // Prime caches
     1368        if ( ! empty( $uncached_ids ) ) {
     1369            $uncached_ids_sql = implode( ',', $uncached_ids );
     1370            $queried_data = $wpdb->get_results( $wpdb->prepare( "SELECT id, user_id, field_id, value, last_updated FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id IN ({$uncached_ids_sql})", $field_id ) );
     1371
     1372            foreach ( $queried_data as $d ) {
     1373                wp_cache_set( $field_id, $d, 'bp_xprofile_data_' . $d->user_id );
     1374            }
     1375        }
     1376
     1377        // Now that the cache is primed with all data, fetch it
     1378        $data = array();
     1379        foreach ( $user_ids as $user_id ) {
     1380            $data[] = wp_cache_get( $field_id, 'bp_xprofile_data_' . $user_id );
     1381        }
     1382
     1383        // If a single ID was passed, just return the value
     1384        if ( $is_single ) {
     1385            return $data[0]->value;
     1386
     1387        // Otherwise return the whole array
     1388        } else {
     1389            return $data;
     1390        }
    13501391    }
    13511392
Note: See TracChangeset for help on using the changeset viewer.