Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/20/2016 02:42:04 PM (9 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.