Skip to:
Content

BuddyPress.org

Changeset 13989


Ignore:
Timestamp:
07/27/2024 08:12:32 PM (7 months ago)
Author:
espellcaste
Message:

Members: Add cache_results flag to the BP_Signup::get getter.

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

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

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-members/classes/class-bp-signup.php

    r13890 r13989  
    259259     *
    260260     * @since 2.0.0
    261      * @since 6.0.0 Added a list of allowed orderby parameters.
     261     * @since 6.0.0  Added a list of allowed orderby parameters.
     262     * @since 15.0.0 Introduced the `cache_results` parameter.
    262263     *
    263264     * @global wpdb $wpdb The WordPress database object.
     
    267268     *     @type int         $offset         Offset amount. Default 0.
    268269     *     @type int         $number         How many to fetch. Pass -1 to fetch all. Default 1.
    269      *     @type bool|string $usersearch     Whether or not to search for a username. Default false.
    270      *     @type string      $orderby        Order By parameter. Possible values are `signup_id`, `login`, `email`,
     270     *     @type bool|string $usersearch     Whether to search for a username. Default false.
     271     *     @type string      $orderby        Order by parameter. Possible values are `signup_id`, `login`, `email`,
    271272     *                                       `registered`, `activated`. Default `signup_id`.
    272273     *     @type string      $order          Order direction. Default 'DESC'.
    273      *     @type bool        $include        Whether or not to include more specific query params.
     274     *     @type bool        $include        Whether to include more specific query params.
    274275     *     @type string      $activation_key Activation key to search for. If specified, all other
    275276     *                                       parameters will be ignored.
    276277     *     @type int|bool    $active         Pass 0 for inactive signups, 1 for active signups,
    277278     *                                       and `false` to ignore.
     279     *     @type bool        $cache_results  Optional. Whether to cache signup information. Default true.
    278280     *     @type string      $user_login     Specific user login to return.
    279281     *     @type string      $fields         Which fields to return. Specify 'ids' to fetch a list of signups IDs.
     
    302304                'user_email'     => '',
    303305                'user_login'     => '',
     306                'cache_results'  => true,
    304307                'fields'         => 'all',
    305308            ),
     
    319322
    320323        $sql = array(
    321             'select'     => "SELECT DISTINCT signup_id",
    322             'from'       => "{$bp->members->table_name_signups}",
    323             'where'      => array(),
    324             'orderby'    => '',
    325             'limit'      => '',
     324            'select'  => 'SELECT DISTINCT signup_id',
     325            'from'    => "{$bp->members->table_name_signups}",
     326            'where'   => array(),
     327            'orderby' => '',
     328            'limit'   => '',
    326329        );
    327330
    328331        // Activation key trumps other parameters because it should be unique.
    329332        if ( ! empty( $r['activation_key'] ) ) {
    330             $sql['where'][] = $wpdb->prepare( "activation_key = %s", $r['activation_key'] );
     333            $sql['where'][] = $wpdb->prepare( 'activation_key = %s', $r['activation_key'] );
    331334
    332335            // `Include` finds signups by ID.
    333         } else if ( ! empty( $r['include'] ) ) {
     336        } elseif ( ! empty( $r['include'] ) ) {
    334337
    335338            $in             = implode( ',', wp_parse_id_list( $r['include'] ) );
     
    343346            // Active.
    344347            if ( false !== $r['active'] ) {
    345                 $sql['where'][] = $wpdb->prepare( "active = %d", absint( $r['active'] ) );
     348                $sql['where'][] = $wpdb->prepare( 'active = %d', absint( $r['active'] ) );
    346349            }
    347350
     
    349352            if ( ! empty( $r['usersearch'] ) ) {
    350353                $search_terms_like = '%' . bp_esc_like( $r['usersearch'] ) . '%';
    351                 $sql['where'][]    = $wpdb->prepare( "( user_login LIKE %s OR user_email LIKE %s OR meta LIKE %s )", $search_terms_like, $search_terms_like, $search_terms_like );
     354                $sql['where'][]    = $wpdb->prepare( '( user_login LIKE %s OR user_email LIKE %s OR meta LIKE %s )', $search_terms_like, $search_terms_like, $search_terms_like );
    352355            }
    353356
    354357            // User email.
    355358            if ( ! empty( $r['user_email'] ) ) {
    356                 $sql['where'][] = $wpdb->prepare( "user_email = %s", $r['user_email'] );
     359                $sql['where'][] = $wpdb->prepare( 'user_email = %s', $r['user_email'] );
    357360            }
    358361
    359362            // User login.
    360363            if ( ! empty( $r['user_login'] ) ) {
    361                 $sql['where'][] = $wpdb->prepare( "user_login = %s", $r['user_login'] );
    362             }
    363 
    364             $order          = bp_esc_sql_order( $r['order'] );
     364                $sql['where'][] = $wpdb->prepare( 'user_login = %s', $r['user_login'] );
     365            }
     366
     367            $order          = bp_esc_sql_order( $r['order'] );
    365368            $sql['orderby'] = "ORDER BY {$r['orderby']} {$order}";
    366369
    367370            $number = intval( $r['number'] );
    368371            if ( -1 !== $number ) {
    369                 $sql['limit'] = $wpdb->prepare( "LIMIT %d, %d", absint( $r['offset'] ), $number );
     372                $sql['limit'] = $wpdb->prepare( 'LIMIT %d, %d', absint( $r['offset'] ), $number );
    370373            }
    371374        }
     
    381384         * @since 2.0.0
    382385         *
    383          * @param string $value SQL statement.
    384          * @param array  $sql   Array of SQL statement parts.
    385          * @param array  $args  Array of original arguments for get() method.
    386          * @param array  $r     Array of parsed arguments for get() method.
     386         * @param string $paged_signups_sql SQL statement.
     387         * @param array  $sql               Array of SQL statement parts.
     388         * @param array  $args              Array of original arguments for get() method.
     389         * @param array  $r                 Array of parsed arguments for get() method.
    387390         */
    388391        $paged_signups_sql = apply_filters( 'bp_members_signups_paged_query', $paged_signups_sql, $sql, $args, $r );
    389392
    390         $cached = bp_core_get_incremented_cache( $paged_signups_sql, 'bp_signups' );
    391         if ( false === $cached ) {
     393        if ( $r['cache_results'] ) {
     394            $cached = bp_core_get_incremented_cache( $paged_signups_sql, 'bp_signups' );
     395            if ( false === $cached ) {
     396                $paged_signup_ids = $wpdb->get_col( $paged_signups_sql );
     397                bp_core_set_incremented_cache( $paged_signups_sql, 'bp_signups', $paged_signup_ids );
     398            } else {
     399                $paged_signup_ids = $cached;
     400            }
     401        } else {
    392402            $paged_signup_ids = $wpdb->get_col( $paged_signups_sql );
    393             bp_core_set_incremented_cache( $paged_signups_sql, 'bp_signups', $paged_signup_ids );
    394         } else {
    395             $paged_signup_ids = $cached;
    396403        }
    397404
     
    399406        if ( 'ids' === $r['fields'] ) {
    400407            $paged_signups = array_map( 'intval', $paged_signup_ids );
    401 
    402408        } else {
    403             $uncached_signup_ids = bp_get_non_cached_ids( $paged_signup_ids, 'bp_signups' );
    404             if ( $uncached_signup_ids ) {
    405                 $signup_ids_sql      = implode( ',', array_map( 'intval', $uncached_signup_ids ) );
    406                 $signup_data_objects = $wpdb->get_results( "SELECT * FROM {$bp->members->table_name_signups} WHERE signup_id IN ({$signup_ids_sql})" );
    407                 foreach ( $signup_data_objects as $signup_data_object ) {
    408                     wp_cache_set( $signup_data_object->signup_id, $signup_data_object, 'bp_signups' );
     409            if ( $r['cache_results'] ) {
     410                $uncached_signup_ids = bp_get_non_cached_ids( $paged_signup_ids, 'bp_signups' );
     411                if ( $uncached_signup_ids ) {
     412                    $signup_ids_sql      = implode( ',', array_map( 'intval', $uncached_signup_ids ) );
     413                    $signup_data_objects = $wpdb->get_results( "SELECT * FROM {$bp->members->table_name_signups} WHERE signup_id IN ({$signup_ids_sql})" );
     414                    foreach ( $signup_data_objects as $signup_data_object ) {
     415                        wp_cache_set( $signup_data_object->signup_id, $signup_data_object, 'bp_signups' );
     416                    }
    409417                }
    410418            }
     
    420428
    421429        /**
    422          * Filters the Signups count query.
    423          *
    424          * @since 2.0.0
    425          *
    426          * @param string $value SQL statement.
    427          * @param array  $sql   Array of SQL statement parts.
    428          * @param array  $args  Array of original arguments for get() method.
    429          * @param array  $r     Array of parsed arguments for get() method.
     430         * Filters the signups count query.
     431         *
     432         * @since 2.0.0
     433         *
     434         * @param string $total_signups_sql SQL statement.
     435         * @param array  $sql               Array of SQL statement parts.
     436         * @param array  $args              Array of original arguments for get() method.
     437         * @param array  $r                 Array of parsed arguments for get() method.
    430438         */
    431439        $total_signups_sql = apply_filters( 'bp_members_signups_count_query', $total_signups_sql, $sql, $args, $r );
    432440
    433         $cached = bp_core_get_incremented_cache( $total_signups_sql, 'bp_signups' );
    434         if ( false === $cached ) {
     441        if ( $r['cache_results'] ) {
     442            $cached = bp_core_get_incremented_cache( $total_signups_sql, 'bp_signups' );
     443            if ( false === $cached ) {
     444                $total_signups = (int) $wpdb->get_var( $total_signups_sql );
     445                bp_core_set_incremented_cache( $total_signups_sql, 'bp_signups', $total_signups );
     446            } else {
     447                $total_signups = (int) $cached;
     448            }
     449        } else {
    435450            $total_signups = (int) $wpdb->get_var( $total_signups_sql );
    436             bp_core_set_incremented_cache( $total_signups_sql, 'bp_signups', $total_signups );
    437         } else {
    438             $total_signups = (int) $cached;
    439         }
    440 
    441         return array( 'signups' => $paged_signups, 'total' => $total_signups );
     451        }
     452
     453        return array(
     454            'signups' => $paged_signups,
     455            'total'   => $total_signups,
     456        );
    442457    }
    443458
  • trunk/tests/phpunit/testcases/members/class-bp-signup.php

    r13980 r13989  
    359359
    360360    /**
     361     * @ticket BP8552
     362     * @group cache
     363     */
     364    public function test_signup_query_with_ids_cache_results() {
     365        global $wpdb;
     366
     367        self::factory()->signup->create_many( 2 );
     368
     369        // Reset.
     370        $wpdb->num_queries = 0;
     371
     372        $first_query = BP_Signup::get(
     373            array(
     374                'cache_results' => true,
     375                'fields'        => 'ids',
     376            )
     377        );
     378
     379        $queries_before = get_num_queries();
     380
     381        $second_query = BP_Signup::get(
     382            array(
     383                'cache_results' => false,
     384                'fields'        => 'ids',
     385            )
     386        );
     387
     388        $queries_after = get_num_queries();
     389
     390        $this->assertNotSame( $queries_before, $queries_after, 'Assert that queries are run' );
     391        $this->assertSame( 4, $queries_after, 'Assert that the uncached query was run' );
     392        $this->assertSameSets( $first_query['signups'], $second_query['signups'], 'Results of the query are expected to match.' );
     393        $this->assertSame( $first_query['total'], $second_query['total'], 'Results of the query are expected to match.' );
     394    }
     395
     396    /**
     397     * @ticket BP8552
     398     * @group cache
     399     */
     400    public function test_signup_query_with_all_cache_results() {
     401        global $wpdb;
     402
     403        self::factory()->signup->create_many( 2 );
     404
     405        // Reset.
     406        $wpdb->num_queries = 0;
     407
     408        $first_query = BP_Signup::get(
     409            array( 'cache_results' => true )
     410        );
     411
     412        $queries_before = get_num_queries();
     413
     414        $second_query = BP_Signup::get(
     415            array( 'cache_results' => false )
     416        );
     417
     418        $queries_after = get_num_queries();
     419
     420        $this->assertNotSame( $queries_before, $queries_after, 'Assert that queries are run' );
     421        $this->assertSame( 5, $queries_after, 'Assert that the uncached query was run' );
     422        $this->assertSame( $first_query['total'], $second_query['total'], 'Results of the query are expected to match.' );
     423    }
     424
     425    /**
    361426     * @group cache
    362427     */
     
    364429        global $wpdb;
    365430
    366         $s = self::factory()->signup->create();
    367 
    368         $found1 = BP_Signup::get(
    369             array(
    370                 'fields' => 'ids',
    371             )
    372         );
    373 
    374         $num_queries = $wpdb->num_queries;
    375 
    376         $found2 = BP_Signup::get(
    377             array(
    378                 'fields' => 'ids',
    379             )
    380         );
    381 
    382         $this->assertEqualSets( $found1, $found2 );
    383         $this->assertSame( $num_queries, $wpdb->num_queries );
     431        self::factory()->signup->create_many( 2 );
     432
     433        // Reset.
     434        $wpdb->num_queries = 0;
     435
     436        $args = array( 'fields' => 'ids' );
     437
     438        $first_query = BP_Signup::get( $args );
     439
     440        $queries_before = get_num_queries();
     441
     442        $second_query = BP_Signup::get( $args );
     443
     444        $queries_after = get_num_queries();
     445
     446        $this->assertSame( $queries_before, $queries_after, 'Assert that queries are run' );
     447        $this->assertSame( 2, $queries_after, 'Assert that the uncached query was run' );
     448        $this->assertSameSets( $first_query['signups'], $second_query['signups'], 'Results of the query are expected to match.' );
    384449    }
    385450
     
    388453     */
    389454    public function test_get_query_caches_should_be_busted_by_add() {
    390         $s1 = self::factory()->signup->create();
    391 
    392         $found1 = BP_Signup::get(
    393             array(
    394                 'fields' => 'ids',
    395             )
    396         );
     455        $s1   = self::factory()->signup->create();
     456        $args = array( 'fields' => 'ids' );
     457
     458        $found1 = BP_Signup::get( $args );
    397459        $this->assertEqualSets( array( $s1 ), $found1['signups'] );
    398460
    399461        $s2 = self::factory()->signup->create();
    400         $found2 = BP_Signup::get(
    401             array(
    402                 'fields' => 'ids',
    403             )
    404         );
     462        $found2 = BP_Signup::get( $args );
    405463        $this->assertEqualSets( array( $s2 ), $found2['signups'] );
    406464    }
     
    521579        $this->assertEqualSets( array( $s1, $s2 ), $found1['signups'] );
    522580
    523         $activate = BP_Signup::activate( (array) $s2 );
     581        BP_Signup::activate( (array) $s2 );
    524582
    525583        $found2 = BP_Signup::get(
     
    570628        $this->assertFalse( $found1->active );
    571629
    572         $activate = BP_Signup::activate( (array) $s1 );
     630        BP_Signup::activate( (array) $s1 );
    573631
    574632        $found2 = new BP_Signup( $s1 );
Note: See TracChangeset for help on using the changeset viewer.