Changeset 13987
- Timestamp:
- 07/27/2024 07:31:15 PM (4 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-groups/classes/class-bp-groups-group.php
r13890 r13987 1058 1058 * See {@link WP_Meta_Query::queries} for description. 1059 1059 * @type array $date_query Optional. Filter results by group last activity date. See first 1060 * param ter of {@link WP_Date_Query::__construct()} for syntax. Only1060 * parameter of {@link WP_Date_Query::__construct()} for syntax. Only 1061 1061 * applicable if $type is either 'newest' or 'active'. 1062 1062 * @type array|string $value Optional. Array or comma-separated list of group IDs. Results … … 1066 1066 * @type array|string $exclude Optional. Array or comma-separated list of group IDs. 1067 1067 * Results will exclude the listed groups. Default: false. 1068 * @type bool $update_meta_cache Whether to pre-fetch groupmeta for the returned groups. 1068 * @type bool $cache_results Optional. Whether to cache group information. Default true. 1069 * @type bool $update_meta_cache Optional. Whether to pre-fetch groupmeta for the returned groups. 1069 1070 * Default: true. 1070 * @type bool $update_admin_cache Whether to pre-fetch administrator IDs for the returned1071 * @type bool $update_admin_cache Optional. Whether to pre-fetch administrator IDs for the returned 1071 1072 * groups. Default: false. 1072 1073 * @type bool $show_hidden Whether to include hidden groups in results. Default: false. … … 1135 1136 'include' => false, 1136 1137 'parent_id' => null, 1138 'cache_results' => true, 1137 1139 'update_meta_cache' => true, 1138 1140 'update_admin_cache' => false, … … 1374 1376 $paged_groups_sql = apply_filters( 'bp_groups_get_paged_groups_sql', $paged_groups_sql, $sql, $r ); 1375 1377 1376 $cached = bp_core_get_incremented_cache( $paged_groups_sql, 'bp_groups' ); 1377 if ( false === $cached ) { 1378 /* 1379 * Ensure the database query is able to be cached. 1380 * 1381 * Random queries are expected to have unpredictable results. 1382 */ 1383 $query_is_cacheable = 'rand()' !== $orderby; 1384 1385 if ( $r['cache_results'] && $query_is_cacheable ) { 1386 $cached = bp_core_get_incremented_cache( $paged_groups_sql, 'bp_groups' ); 1387 if ( false === $cached ) { 1388 $paged_group_ids = $wpdb->get_col( $paged_groups_sql ); 1389 bp_core_set_incremented_cache( $paged_groups_sql, 'bp_groups', $paged_group_ids ); 1390 } else { 1391 $paged_group_ids = $cached; 1392 } 1393 } else { 1378 1394 $paged_group_ids = $wpdb->get_col( $paged_groups_sql ); 1379 bp_core_set_incremented_cache( $paged_groups_sql, 'bp_groups', $paged_group_ids );1380 } else {1381 $paged_group_ids = $cached;1382 1395 } 1383 1396 … … 1386 1399 $paged_groups = array_map( 'intval', $paged_group_ids ); 1387 1400 } else { 1388 $uncached_group_ids = bp_get_non_cached_ids( $paged_group_ids, 'bp_groups' ); 1389 if ( $uncached_group_ids ) { 1390 $group_ids_sql = implode( ',', array_map( 'intval', $uncached_group_ids ) ); 1391 $group_data_objects = $wpdb->get_results( "SELECT g.* FROM {$bp->groups->table_name} g WHERE g.id IN ({$group_ids_sql})" ); 1392 foreach ( $group_data_objects as $group_data_object ) { 1393 wp_cache_set( $group_data_object->id, $group_data_object, 'bp_groups' ); 1401 if ( $r['cache_results'] && $query_is_cacheable ) { 1402 $uncached_group_ids = bp_get_non_cached_ids( $paged_group_ids, 'bp_groups' ); 1403 if ( $uncached_group_ids ) { 1404 $group_ids_sql = implode( ',', array_map( 'intval', $uncached_group_ids ) ); 1405 $group_data_objects = $wpdb->get_results( "SELECT g.* FROM {$bp->groups->table_name} g WHERE g.id IN ({$group_ids_sql})" ); 1406 foreach ( $group_data_objects as $group_data_object ) { 1407 wp_cache_set( $group_data_object->id, $group_data_object, 'bp_groups' ); 1408 } 1394 1409 } 1395 1410 } … … 1401 1416 1402 1417 $group_ids = array(); 1403 foreach ( (array)$paged_groups as $group ) {1418 foreach ( $paged_groups as $group ) { 1404 1419 $group_ids[] = $group->id; 1405 1420 } 1406 1421 1407 1422 // Grab all groupmeta. 1408 if ( ! empty( $r['update_meta_cache'] )) {1423 if ( $r['update_meta_cache'] ) { 1409 1424 bp_groups_update_meta_cache( $group_ids ); 1410 1425 } … … 1416 1431 1417 1432 // Set up integer properties needing casting. 1418 $int_props = array( 1419 'id', 'creator_id', 'enable_forum' 1420 ); 1433 $int_props = array( 'id', 'creator_id', 'enable_forum' ); 1421 1434 1422 1435 // Integer casting. … … 1426 1439 } 1427 1440 } 1428 1429 1441 } 1430 1442 … … 1443 1455 $total_groups_sql = apply_filters( 'bp_groups_get_total_groups_sql', $total_groups_sql, $sql, $r ); 1444 1456 1445 $cached = bp_core_get_incremented_cache( $total_groups_sql, 'bp_groups' ); 1446 if ( false === $cached ) { 1457 if ( $r['cache_results'] && $query_is_cacheable ) { 1458 $cached = bp_core_get_incremented_cache( $total_groups_sql, 'bp_groups' ); 1459 if ( false === $cached ) { 1460 $total_groups = (int) $wpdb->get_var( $total_groups_sql ); 1461 bp_core_set_incremented_cache( $total_groups_sql, 'bp_groups', $total_groups ); 1462 } else { 1463 $total_groups = (int) $cached; 1464 } 1465 } else { 1447 1466 $total_groups = (int) $wpdb->get_var( $total_groups_sql ); 1448 bp_core_set_incremented_cache( $total_groups_sql, 'bp_groups', $total_groups );1449 } else { 1450 $total_groups = (int) $cached;1451 }1452 1453 return array( 'groups' => $paged_groups, 'total' => $total_groups);1467 } 1468 1469 return array( 1470 'groups' => $paged_groups, 1471 'total' => $total_groups, 1472 ); 1454 1473 } 1455 1474 -
trunk/tests/phpunit/testcases/groups/cache.php
r13874 r13987 6 6 */ 7 7 class BP_Tests_Group_Cache extends BP_UnitTestCase { 8 9 /** 10 * @ticket BP8552 11 */ 12 public function test_query_cache_results() { 13 global $wpdb; 14 15 self::factory()->group->create_many( 2 ); 16 17 // Reset. 18 $wpdb->num_queries = 0; 19 20 $first_query = BP_Groups_Group::get( 21 array( 22 'cache_results' => true, 23 'fields' => 'ids', 24 ) 25 ); 26 27 $queries_before = get_num_queries(); 28 29 $second_query = BP_Groups_Group::get( 30 array( 31 'cache_results' => false, 32 'fields' => 'ids', 33 ) 34 ); 35 36 $queries_after = get_num_queries(); 37 38 $this->assertNotSame( $queries_before, $queries_after, 'Assert that queries are run' ); 39 $this->assertSame( 4, $queries_after, 'Assert that the uncached query was run' ); 40 $this->assertSameSets( $first_query['groups'], $second_query['groups'], 'Results of the query are expected to match.' ); 41 } 42 43 /** 44 * @ticket BP8552 45 */ 46 public function test_random_query_cache_results() { 47 global $wpdb; 48 49 self::factory()->group->create_many( 2 ); 50 51 // Reset. 52 $wpdb->num_queries = 0; 53 54 $args = array( 55 'orderby' => 'random', 56 'fields' => 'ids', 57 ); 58 59 BP_Groups_Group::get( $args ); 60 BP_Groups_Group::get( $args ); 61 62 $this->assertSame( 4, get_num_queries(), 'Assert random group queries are not cached.' ); 63 } 8 64 9 65 /**
Note: See TracChangeset
for help on using the changeset viewer.