Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/07/2015 02:30:31 PM (9 years ago)
Author:
boonebgorges
Message:

Introduce cache support for xprofile fields.

xprofile_get_field() will now look in the cache before returning a
BP_XProfile_Field object (via the new BP_XProfile_Field::get_instance()),
and will populate the cache if necessary. It's strongly recommended to use
xprofile_get_field() when fetching individual existing fields, instead of
creating new BP_XProfile_Field objects directly.

BP_XProfile_Group::get() now leverages the field cache too. Instead of a
SELECT * query, it fetches a list of matching field IDs SELECT id, and
populates the full field objects from the cache, if available. The fields
property of BP_XProfile_Group objects is now an array of BP_XProfile_Field
objects, not stdClass. See #6358.

Props boonebgorges, r-a-y.
See #6638.

File:
1 edited

Legend:

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

    r10176 r10198  
    370370
    371371        // Fetch the fields.
    372         $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_in} ) AND parent_id = 0 {$exclude_fields_sql} {$in_sql} ORDER BY field_order" );
    373 
    374         $field_ids = wp_list_pluck( $fields, 'id' );
     372        $field_ids = $wpdb->get_col( "SELECT id FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids_in} ) AND parent_id = 0 {$exclude_fields_sql} {$in_sql} ORDER BY field_order" );
     373
     374        // Bail if no fields.
     375        if ( empty( $field_ids ) ) {
     376            return $groups;
     377        }
     378
     379        $field_ids = array_map( 'intval', $field_ids );
     380
     381        // Prime the field cache.
     382        $uncached_field_ids = bp_get_non_cached_ids( $field_ids, 'bp_xprofile_fields' );
     383        if ( ! empty( $uncached_field_ids ) ) {
     384            $_uncached_field_ids = implode( ',', array_map( 'intval', $uncached_field_ids ) );
     385            $uncached_fields = $wpdb->get_results( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id IN ({$_uncached_field_ids})" );
     386            foreach ( $uncached_fields as $uncached_field ) {
     387                $fid = intval( $uncached_field->id );
     388                wp_cache_set( $fid, $uncached_field, 'bp_xprofile_fields' );
     389            }
     390        }
     391
     392        // Pull field objects from the cache.
     393        $fields = array();
     394        foreach ( $field_ids as $field_id ) {
     395            $fields[] = xprofile_get_field( $field_id );
     396        }
    375397
    376398        // Store field IDs for meta cache priming.
    377399        $object_ids['field'] = $field_ids;
    378 
    379         // Bail if no fields.
    380         if ( empty( $fields ) ) {
    381             return $groups;
    382         }
    383400
    384401        // Maybe fetch field data.
Note: See TracChangeset for help on using the changeset viewer.