Skip to:
Content

BuddyPress.org

Changeset 8105


Ignore:
Timestamp:
03/10/2014 08:36:19 PM (11 years ago)
Author:
r-a-y
Message:

Cache the friendship request user IDs for a given user.

This commit:

  • Caches calls to BP_Friends_Friendship::get_friendship_request_user_ids()
  • Invalidates the friend request cache when a friendship is saved or removed
  • Adds unit tests

See #5435, #5414

Location:
trunk
Files:
1 added
2 edited

Legend:

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

    r7619 r8105  
    3232add_action( 'friends_friendship_deleted',  'friends_clear_friend_object_cache' );
    3333
     34/**
     35 * Clear the friend request cache for the user not initiating the friendship.
     36 *
     37 * @since BuddyPress (2.0.0)
     38 *
     39 * @param int $friend_user_id The user ID not initiating the friendship
     40 */
     41function bp_friends_clear_request_cache( $friend_user_id ) {
     42    wp_cache_delete( $friend_user_id, 'bp_friends_requests' );
     43}
     44
     45/**
     46 * Clear the friend request cache when a friendship is saved.
     47 *
     48 * A friendship is deemed saved when a friendship is requested or accepted.
     49 *
     50 * @since BuddyPress (2.0.0)
     51 *
     52 * @param int $friendship_id The friendship ID
     53 * @param int $initiator_user_id The user ID initiating the friendship
     54 * @param int $friend_user_id The user ID not initiating the friendship
     55 */
     56function bp_friends_clear_request_cache_on_save( $friendship_id, $initiator_user_id, $friend_user_id ) {
     57    bp_friends_clear_request_cache( $friend_user_id );
     58}
     59add_action( 'friends_friendship_requested', 'bp_friends_clear_request_cache_on_save', 10, 3 );
     60add_action( 'friends_friendship_accepted',  'bp_friends_clear_request_cache_on_save', 10, 3 );
     61
     62/**
     63 * Clear the friend request cache when a friendship is removed.
     64 *
     65 * A friendship is deemed removed when a friendship is withdrawn or rejected.
     66 *
     67 * @since BuddyPress (2.0.0)
     68 *
     69 * @param int $friendship_id The friendship ID
     70 * @param BP_Friends_Friendship $friendship
     71 */
     72function bp_friends_clear_request_cache_on_remove( $friendship_id, BP_Friends_Friendship $friendship ) {
     73    bp_friends_clear_request_cache( $friendship->friend_user_id ); 
     74}
     75add_action( 'friends_friendship_withdrawn', 'bp_friends_clear_request_cache_on_remove', 10, 2 );
     76add_action( 'friends_friendship_rejected',  'bp_friends_clear_request_cache_on_remove', 10, 2 );
     77
    3478// List actions to clear super cached pages on, if super cache is installed
    3579add_action( 'friends_friendship_rejected',  'bp_core_clear_cache' );
  • trunk/bp-friends/bp-friends-classes.php

    r7860 r8105  
    227227     */
    228228    public static function get_friendship_request_user_ids( $user_id ) {
    229         global $wpdb, $bp;
    230 
    231         return $wpdb->get_col( $wpdb->prepare( "SELECT initiator_user_id FROM {$bp->friends->table_name} WHERE friend_user_id = %d AND is_confirmed = 0", $user_id ) );
     229        $friend_requests = wp_cache_get( $user_id, 'bp_friends_requests' );
     230
     231        if ( false === $friend_requests ) {
     232            global $wpdb, $bp;
     233
     234            $friend_requests = $wpdb->get_col( $wpdb->prepare( "SELECT initiator_user_id FROM {$bp->friends->table_name} WHERE friend_user_id = %d AND is_confirmed = 0", $user_id ) );
     235
     236            wp_cache_set( $user_id, $friend_requests, 'bp_friends_requests' );
     237        }
     238
     239        return $friend_requests;
    232240    }
    233241
Note: See TracChangeset for help on using the changeset viewer.