Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/05/2014 02:15:53 PM (13 years ago)
Author:
boonebgorges
Message:

Add persistent caching support for xprofile field data

See #1332

File:
1 edited

Legend:

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

    r7781 r7786  
    194194
    195195                        if ( ! empty( $field_ids ) && ! empty( $user_id ) ) {
    196                                 $field_data = $wpdb->get_results( $wpdb->prepare( "SELECT id, field_id, value FROM {$bp->profile->table_name_data} WHERE field_id IN ( {$field_ids_sql} ) AND user_id = %d", $user_id ) );
     196                                $field_data = BP_XProfile_ProfileData::get_data_for_user( $user_id, $field_ids );
    197197                        }
    198198
     
    10951095                global $wpdb, $bp;
    10961096
    1097                 $sql = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_id );
    1098 
    1099                 if ( $profiledata = $wpdb->get_row( $sql ) ) {
    1100                         $this->id           = $profiledata->id;
    1101                         $this->user_id      = $profiledata->user_id;
    1102                         $this->field_id     = $profiledata->field_id;
    1103                         $this->value        = stripslashes( $profiledata->value );
    1104                         $this->last_updated = $profiledata->last_updated;
    1105                 } else {
    1106                         // When no row is found, we'll need to set these properties manually
    1107                         $this->field_id     = $field_id;
    1108                         $this->user_id      = $user_id;
     1097                $cache_group = 'bp_xprofile_data_' . $user_id;
     1098                $profiledata = wp_cache_get( $field_id, $cache_group );
     1099
     1100                if ( false === $profiledata ) {
     1101                        $sql = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_id );
     1102
     1103                        if ( $profiledata = $wpdb->get_row( $sql ) ) {
     1104                                $this->id           = $profiledata->id;
     1105                                $this->user_id      = $profiledata->user_id;
     1106                                $this->field_id     = $profiledata->field_id;
     1107                                $this->value        = stripslashes( $profiledata->value );
     1108                                $this->last_updated = $profiledata->last_updated;
     1109
     1110                                wp_cache_set( $field_id, $profiledata, $cache_group );
     1111                        } else {
     1112                                // When no row is found, we'll need to set these properties manually
     1113                                $this->field_id     = $field_id;
     1114                                $this->user_id      = $user_id;
     1115                        }
    11091116                }
    11101117        }
     
    11961203
    11971204        /** Static Methods ********************************************************/
     1205
     1206        /**
     1207         * Get a user's profile data for a set of fields.
     1208         *
     1209         * @param int $user_id
     1210         * @param array $field_ids
     1211         * @return array
     1212         */
     1213        public static function get_data_for_user( $user_id, $field_ids ) {
     1214                global $wpdb;
     1215
     1216                $data = array();
     1217
     1218                $cache_group = 'bp_xprofile_data_' . $user_id;
     1219
     1220                $uncached_field_ids = bp_get_non_cached_ids( $field_ids, $cache_group );
     1221
     1222                // Prime the cache
     1223                if ( ! empty( $uncached_field_ids ) ) {
     1224                        $bp = buddypress();
     1225                        $uncached_field_ids_sql = implode( ',', wp_parse_id_list( $uncached_field_ids ) );
     1226                        $uncached_data = $wpdb->get_results( $wpdb->prepare( "SELECT id, user_id, field_id, value, last_updated FROM {$bp->profile->table_name_data} WHERE field_id IN ({$uncached_field_ids_sql}) AND user_id = %d", $user_id ) );
     1227
     1228                        // Rekey
     1229                        $queried_data = array();
     1230                        foreach ( $uncached_data as $ud ) {
     1231                                $d               = new stdClass;
     1232                                $d->id           = $ud->id;
     1233                                $d->user_id      = $ud->user_id;
     1234                                $d->field_id     = $ud->field_id;
     1235                                $d->value        = $ud->value;
     1236                                $d->last_updated = $ud->last_updated;
     1237
     1238                                $queried_data[ $ud->field_id ] = $d;
     1239                        }
     1240
     1241                        // Set caches
     1242                        foreach ( $field_ids as $field_id ) {
     1243
     1244                                // If a value was found, cache it
     1245                                if ( isset( $queried_data[ $field_id ] ) ) {
     1246                                        wp_cache_set( $field_id, $queried_data[ $field_id ], $cache_group );
     1247
     1248                                // If no value was found, cache an empty item
     1249                                // to avoid future cache misses
     1250                                } else {
     1251                                        $d           = new stdClass;
     1252                                        $d->id       = '';
     1253                                        $d->field_id = $field_id;
     1254                                        $d->value    = '';
     1255
     1256                                        wp_cache_set( $field_id, $d, $cache_group );
     1257                                }
     1258                        }
     1259                }
     1260
     1261                // Now that all items are cached, fetch them
     1262                foreach ( $field_ids as $field_id ) {
     1263                        $data[] = wp_cache_get( $field_id, $cache_group );
     1264                }
     1265
     1266                return $data;
     1267        }
    11981268
    11991269        /**
Note: See TracChangeset for help on using the changeset viewer.