Changeset 7786
- Timestamp:
- 02/05/2014 02:15:53 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-core/bp-core-cache.php
r7434 r7786 56 56 add_action( 'bp_first_activity_for_member', 'bp_core_clear_member_count_caches' ); 57 57 add_action( 'deleted_user', 'bp_core_clear_member_count_caches' ); 58 59 /** 60 * Determine which items from a list do not have cached values. 61 * 62 * @since BuddyPress (2.0.0) 63 * 64 * @param array $item_ids ID list. 65 * @param string $cache_group The cache group to check against. 66 * @return array 67 */ 68 function bp_get_non_cached_ids( $item_ids, $cache_group ) { 69 $uncached = array(); 70 71 foreach ( $item_ids as $item_id ) { 72 $item_id = (int) $item_id; 73 if ( false === wp_cache_get( $item_id, $cache_group ) ) { 74 $uncached[] = $item_id; 75 } 76 } 77 78 return $uncached; 79 } 58 80 59 81 /** -
trunk/bp-xprofile/bp-xprofile-cache.php
r7782 r7786 40 40 add_action( 'xprofile_fields_deleted_field', 'xprofile_clear_profile_field_object_cache' ); 41 41 42 /** 43 * Clear caches when a user's updates a field data object. 44 * 45 * @since BuddyPress (2.0.0) 46 * 47 * @param BP_XProfile_ProfileData 48 */ 49 function xprofile_clear_profiledata_object_cache( $data_obj ) { 50 wp_cache_delete( $data_obj->field_id, 'bp_xprofile_data_' . $data_obj->user_id ); 51 } 52 add_action( 'xprofile_data_after_save', 'xprofile_clear_profiledata_object_cache' ); 53 add_action( 'xprofile_data_after_delete', 'xprofile_clear_profiledata_object_cache' ); 54 42 55 // List actions to clear super cached pages on, if super cache is installed 43 56 add_action( 'xprofile_updated_profile', 'bp_core_clear_cache' ); -
trunk/bp-xprofile/bp-xprofile-classes.php
r7781 r7786 194 194 195 195 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 ); 197 197 } 198 198 … … 1095 1095 global $wpdb, $bp; 1096 1096 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 } 1109 1116 } 1110 1117 } … … 1196 1203 1197 1204 /** 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 } 1198 1268 1199 1269 /**
Note: See TracChangeset
for help on using the changeset viewer.