Changeset 9336
- Timestamp:
- 01/09/2015 08:38:37 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-xprofile/bp-xprofile-cache.php
r9318 r9336 12 12 // Exit if accessed directly 13 13 if ( !defined( 'ABSPATH' ) ) exit; 14 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 } 14 37 15 38 /** … … 41 64 'group' => array(), 42 65 'field' => array(), 43 'data' 66 'data' => array(), 44 67 ); 45 68 … … 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);174 } 175 add_action( 'xprofile_data_after_save', 'xprofile_clear_profiledata_object_cache' );196 wp_cache_delete( "{$data_obj->user_id}:{$data_obj->field_id}", 'bp_xprofile_data' ); 197 } 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 -
trunk/src/bp-xprofile/bp-xprofile-classes.php
r9329 r9336 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 } … … 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 ) ) { … … 1401 1402 $data = array(); 1402 1403 1403 $cache_group = 'bp_xprofile_data_' . $user_id; 1404 1405 $uncached_field_ids = bp_get_non_cached_ids( $field_ids, $cache_group ); 1404 $uncached_field_ids = bp_xprofile_get_non_cached_field_ids( $user_id, $field_ids, 'bp_xprofile_data' ); 1406 1405 1407 1406 // Prime the cache … … 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 … … 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 } … … 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 … … 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( … … 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 ) ); … … 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 } … … 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 } … … 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 -
trunk/src/bp-xprofile/bp-xprofile-loader.php
r9289 r9336 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 * -
trunk/tests/phpunit/testcases/members/functions.php
r9234 r9336 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 ) ); -
trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-profiledata.php
r9139 r9336 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() ); … … 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() ); … … 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 ) ); … … 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 ) ); … … 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 ) ); … … 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 ) ); … … 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; … … 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 ) ); … … 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; … … 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 ); … … 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 );
Note: See TracChangeset
for help on using the changeset viewer.