Skip to:
Content

BuddyPress.org

Changeset 7914


Ignore:
Timestamp:
02/17/2014 07:42:03 PM (13 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

Location:
trunk
Files:
1 added
3 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 ) {
  • trunk/bp-xprofile/bp-xprofile-classes.php

    r7820 r7914  
    118118         *              'exclude_groups' - Comma-separated list of groups to exclude
    119119         *              'exclude_fields' - Comma-separated list of fields to exclude
     120         *              'update_meta_cache' - Whether to pre-fetch xprofilemeta
     121         *                 for all retrieved groups, fields, and data
    120122         *
    121123         * @return array $groups
     
    133135                        'fetch_visibility_level' => false,
    134136                        'exclude_groups'         => false,
    135                         'exclude_fields'         => false
     137                        'exclude_fields'         => false,
     138                        'update_meta_cache'      => true,
    136139                );
    137140
    138141                $r = wp_parse_args( $args, $defaults );
    139142                extract( $r, EXTR_SKIP );
     143
     144                // Keep track of object IDs for cache-priming
     145                $object_ids = array(
     146                        'group' => array(),
     147                        'field' => array(),
     148                        'data'  => array(),
     149                );
     150
    140151                $where_sql = '';
    141152
     
    162173                }
    163174
     175                // Store for meta cache priming
     176                $object_ids['group'] = $group_ids;
     177
    164178                $group_ids = implode( ',', (array) $group_ids );
    165179
     
    183197                // Fetch the fields
    184198                $fields = $wpdb->get_results( "SELECT id, name, description, type, group_id, is_required FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids} ) AND parent_id = 0 {$exclude_fields_sql} ORDER BY field_order" );
     199
     200                // Store field IDs for meta cache priming
     201                $object_ids['field'] = wp_list_pluck( $fields, 'id' );
    185202
    186203                if ( empty( $fields ) )
     
    241258                                                        $fields[$field_key]->data->id    = $data->id;
    242259                                                }
     260
     261                                                // Store for meta cache priming
     262                                                $object_ids['data'][] = $data->id;
    243263                                        }
    244264                                }
     
    272292                        // Reset indexes
    273293                        $groups = array_values( $groups );
     294                }
     295
     296                // Prime the meta cache, if necessary
     297                if ( $update_meta_cache ) {
     298                        bp_xprofile_update_meta_cache( $object_ids );
    274299                }
    275300
  • trunk/bp-xprofile/bp-xprofile-template.php

    r7888 r7914  
    2525        var $user_id;
    2626
    27         function __construct( $user_id, $profile_group_id, $hide_empty_groups = false, $fetch_fields = false, $fetch_field_data = false, $exclude_groups = false, $exclude_fields = false, $hide_empty_fields = false, $fetch_visibility_level = false ) {
     27        function __construct( $user_id, $profile_group_id, $hide_empty_groups = false, $fetch_fields = false, $fetch_field_data = false, $exclude_groups = false, $exclude_fields = false, $hide_empty_fields = false, $fetch_visibility_level = false, $update_meta_cache = true ) {
    2828                $this->groups = BP_XProfile_Group::get( array(
    2929                        'profile_group_id'    => $profile_group_id,
     
    3535                        'fetch_visibility_level' => $fetch_visibility_level,
    3636                        'exclude_groups'      => $exclude_groups,
    37                         'exclude_fields'      => $exclude_fields
     37                        'exclude_fields'      => $exclude_fields,
     38                        'update_meta_cache'   => $update_meta_cache,
    3839                ) );
    3940
     
    175176                'fetch_visibility_level' => $fetch_visibility_level_default,
    176177                'exclude_groups'      => false, // Comma-separated list of profile field group IDs to exclude
    177                 'exclude_fields'      => false  // Comma-separated list of profile field IDs to exclude
     178                'exclude_fields'      => false,  // Comma-separated list of profile field IDs to exclude
     179                'update_meta_cache'   => true,
    178180        );
    179181
     
    181183        extract( $r, EXTR_SKIP );
    182184
    183         $profile_template = new BP_XProfile_Data_Template( $user_id, $profile_group_id, $hide_empty_groups, $fetch_fields, $fetch_field_data, $exclude_groups, $exclude_fields, $hide_empty_fields, $fetch_visibility_level );
     185        $profile_template = new BP_XProfile_Data_Template( $user_id, $profile_group_id, $hide_empty_groups, $fetch_fields, $fetch_field_data, $exclude_groups, $exclude_fields, $hide_empty_fields, $fetch_visibility_level, $update_meta_cache );
    184186        return apply_filters( 'bp_has_profile', $profile_template->has_groups(), $profile_template );
    185187}
Note: See TracChangeset for help on using the changeset viewer.