Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/17/2014 07:42:03 PM (8 years ago)
Author:
boonebgorges
Message:

Introduce metadata pre-fetching for xprofile objects

XProfile objects (groups, fields, and data) are generally fetched in a nested
way, as within the context of a bp_has_profile() loop. Thus, it makes sense to
pre-fetch xprofile metadata within a bp_has_profile() loop in a nested way as
well. This changeset introduces bp_xprofile_update_meta_cache(), which is able
to fetch all uncached metadata for nested meta types with a single database
query.

This changeset also introduces an 'update_meta_cache' parameter to the
bp_has_profile() stack. This param allows developers to disable the pre-
fetching introduced here. Defaults to true.

See #5398

File:
1 edited

Legend:

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

    r7812 r7914  
    22
    33/**
    4  * BuddyPress XProfile Template Tags
     4 * BuddyPress XProfile Caching Functions
    55 *
    66 * Caching functions handle the clearing of cached objects and pages on specific
     
    88 *
    99 * @package BuddyPress
    10  * @subpackage XProfileTemplate
    1110 */
    1211
    1312// Exit if accessed directly
    1413if ( !defined( 'ABSPATH' ) ) exit;
     14
     15/**
     16 * Slurp up xprofilemeta for a specified set of profile objects.
     17 *
     18 * We do not use bp_update_meta_cache() for the xprofile component. This is
     19 * because the xprofile component has three separate object types (group,
     20 * field, and data) and three corresponding cache groups. Using the technique
     21 * in bp_update_meta_cache(), pre-fetching would take three separate database
     22 * queries. By grouping them together, we can reduce the required queries to
     23 * one.
     24 *
     25 * This function is called within a bp_has_profile() loop.
     26 *
     27 * @since BuddyPress (2.0.0)
     28 *
     29 * @param array $object_ids Multi-dimensional array of object_ids, keyed by
     30 *        object type ('group', 'field', 'data')
     31 */
     32function bp_xprofile_update_meta_cache( $object_ids = array(), $user_id = 0 ) {
     33    global $wpdb;
     34
     35    if ( empty( $object_ids ) ) {
     36        return false;
     37    }
     38
     39    // $object_ids is a multi-dimensional array
     40    $uncached_object_ids = array(
     41        'group' => array(),
     42        'field' => array(),
     43        'data'   => array(),
     44    );
     45
     46    $cache_groups = array(
     47        'group' => 'xprofile_group_meta',
     48        'field' => 'xprofile_field_meta',
     49        'data'  => 'xprofile_data_meta',
     50    );
     51
     52    $do_query = false;
     53    foreach ( $uncached_object_ids as $object_type => $uncached_object_type_ids ) {
     54        if ( ! empty( $object_ids[ $object_type ] ) ) {
     55            // Sanitize $object_ids passed to the function
     56            $object_type_ids = wp_parse_id_list( $object_ids[ $object_type ] );
     57
     58            // Get non-cached IDs for each object type
     59            $uncached_object_ids[ $object_type ] = bp_get_non_cached_ids( $object_type_ids, $cache_groups[ $object_type ] );
     60
     61            // Set the flag to do the meta query
     62            if ( ! empty( $uncached_object_ids[ $object_type ] ) && ! $do_query ) {
     63                $do_query = true;
     64            }
     65        }
     66    }
     67
     68    // If there are uncached items, go ahead with the query
     69    if ( $do_query ) {
     70        $where = array();
     71        foreach ( $uncached_object_ids as $otype => $oids ) {
     72            if ( empty( $oids ) ) {
     73                continue;
     74            }
     75
     76            $oids_sql = implode( ',', wp_parse_id_list( $oids ) );
     77            $where[]  = $wpdb->prepare( "( object_type = %s AND object_id IN ({$oids_sql}) )", $otype );
     78        }
     79        $where_sql = implode( " OR ", $where );
     80    }
     81
     82
     83    $bp = buddypress();
     84    $meta_list = $wpdb->get_results( "SELECT object_id, object_type, meta_key, meta_value FROM {$bp->profile->table_name_meta} WHERE {$where_sql}" );
     85
     86    if ( ! empty( $meta_list ) ) {
     87        $object_type_caches = array(
     88            'group' => array(),
     89            'field' => array(),
     90            'data'  => array(),
     91        );
     92
     93        foreach ( $meta_list as $meta ) {
     94            $oid    = $meta->object_id;
     95            $otype  = $meta->object_type;
     96            $okey   = $meta->meta_key;
     97            $ovalue = $meta->meta_value;
     98
     99            // Force subkeys to be array type
     100            if ( ! isset( $cache[ $otype ][ $oid ] ) || ! is_array( $cache[ $otype ][ $oid ] ) ) {
     101                $cache[ $otype ][ $oid ] = array();
     102            }
     103
     104            if ( ! isset( $cache[ $otype ][ $oid ][ $okey ] ) || ! is_array( $cache[ $otype ][ $oid ][ $okey ] ) ) {
     105                $cache[ $otype ][ $oid ][ $okey ] = array();
     106            }
     107
     108            // Add to the cache array
     109            $cache[ $otype ][ $oid ][ $okey ][] = maybe_unserialize( $ovalue );
     110        }
     111
     112        foreach ( $cache as $object_type => $object_caches ) {
     113            $cache_group = $cache_groups[ $object_type ];
     114            foreach ( $object_caches as $object_id => $object_cache ) {
     115                wp_cache_set( $object_id, $object_cache, $cache_group );
     116            }
     117        }
     118    }
     119
     120    return;
     121}
    15122
    16123function xprofile_clear_profile_groups_object_cache( $group_obj ) {
Note: See TracChangeset for help on using the changeset viewer.