Skip to:
Content

BuddyPress.org

Changeset 8076


Ignore:
Timestamp:
03/07/2014 01:32:04 AM (10 years ago)
Author:
boonebgorges
Message:

Improved cache invalidation when posting activity comments

  • Because activity comment caches are stored only with the top-level parent, we should be invalidating the bp_activity_comments cache for that item, not for the immediate parent
  • When a new comment is created, clear the bp_activity cache for each ancestor

See #5434

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity/bp-activity-functions.php

    r8035 r8076  
    12411241    ) );
    12421242
    1243     // Clear the comment and item cache for this activity
    1244     wp_cache_delete( $parent_id, 'bp_activity_comments' );
    1245     wp_cache_delete( $parent_id, 'bp_activity' );
     1243    // Comment caches are stored only with the top-level item
     1244    wp_cache_delete( $activity_id, 'bp_activity_comments' );
     1245
     1246    // Walk the tree to clear caches for all parent items
     1247    $clear_id = $parent_id;
     1248    while ( $clear_id != $activity_id ) {
     1249        $clear_object = new BP_Activity_Activity( $clear_id );
     1250        wp_cache_delete( $clear_id, 'bp_activity' );
     1251        $clear_id = intval( $clear_object->secondary_item_id );
     1252    }
     1253    wp_cache_delete( $activity_id, 'bp_activity' );
    12461254
    12471255    do_action( 'bp_activity_comment_posted', $comment_id, $params, $activity );
  • trunk/tests/testcases/activity/functions.php

    r7901 r8076  
    541541    }
    542542
     543    /**
     544     * @group bp_activity_new_comment
     545     * @group cache
     546     */
     547    public function test_bp_activity_new_comment_clear_comment_caches() {
     548        $a1 = $this->factory->activity->create();
     549        $a2 = bp_activity_new_comment( array(
     550            'activity_id' => $a1,
     551            'parent_id' => $a1,
     552            'content' => 'foo',
     553            'user_id' => 1,
     554        ) );
     555        $a3 = bp_activity_new_comment( array(
     556            'activity_id' => $a1,
     557            'parent_id' => $a2,
     558            'content' => 'foo',
     559            'user_id' => 1,
     560        ) );
     561        $a4 = bp_activity_new_comment( array(
     562            'activity_id' => $a1,
     563            'parent_id' => $a3,
     564            'content' => 'foo',
     565            'user_id' => 1,
     566        ) );
     567        $a5 = bp_activity_new_comment( array(
     568            'activity_id' => $a1,
     569            'parent_id' => $a3,
     570            'content' => 'foo',
     571            'user_id' => 1,
     572        ) );
     573
     574        // prime caches
     575        bp_activity_get( array(
     576            'in' => array( $a1 ),
     577            'display_comments' => 'threaded',
     578        ) );
     579
     580        // should be populated
     581        $this->assertNotEmpty( wp_cache_get( $a1, 'bp_activity_comments' ) );
     582
     583        bp_activity_new_comment( array(
     584            'activity_id' => $a1,
     585            'parent_id' => $a4,
     586            'content' => 'foo',
     587            'user_id' => 1,
     588        ) );
     589
     590        // should be empty
     591        $this->assertFalse( wp_cache_get( $a1, 'bp_activity_comments' ) );
     592    }
     593
     594    /**
     595     * @group bp_activity_new_comment
     596     * @group cache
     597     */
     598    public function test_bp_activity_new_comment_clear_activity_caches() {
     599        $a1 = $this->factory->activity->create();
     600        $a2 = bp_activity_new_comment( array(
     601            'activity_id' => $a1,
     602            'parent_id' => $a1,
     603            'content' => 'foo',
     604            'user_id' => 1,
     605        ) );
     606        $a3 = bp_activity_new_comment( array(
     607            'activity_id' => $a1,
     608            'parent_id' => $a2,
     609            'content' => 'foo',
     610            'user_id' => 1,
     611        ) );
     612        $a4 = bp_activity_new_comment( array(
     613            'activity_id' => $a1,
     614            'parent_id' => $a3,
     615            'content' => 'foo',
     616            'user_id' => 1,
     617        ) );
     618        $a5 = bp_activity_new_comment( array(
     619            'activity_id' => $a1,
     620            'parent_id' => $a3,
     621            'content' => 'foo',
     622            'user_id' => 1,
     623        ) );
     624
     625        // prime caches
     626        bp_activity_get( array(
     627            'in' => array( $a1 ),
     628            'display_comments' => 'threaded',
     629        ) );
     630
     631        // should be populated
     632        $this->assertNotEmpty( wp_cache_get( $a1, 'bp_activity' ) );
     633        $this->assertNotEmpty( wp_cache_get( $a2, 'bp_activity' ) );
     634        $this->assertNotEmpty( wp_cache_get( $a3, 'bp_activity' ) );
     635        $this->assertNotEmpty( wp_cache_get( $a4, 'bp_activity' ) );
     636        $this->assertNotEmpty( wp_cache_get( $a5, 'bp_activity' ) );
     637
     638        // Stuff may run on bp_activity_comment_posted that loads the
     639        // cache, so we use this dumb technique to check cache values
     640        // before any of that stuff gets a chance to run. WordPress
     641        // sure is neat sometimes
     642        $this->acaches = array(
     643            $a1 => '',
     644            $a2 => '',
     645            $a3 => '',
     646            $a4 => '',
     647        );
     648        add_action( 'bp_activity_comment_posted', array( $this, 'check_activity_caches' ), 0 );
     649
     650        bp_activity_new_comment( array(
     651            'activity_id' => $a1,
     652            'parent_id' => $a4,
     653            'content' => 'foo',
     654            'user_id' => 1,
     655        ) );
     656
     657        // should be empty
     658        foreach ( $this->acaches as $k => $v ) {
     659            $this->assertFalse( $v, "Cache should be false for $k" );
     660        }
     661    }
     662
     663    public function check_activity_caches() {
     664        foreach ( $this->acaches as $k => $v ) {
     665            $this->acaches[ $k ] = wp_cache_get( $k, 'bp_activity' );
     666        }
     667    }
    543668}
Note: See TracChangeset for help on using the changeset viewer.