Skip to:
Content

BuddyPress.org

Ticket #7245: 7245.2.diff

File 7245.2.diff, 6.3 KB (added by boonebgorges, 5 years ago)
  • src/bp-activity/bp-activity-cache.php

    diff --git src/bp-activity/bp-activity-cache.php src/bp-activity/bp-activity-cache.php
    index d1c4bc0..d6655a4 100644
    add_action( 'bp_activity_deleted_activities', 'bp_activity_clear_cache_for_delet 
    7575 * @return bool True on success, false on failure.
    7676 */
    7777function bp_activity_reset_cache_incrementor() {
    78         return bp_core_reset_incrementor( 'bp_activity' );
     78        $without_last_activity = bp_core_reset_incrementor( 'bp_activity' );
     79        $with_last_activity    = bp_core_reset_incrementor( 'bp_activity_with_last_activity' );
     80        return $without_last_activity && $with_last_activity;
    7981}
    8082add_action( 'bp_activity_delete', 'bp_activity_reset_cache_incrementor' );
    8183add_action( 'bp_activity_add', 'bp_activity_reset_cache_incrementor' );
  • src/bp-activity/classes/class-bp-activity-activity.php

    diff --git src/bp-activity/classes/class-bp-activity-activity.php src/bp-activity/classes/class-bp-activity-activity.php
    index bfcf975..d405295 100644
    class BP_Activity_Activity { 
    645645                         */
    646646                        $activity_ids_sql = apply_filters( 'bp_activity_paged_activities_sql', $activity_ids_sql, $r );
    647647
    648                         $cached = bp_core_get_incremented_cache( $activity_ids_sql, 'bp_activity' );
     648                        if ( preg_match( '/a\.type NOT IN \([^\)]*\'last_activity\'[^\)]*\)/', $activity_ids_sql ) ) {
     649                                $cache_group = 'bp_activity';
     650                        } else {
     651                                $cache_group = 'bp_activity_with_last_activity';
     652                        }
     653
     654                        $cached = bp_core_get_incremented_cache( $activity_ids_sql, $cache_group );
    649655                        if ( false === $cached ) {
    650656                                $activity_ids = $wpdb->get_col( $activity_ids_sql );
    651                                 bp_core_set_incremented_cache( $activity_ids_sql, 'bp_activity', $activity_ids );
     657                                bp_core_set_incremented_cache( $activity_ids_sql, $cache_group, $activity_ids );
    652658                        } else {
    653659                                $activity_ids = $cached;
    654660                        }
    class BP_Activity_Activity { 
    708714                         * @param string $sort      Sort direction for query.
    709715                         */
    710716                        $total_activities_sql = apply_filters( 'bp_activity_total_activities_sql', "SELECT count(DISTINCT a.id) FROM {$bp->activity->table_name} a {$join_sql} {$where_sql}", $where_sql, $sort );
    711                         $cached = bp_core_get_incremented_cache( $total_activities_sql, 'bp_activity' );
     717                        $cached = bp_core_get_incremented_cache( $total_activities_sql, $cache_group );
    712718                        if ( false === $cached ) {
    713719                                $total_activities = $wpdb->get_var( $total_activities_sql );
    714                                 bp_core_set_incremented_cache( $total_activities_sql, 'bp_activity', $total_activities );
     720                                bp_core_set_incremented_cache( $total_activities_sql, $cache_group, $total_activities );
    715721                        } else {
    716722                                $total_activities = $cached;
    717723                        }
  • src/bp-core/classes/class-bp-core-user.php

    diff --git src/bp-core/classes/class-bp-core-user.php src/bp-core/classes/class-bp-core-user.php
    index a9657aa..f102892 100644
    class BP_Core_User { 
    912912                // Set cache.
    913913                wp_cache_set( $user_id, $activity[ $user_id ], 'bp_last_activity' );
    914914
     915                /**
     916                 * Fires when a user's last_activity value has been updated.
     917                 *
     918                 * @since 2.7.0
     919                 *
     920                 * @param int    $user_id ID of the user.
     921                 * @param string $time    Last activity timestamp, in 'Y-m-d H:i:s' format.
     922                 */
     923                do_action( 'bp_core_user_updated_last_activity', $user_id, $time );
     924
    915925                return $updated;
    916926        }
    917927
  • src/bp-members/bp-members-cache.php

    diff --git src/bp-members/bp-members-cache.php src/bp-members/bp-members-cache.php
    index 49b4b50..df36860 100644
    function bp_members_clear_member_type_cache( $user_id ) { 
    6161}
    6262add_action( 'wpmu_delete_user', 'bp_members_clear_member_type_cache' );
    6363add_action( 'delete_user', 'bp_members_clear_member_type_cache' );
     64
     65/**
     66 * Invalidate activity caches when a user's last_activity value is changed.
     67 *
     68 * @since 2.7.0
     69 *
     70 * @return bool True on success, false on failure.
     71 */
     72function bp_members_reset_activity_cache_incrementor() {
     73        return bp_core_reset_incrementor( 'bp_activity_with_last_activity' );
     74}
     75add_action( 'bp_core_user_updated_last_activity', 'bp_members_reset_activity_cache_incrementor' );
  • tests/phpunit/testcases/members/cache.php

    diff --git tests/phpunit/testcases/members/cache.php tests/phpunit/testcases/members/cache.php
    index e4d7e04..725e791 100644
    class BP_Tests_Members_Cache extends BP_UnitTestCase { 
    2222                // check if function references cache or hits the DB by comparing query count
    2323                $this->assertEquals( $num_queries, $wpdb->num_queries );
    2424        }
     25
     26        /**
     27         * @ticket BP7237
     28         * @ticket BP6643
     29         * @ticket BP7245
     30         */
     31        public function test_last_activity_should_bust_activity_with_last_activity_cache() {
     32                global $wpdb;
     33
     34                $u1 = $this->factory->user->create();
     35                $u2 = $this->factory->user->create();
     36
     37                $time_1 = date( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS );
     38                $time_2 = date( 'Y-m-d H:i:s', time() - ( HOUR_IN_SECONDS * 2 ) );
     39                bp_update_user_last_activity( $u1, $time_1 );
     40                bp_update_user_last_activity( $u2, $time_2 );
     41
     42                $activity_args_a = array(
     43                        'filter' => array(
     44                                'object' => buddypress()->members->id,
     45                                'action' => 'last_activity',
     46                        ),
     47                        'max' => 1,
     48                );
     49
     50                $activity_args_b = array(
     51                        'filter' => array(
     52                                'action' => 'new_member',
     53                        ),
     54                        'fields' => 'ids',
     55                );
     56
     57                // Prime bp_activity and bp_activity_with_last_activity caches.
     58                $a1 = bp_activity_get( $activity_args_a );
     59                $expected = array( $u1, $u2 );
     60                $found = array_map( 'intval', wp_list_pluck( $a1['activities'], 'user_id' ) );
     61                $this->assertSame( $expected, $found );
     62
     63                $b1 = bp_activity_get( $activity_args_b );
     64
     65                // Bump u2 activity so it should appear first.
     66                $new_time = date( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS );
     67                bp_update_user_last_activity( $u2, $new_time );
     68
     69                $a2 = bp_activity_get( $activity_args_a );
     70                $expected = array( $u2, $u1 );
     71                $found = array_map( 'intval', wp_list_pluck( $a2['activities'], 'user_id' ) );
     72                $this->assertSame( $expected, $found );
     73
     74                $num_queries = $wpdb->num_queries;
     75
     76                // bp_activity cache should not have been touched.
     77                $b2 = bp_activity_get( $activity_args_b );
     78                $this->assertEqualSets( $b1, $b2 );
     79                $this->assertSame( $num_queries, $wpdb->num_queries );
     80        }
    2581}
    2682