Skip to:
Content

BuddyPress.org

Changeset 13987


Ignore:
Timestamp:
07/27/2024 07:31:15 PM (2 years ago)
Author:
espellcaste
Message:

Groups: Add cache_results flag to the BP_Groups_Group::get getter.

When performing a request with cache_results, it stops the group information retrieved from being added to the cache.

See #8552
Closes ​https://github.com/buddypress/buddypress/pull/342/

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/classes/class-bp-groups-group.php

    r13890 r13987  
    10581058         *                                            See {@link WP_Meta_Query::queries} for description.
    10591059         *     @type array        $date_query         Optional. Filter results by group last activity date. See first
    1060          *                                            paramter of {@link WP_Date_Query::__construct()} for syntax. Only
     1060         *                                            parameter of {@link WP_Date_Query::__construct()} for syntax. Only
    10611061         *                                            applicable if $type is either 'newest' or 'active'.
    10621062         *     @type array|string $value              Optional. Array or comma-separated list of group IDs. Results
    … …  
    10661066         *     @type array|string $exclude            Optional. Array or comma-separated list of group IDs.
    10671067         *                                            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.
    10691070         *                                            Default: true.
    1070          *     @type bool         $update_admin_cache Whether to pre-fetch administrator IDs for the returned
     1071         *     @type bool         $update_admin_cache Optional. Whether to pre-fetch administrator IDs for the returned
    10711072         *                                            groups. Default: false.
    10721073         *     @type bool         $show_hidden        Whether to include hidden groups in results. Default: false.
    … …  
    11351136                        'include'            => false,
    11361137                        'parent_id'          => null,
     1138                        'cache_results'      => true,
    11371139                        'update_meta_cache'  => true,
    11381140                        'update_admin_cache' => false,
    … …  
    13741376                $paged_groups_sql = apply_filters( 'bp_groups_get_paged_groups_sql', $paged_groups_sql, $sql, $r );
    13751377
    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 {
    13781394                        $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;
    13821395                }
    13831396
    … …  
    13861399                        $paged_groups = array_map( 'intval', $paged_group_ids );
    13871400                } 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                                        }
    13941409                                }
    13951410                        }
    … …  
    14011416
    14021417                        $group_ids = array();
    1403                         foreach ( (array) $paged_groups as $group ) {
     1418                        foreach ( $paged_groups as $group ) {
    14041419                                $group_ids[] = $group->id;
    14051420                        }
    14061421
    14071422                        // Grab all groupmeta.
    1408                         if ( ! empty( $r['update_meta_cache'] ) ) {
     1423                        if ( $r['update_meta_cache'] ) {
    14091424                                bp_groups_update_meta_cache( $group_ids );
    14101425                        }
    … …  
    14161431
    14171432                        // 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' );
    14211434
    14221435                        // Integer casting.
    … …  
    14261439                                }
    14271440                        }
    1428 
    14291441                }
    14301442
    … …  
    14431455                $total_groups_sql = apply_filters( 'bp_groups_get_total_groups_sql', $total_groups_sql, $sql, $r );
    14441456
    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 {
    14471466                        $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                );
    14541473        }
    14551474
  • trunk/tests/phpunit/testcases/groups/cache.php

    r13874 r13987  
    66 */
    77class 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        }
    864
    965        /**
Note: See TracChangeset for help on using the changeset viewer.