Skip to:
Content

BuddyPress.org

Changeset 11101


Ignore:
Timestamp:
09/14/2016 02:06:10 AM (10 years ago)
Author:
boonebgorges
Message:

Activity: Invalidate query caches when a user's 'last_activity' is updated.

User 'last_activity' is stored in the activity table, but not updated
via the Activity API, so regular invalidation techniques do not work.

In addition, 'last_activity' is quite frequently updated, meaning that
the activity query cache becomes less valuable. To mitigate this, the
current changeset separates the cache for queries containing
'last_activity' from the cache for those that do not.

Fixes #7245.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/bp-activity-cache.php

    r11077 r11101  
    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' );
  • trunk/src/bp-activity/classes/class-bp-activity-activity.php

    r11054 r11101  
    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                        /*
     649                         * Queries that include 'last_activity' are cached separately,
     650                         * since they are generally much less long-lived.
     651                         */
     652                        if ( preg_match( '/a\.type NOT IN \([^\)]*\'last_activity\'[^\)]*\)/', $activity_ids_sql ) ) {
     653                                $cache_group = 'bp_activity';
     654                        } else {
     655                                $cache_group = 'bp_activity_with_last_activity';
     656                        }
     657
     658                        $cached = bp_core_get_incremented_cache( $activity_ids_sql, $cache_group );
    649659                        if ( false === $cached ) {
    650660                                $activity_ids = $wpdb->get_col( $activity_ids_sql );
    651                                 bp_core_set_incremented_cache( $activity_ids_sql, 'bp_activity', $activity_ids );
     661                                bp_core_set_incremented_cache( $activity_ids_sql, $cache_group, $activity_ids );
    652662                        } else {
    653663                                $activity_ids = $cached;
    … …  
    709719                         */
    710720                        $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' );
     721                        $cached = bp_core_get_incremented_cache( $total_activities_sql, $cache_group );
    712722                        if ( false === $cached ) {
    713723                                $total_activities = $wpdb->get_var( $total_activities_sql );
    714                                 bp_core_set_incremented_cache( $total_activities_sql, 'bp_activity', $total_activities );
     724                                bp_core_set_incremented_cache( $total_activities_sql, $cache_group, $total_activities );
    715725                        } else {
    716726                                $total_activities = $cached;
  • trunk/src/bp-core/classes/class-bp-core-user.php

    r11026 r11101  
    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        }
  • trunk/src/bp-members/bp-members-cache.php

    r10711 r11101  
    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' );
  • trunk/tests/phpunit/testcases/members/cache.php

    r9819 r11101  
    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
Note: See TracChangeset for help on using the changeset viewer.