Skip to:
Content

BuddyPress.org

Changeset 11122


Ignore:
Timestamp:
09/20/2016 02:42:04 PM (8 years ago)
Author:
dcavins
Message:

Add bp_friends_friendships cache group.

Add friendship caching by the ID of the friendship.
Check the cache before querying for friendship
details in BP_Friends_Friendship::populate.

Update get_user_ids_for_friendship() to create
a new friendship object so it could possibly use
the cached object.

Props dcavins, boonebgorges.

See #6978.

Location:
trunk
Files:
4 edited

Legend:

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

    r10587 r11122  
    3434add_action( 'friends_friendship_accepted', 'friends_clear_friend_object_cache' );
    3535add_action( 'friends_friendship_deleted',  'friends_clear_friend_object_cache' );
     36
     37/**
     38 * Clear friendship caches on friendship changes.
     39 *
     40 * @since 2.7.0
     41 *
     42 * @param int $friendship_id     ID of the friendship that has changed.
     43 * @param int $initiator_user_id ID of the first user.
     44 * @param int $friend_user_id    ID of the second user.
     45 * @return bool
     46 */
     47function bp_friends_clear_bp_friends_friendships_cache( $friendship_id, $initiator_user_id, $friend_user_id ) {
     48    // Clear the friendship object cache.
     49    wp_cache_delete( $friendship_id, 'bp_friends_friendships' );
     50}
     51add_action( 'friends_friendship_requested', 'bp_friends_clear_bp_friends_friendships_cache', 10, 3 );
     52add_action( 'friends_friendship_accepted',  'bp_friends_clear_bp_friends_friendships_cache', 10, 3 );
     53add_action( 'friends_friendship_deleted',   'bp_friends_clear_bp_friends_friendships_cache', 10, 3 );
     54
     55/**
     56 * Clear friendship caches on friendship changes.
     57 *
     58 * @since 2.7.0
     59 *
     60 * @param int                   $friendship_id The friendship ID.
     61 * @param BP_Friends_Friendship $friendship Friendship object.
     62 */
     63function bp_friends_clear_bp_friends_friendships_cache_remove( $friendship_id, BP_Friends_Friendship $friendship ) {
     64    // Clear the friendship object cache.
     65    wp_cache_delete( $friendship_id, 'bp_friends_friendships' );
     66}
     67add_action( 'friends_friendship_withdrawn', 'bp_friends_clear_bp_friends_friendships_cache_remove', 10, 2 );
     68add_action( 'friends_friendship_rejected',  'bp_friends_clear_bp_friends_friendships_cache_remove', 10, 2 );
    3669
    3770/**
  • trunk/src/bp-friends/classes/class-bp-friends-component.php

    r11022 r11122  
    289289        // Global groups.
    290290        wp_cache_add_global_groups( array(
    291             'bp_friends_requests'
     291            'bp_friends_requests',
     292            'bp_friends_friendships', // Individual friendship objects are cached here by ID.
    292293        ) );
    293294
  • trunk/src/bp-friends/classes/class-bp-friends-friendship.php

    r11025 r11122  
    123123        $bp = buddypress();
    124124
    125         if ( $friendship = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->friends->table_name} WHERE id = %d", $this->id ) ) ) {
    126             $this->initiator_user_id = (int) $friendship->initiator_user_id;
    127             $this->friend_user_id    = (int) $friendship->friend_user_id;
    128             $this->is_confirmed      = (int) $friendship->is_confirmed;
    129             $this->is_limited        = (int) $friendship->is_limited;
    130             $this->date_created      = $friendship->date_created;
    131         }
    132 
    133         if ( !empty( $this->populate_friend_details ) ) {
     125        // Check cache for friendship data.
     126        $friendship = wp_cache_get( $this->id, 'bp_friends_friendships' );
     127
     128        // Cache missed, so query the DB.
     129        if ( false === $friendship ) {
     130            $friendship = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->friends->table_name} WHERE id = %d", $this->id ) );
     131
     132            wp_cache_set( $this->id, $friendship, 'bp_friends_friendships' );
     133        }
     134
     135        // No friendship found so set the ID and bail.
     136        if ( empty( $friendship ) || is_wp_error( $friendship ) ) {
     137            $this->id = 0;
     138            return;
     139        }
     140
     141        $this->initiator_user_id = (int) $friendship->initiator_user_id;
     142        $this->friend_user_id    = (int) $friendship->friend_user_id;
     143        $this->is_confirmed      = (int) $friendship->is_confirmed;
     144        $this->is_limited        = (int) $friendship->is_limited;
     145        $this->date_created      = $friendship->date_created;
     146
     147        if ( ! empty( $this->populate_friend_details ) ) {
    134148            if ( $this->friend_user_id == bp_displayed_user_id() ) {
    135149                $this->friend = new BP_Core_User( $this->initiator_user_id );
     
    708722     */
    709723    public static function get_user_ids_for_friendship( $friendship_id ) {
    710         global $wpdb;
    711 
    712         $bp = buddypress();
    713 
    714         return $wpdb->get_row( $wpdb->prepare( "SELECT friend_user_id, initiator_user_id FROM {$bp->friends->table_name} WHERE id = %d", $friendship_id ) );
     724
     725        $friendship = new BP_Friends_Friendship( $friendship_id, false, false );
     726
     727        if ( empty( $friendship->id ) ) {
     728            return null;
     729        }
     730
     731        $retval = new StdClass;
     732        $retval->friend_user_id = $friendship->friend_user_id;
     733        $retval->initiator_user_id = $friendship->initiator_user_id;
     734
     735        return $retval;
    715736    }
    716737
  • trunk/tests/phpunit/testcases/friends/class-bp-friends-friendship.php

    r9985 r11122  
    148148        $this->assertEquals( 1, $f2->is_confirmed );
    149149    }
     150
     151    /**
     152     * @group friendship_caching
     153     */
     154    public function test_new_bp_friends_friendship_object_should_hit_friendship_object_cache() {
     155        global $wpdb;
     156        $now = time();
     157        $u1 = $this->factory->user->create( array(
     158            'last_activity' => date( 'Y-m-d H:i:s', $now ),
     159        ) );
     160        $u2 = $this->factory->user->create( array(
     161            'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ),
     162        ) );
     163
     164        friends_add_friend( $u1, $u2, true );
     165        $fid = friends_get_friendship_id( $u1, $u2 );
     166
     167        $friendship_obj = new BP_Friends_Friendship( $fid, false, false );
     168        $first_query_count = $wpdb->num_queries;
     169        // Create it again.
     170        $friendship_obj = new BP_Friends_Friendship( $fid, false, false );
     171
     172        $this->assertEquals( $first_query_count, $wpdb->num_queries );
     173    }
    150174}
Note: See TracChangeset for help on using the changeset viewer.