Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/15/2015 10:44:55 PM (9 years ago)
Author:
r-a-y
Message:

Messages: Cache calls to BP_Messages_Thread class.

This commit:

  • Introduces static method - BP_Messages_Thread::get_messages() - caches fetching messages by thread ID and introduces the 'bp_messages_threads' cache group.
  • Converts BP_Messages_Thread::check_access() and BP_Messages_Thread::is_valid() methods from using direct DB queries to reference the cache.
  • Includes unit tests.
  • Consolidates cache clearing functions in bp-messages-cache.php.

This handles the majority of uncached message DB queries. For the inbox
page, if there are 10 message threads in the loop, we save 20 DB queries.
On a single message thread, we save 5 DB queries.

Fixes #6221.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-messages/classes/class-bp_messages-thread.php

    r9752 r9753  
    148148        $this->thread_id      = $thread_id;
    149149
    150         $bp = buddypress();
    151 
    152         if ( !$this->messages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d ORDER BY date_sent " . $order, $this->thread_id ) ) ) {
     150        // get messages for thread
     151        $this->messages = self::get_messages( $thread_id );
     152
     153        if ( empty( $this->messages ) || is_wp_error( $this->messages ) ) {
    153154            return false;
     155        }
     156
     157        // flip if order is DESC
     158        if ( 'DESC' === $order ) {
     159            $this->messages = array_reverse( $this->messages );
    154160        }
    155161
     
    245251
    246252    /** Static Functions ******************************************************/
     253
     254    /**
     255     * Get all messages associated with a thread.
     256     *
     257     * @since BuddyPress (2.3.0)
     258     *
     259     * @param int $thread_id The message thread ID
     260     * @return array
     261     */
     262    public static function get_messages( $thread_id = 0 ) {
     263        $messages = wp_cache_get( $thread_id, 'bp_messages_threads' );
     264
     265        if ( false === $messages ) {
     266            global $wpdb;
     267
     268            $bp = buddypress();
     269
     270            // always sort by ASC by default
     271            $messages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d ORDER BY date_sent ASC", $thread_id ) );
     272
     273            wp_cache_set( $thread_id, (array) $messages, 'bp_messages_threads' );
     274        }
     275
     276        return $messages;
     277    }
    247278
    248279    /**
     
    664695     * @param int $thread_id The message thread ID.
    665696     * @param int $user_id The user ID.
    666      * @return int The message ID on success.
     697     * @return int|null The recorded recipient ID on success, null on failure
    667698     */
    668699    public static function check_access( $thread_id, $user_id = 0 ) {
    669         global $wpdb;
    670 
    671         if ( empty( $user_id ) )
     700        if ( empty( $user_id ) ) {
    672701            $user_id = bp_loggedin_user_id();
    673 
    674         $bp = buddypress();
    675 
    676         return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d AND is_deleted = 0 AND user_id = %d", $thread_id, $user_id ) );
     702        }
     703
     704        $recipients = self::get_recipients_for_thread( $thread_id );
     705
     706        if ( isset( $recipients[$user_id] ) && 0 == $recipients[$user_id]->is_deleted ) {
     707            return $recipients[$user_id]->id;
     708        } else {
     709            return null;
     710        }
    677711    }
    678712
     
    683717     *
    684718     * @param int $thread_id The message thread ID.
    685      * @return int The message thread ID on success.
     719     * @return int|null The message thread ID on success, null on failure
    686720     */
    687721    public static function is_valid( $thread_id = 0 ) {
    688         global $wpdb;
    689 
    690722        // Bail if no thread ID is passed
    691723        if ( empty( $thread_id ) ) {
     
    693725        }
    694726
    695         $bp = buddypress();
    696 
    697         return $wpdb->get_var( $wpdb->prepare( "SELECT thread_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d LIMIT 1", $thread_id ) );
     727        $thread = self::get_messages( $thread_id );
     728
     729        if ( ! empty( $thread ) ) {
     730            return $thread_id;
     731        } else {
     732            return null;
     733        }
    698734    }
    699735
Note: See TracChangeset for help on using the changeset viewer.