Ticket #6100: 6100.patch
File 6100.patch, 10.4 KB (added by , 10 years ago) |
---|
-
src/bp-xprofile/bp-xprofile-cache.php
13 13 if ( !defined( 'ABSPATH' ) ) exit; 14 14 15 15 /** 16 * Determine which xprofile fields do not have cached values for a user. 17 * 18 * @since BuddyPress (2.2.0) 19 * 20 * @param int $user_id User ID to check 21 * @param array $field_ids XProfile field IDs. 22 * @return array 23 */ 24 function bp_xprofile_get_non_cached_field_ids( $user_id = 0, $field_ids = array() ) { 25 $uncached_fields = array(); 26 27 foreach ( $field_ids as $field_id ) { 28 $field_id = (int) $field_id; 29 $cache_key = "{$user_id}:{$field_id}"; 30 if ( false === wp_cache_get( $cache_key, 'bp_xprofile_data' ) ) { 31 $uncached_fields[] = $field_id; 32 } 33 } 34 35 return $uncached_fields; 36 } 37 38 /** 16 39 * Slurp up xprofilemeta for a specified set of profile objects. 17 40 * 18 41 * We do not use bp_update_meta_cache() for the xprofile component. This is … … 40 63 $uncached_object_ids = array( 41 64 'group' => array(), 42 65 'field' => array(), 43 'data' 66 'data' => array(), 44 67 ); 45 68 46 69 $cache_groups = array( … … 170 193 * @param BP_XProfile_ProfileData $data_obj 171 194 */ 172 195 function xprofile_clear_profiledata_object_cache( $data_obj ) { 173 wp_cache_delete( $data_obj->field_id, 'bp_xprofile_data_' . $data_obj->user_id);196 wp_cache_delete( "{$data_obj->user_id}:{$data_obj->field_id}", 'bp_xprofile_data' ); 174 197 } 175 add_action( 'xprofile_data_after_save', 'xprofile_clear_profiledata_object_cache' );198 add_action( 'xprofile_data_after_save', 'xprofile_clear_profiledata_object_cache' ); 176 199 add_action( 'xprofile_data_after_delete', 'xprofile_clear_profiledata_object_cache' ); 177 200 178 201 /** -
src/bp-xprofile/bp-xprofile-classes.php
1222 1222 public function populate( $field_id, $user_id ) { 1223 1223 global $wpdb, $bp; 1224 1224 1225 $cache_ group = 'bp_xprofile_data_' . $user_id;1226 $profiledata = wp_cache_get( $ field_id, $cache_group);1225 $cache_key = "{$user_id}:{$field_id}"; 1226 $profiledata = wp_cache_get( $cache_key, 'bp_xprofile_data' ); 1227 1227 1228 1228 if ( false === $profiledata ) { 1229 $sql = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_id );1229 $sql = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_id ); 1230 1230 $profiledata = $wpdb->get_row( $sql ); 1231 1231 1232 1232 if ( $profiledata ) { 1233 wp_cache_set( $ field_id, $profiledata, $cache_group);1233 wp_cache_set( $cache_key, $profiledata, 'bp_xprofile_data' ); 1234 1234 } 1235 1235 } 1236 1236 … … 1259 1259 global $wpdb, $bp; 1260 1260 1261 1261 // Check cache first 1262 $cached = wp_cache_get( $this->field_id, 'bp_xprofile_data_' . $this->user_id ); 1262 $cache_key = "{$this->user_id}:{$this->field_id}"; 1263 $cached = wp_cache_get( $cache_key, 'bp_xprofile_data' ); 1263 1264 1264 1265 if ( $cached && ! empty( $cached->id ) ) { 1265 1266 $retval = true; … … 1400 1401 1401 1402 $data = array(); 1402 1403 1403 $ cache_group = 'bp_xprofile_data_' . $user_id;1404 $uncached_field_ids = bp_xprofile_get_non_cached_field_ids( $user_id, $field_ids, 'bp_xprofile_data' ); 1404 1405 1405 $uncached_field_ids = bp_get_non_cached_ids( $field_ids, $cache_group );1406 1407 1406 // Prime the cache 1408 1407 if ( ! empty( $uncached_field_ids ) ) { 1409 1408 $bp = buddypress(); … … 1426 1425 // Set caches 1427 1426 foreach ( $uncached_field_ids as $field_id ) { 1428 1427 1428 $cache_key = "{$user_id}:{$field_id}"; 1429 1429 1430 // If a value was found, cache it 1430 1431 if ( isset( $queried_data[ $field_id ] ) ) { 1431 wp_cache_set( $ field_id, $queried_data[ $field_id ], $cache_group);1432 wp_cache_set( $cache_key, $queried_data[ $field_id ], 'bp_xprofile_data' ); 1432 1433 1433 1434 // If no value was found, cache an empty item 1434 1435 // to avoid future cache misses … … 1440 1441 $d->value = ''; 1441 1442 $d->last_updated = ''; 1442 1443 1443 wp_cache_set( $ field_id, $d, $cache_group);1444 wp_cache_set( $cache_key, $d, 'bp_xprofile_data' ); 1444 1445 } 1445 1446 } 1446 1447 } 1447 1448 1448 1449 // Now that all items are cached, fetch them 1449 1450 foreach ( $field_ids as $field_id ) { 1450 $data[] = wp_cache_get( $field_id, $cache_group ); 1451 $cache_key = "{$user_id}:{$field_id}"; 1452 $data[] = wp_cache_get( $cache_key, 'bp_xprofile_data' ); 1451 1453 } 1452 1454 1453 1455 return $data; … … 1460 1462 * @return array 1461 1463 */ 1462 1464 public static function get_all_for_user( $user_id ) { 1463 global $wpdb, $bp;1464 1465 1465 1466 $groups = bp_xprofile_get_groups( array( 1466 1467 'user_id' => $user_id, … … 1514 1515 } else { 1515 1516 1516 1517 // Check cache first 1517 $fielddata = wp_cache_get( $field_id, 'bp_xprofile_data_' . $user_id ); 1518 $cache_key = "{$user_id}:{$field_id}"; 1519 $fielddata = wp_cache_get( $cache_key, 'bp_xprofile_data' ); 1518 1520 if ( false === $fielddata || empty( $fielddata->id ) ) { 1519 1521 $fielddata_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_id ) ); 1520 1522 } else { … … 1551 1553 // Assemble uncached IDs 1552 1554 $uncached_ids = array(); 1553 1555 foreach ( $user_ids as $user_id ) { 1554 if ( false === wp_cache_get( $field_id, 'bp_xprofile_data_' . $user_id ) ) { 1556 $cache_key = "{$user_id}:{$field_id}"; 1557 if ( false === wp_cache_get( $cache_key, 'bp_xprofile_data' ) ) { 1555 1558 $uncached_ids[] = $user_id; 1556 1559 } 1557 1560 } … … 1583 1586 $d->last_updated = ''; 1584 1587 } 1585 1588 1586 wp_cache_set( $field_id, $d, 'bp_xprofile_data_' . $d->user_id ); 1589 $cache_key = "{$d->user_id}:{$field_id}"; 1590 wp_cache_set( $cache_key, $d, 'bp_xprofile_data' ); 1587 1591 } 1588 1592 } 1589 1593 1590 1594 // Now that the cache is primed with all data, fetch it 1591 1595 $data = array(); 1592 1596 foreach ( $user_ids as $user_id ) { 1593 $data[] = wp_cache_get( $field_id, 'bp_xprofile_data_' . $user_id ); 1597 $cache_key = "{$user_id}:{$field_id}"; 1598 $data[] = wp_cache_get( $cache_key, 'bp_xprofile_data' ); 1594 1599 } 1595 1600 1596 1601 // If a single ID was passed, just return the value -
src/bp-xprofile/bp-xprofile-loader.php
353 353 } 354 354 355 355 /** 356 * Setup cache groups 357 * 358 * @since BuddyPress (2.2.0) 359 */ 360 public function setup_cache_groups() { 361 362 // Global groups 363 wp_cache_add_global_groups( array( 364 'bp_xprofile', 365 'bp_xprofile_data', 366 //'xprofile_meta' 367 ) ); 368 369 parent::setup_cache_groups(); 370 } 371 372 /** 356 373 * Adds "Settings > Profile" subnav item under the "Settings" adminbar menu. 357 374 * 358 375 * @since BuddyPress (2.0.0) -
tests/phpunit/testcases/members/functions.php
183 183 global $wpdb, $bp; 184 184 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE user_id = %d AND field_id = 1", $u ) ); 185 185 wp_cache_delete( 'bp_user_fullname_' . $u, 'bp' ); 186 wp_cache_delete( 1, 'bp_xprofile_data_' . $u, 'bp' );186 wp_cache_delete( "{$u}:1", 'bp_xprofile_data', 'bp' ); 187 187 188 188 $this->assertSame( '', xprofile_get_field_data( 1, $u ) ); 189 189 $this->assertSame( 'Foo Foo', bp_core_get_user_displayname( $u ) ); -
tests/phpunit/testcases/xprofile/class-bp-xprofile-profiledata.php
36 36 37 37 $d = new BP_XProfile_ProfileData( $f, $u ); 38 38 39 wp_cache_delete( $f, 'bp_xprofile_data_' . $u);39 wp_cache_delete( "{$u}:{$f}", 'bp_xprofile_data' ); 40 40 41 41 $this->assertTrue( $d->exists() ); 42 42 } … … 56 56 // Fake the cache 57 57 $c = new stdClass; 58 58 $c->id = 3; 59 wp_cache_set( $f, $c, 'bp_xprofile_data_' . $u);59 wp_cache_set( "{$u}:{$f}", $c, 'bp_xprofile_data' ); 60 60 61 61 $this->assertTrue( $d->exists() ); 62 62 } … … 73 73 ) ); 74 74 75 75 // Just to be sure 76 wp_cache_delete( $f, 'bp_xprofile_data_' . $u);76 wp_cache_delete( "{$u}:{$f}", 'bp_xprofile_data' ); 77 77 78 78 $this->assertEquals( 0, BP_XProfile_ProfileData::get_fielddataid_byid( $f, $u ) ); 79 79 } … … 96 96 $d->save(); 97 97 98 98 // Ensure it's deleted from cache 99 wp_cache_delete( $f, 'bp_xprofile_data_' . $u);99 wp_cache_delete( "{$u}:{$f}", 'bp_xprofile_data' ); 100 100 101 101 $this->assertEquals( $d->id, BP_XProfile_ProfileData::get_fielddataid_byid( $f, $u ) ); 102 102 } … … 115 115 // Fake the cache 116 116 $d = new stdClass; 117 117 $d->id = 5; 118 wp_cache_set( $f, $d, 'bp_xprofile_data_' . $u);118 wp_cache_set( "{$u}:{$f}", $d, 'bp_xprofile_data' ); 119 119 120 120 $this->assertSame( 5, BP_XProfile_ProfileData::get_fielddataid_byid( $f, $u ) ); 121 121 } … … 138 138 $d->save(); 139 139 140 140 // Ensure it's deleted from cache 141 wp_cache_delete( $f, 'bp_xprofile_data_' . $u);141 wp_cache_delete( "{$u}:{$f}", 'bp_xprofile_data' ); 142 142 143 143 $this->assertSame( 'foo', BP_XProfile_ProfileData::get_value_byid( $f, $u ) ); 144 144 } … … 179 179 remove_filter( 'xprofile_data_last_updated_before_save', array( $this, 'filter_time' ) ); 180 180 181 181 // Ensure it's deleted from cache 182 wp_cache_delete( $f, 'bp_xprofile_data_' . $u1);183 wp_cache_delete( $f, 'bp_xprofile_data_' . $u2);182 wp_cache_delete( "{$u1}:{$f}", 'bp_xprofile_data' ); 183 wp_cache_delete( "{$u2}:{$f}", 'bp_xprofile_data' ); 184 184 185 185 $eu1 = new stdClass; 186 186 $eu1->user_id = $u1; … … 218 218 $d = new stdClass; 219 219 $d->value = 'foo'; 220 220 $d->field_id = $f; 221 wp_cache_set( $f, $d, 'bp_xprofile_data_' . $u);221 wp_cache_set( "{$u}:{$f}", $d, 'bp_xprofile_data' ); 222 222 223 223 $this->assertSame( 'foo', BP_XProfile_ProfileData::get_value_byid( $f, $u ) ); 224 224 } … … 252 252 $d2->value = 'bar'; 253 253 $d2->last_updated = $time; 254 254 255 wp_cache_set( $f, $d1, 'bp_xprofile_data_' . $u1);256 wp_cache_set( $f, $d2, 'bp_xprofile_data_' . $u2);255 wp_cache_set( "{$u1}:{$f}", $d1, 'bp_xprofile_data' ); 256 wp_cache_set( "{$u2}:{$f}", $d2, 'bp_xprofile_data' ); 257 257 258 258 $eu1 = new stdClass; 259 259 $eu1->id = 10; … … 313 313 $d2->save(); 314 314 315 315 // Ensure it's deleted from cache 316 wp_cache_delete( $f1, 'bp_xprofile_data_' . $u);317 wp_cache_delete( $f2, 'bp_xprofile_data_' . $u);316 wp_cache_delete( "{$u}:{$f1}", 'bp_xprofile_data' ); 317 wp_cache_delete( "{$u}:{$f2}", 'bp_xprofile_data' ); 318 318 319 319 $u_obj = new WP_User( $u ); 320 320 … … 389 389 $d2->last_updated = $time; 390 390 $d2->id = 2; 391 391 392 wp_cache_set( $f1, $d1, 'bp_xprofile_data_' . $u);393 wp_cache_set( $f2, $d2, 'bp_xprofile_data_' . $u);392 wp_cache_set( "{$u}:{$f1}", $d1, 'bp_xprofile_data' ); 393 wp_cache_set( "{$u}:{$f2}", $d2, 'bp_xprofile_data' ); 394 394 395 395 $u_obj = new WP_User( $u ); 396 396