[buddypress-trac] [BuddyPress Trac] #6220: Cache BP_Messages_Thread::get_recipients() to reduce inbox queries by 20

buddypress-trac noreply at wordpress.org
Fri Feb 13 09:11:11 UTC 2015


#6220: Cache BP_Messages_Thread::get_recipients() to reduce inbox queries by 20
-----------------------------------+-----------------------------
 Reporter:  wpdennis               |      Owner:
     Type:  enhancement            |     Status:  new
 Priority:  normal                 |  Milestone:  Awaiting Review
Component:  Component - Messaging  |    Version:
 Severity:  normal                 |   Keywords:  dev-feedback
-----------------------------------+-----------------------------
 The inbox (and sentbox) calls
 [https://buddypress.trac.wordpress.org/browser/tags/2.2.0/src/bp-messages
 /bp-messages-classes.php#L211 BP_Messages_Thread::get_recipients()] twice
 per conversation in ''messages-loop.php''. Once via
 ''bp_has_message_threads()'' and once via
 ''bp_message_thread_total_and_unread_count()''.

 So, in an inbox/sentbox with 10 conversations, that are 20 queries on each
 page load. I added some caching to ''get_recipients()'', to remove these
 20 queries:

 {{{
 public function get_recipients() {
         $recipients = wp_cache_get( 'recipients-' . $this->thread_id,
 'bp_messages' );

         if ( false === $recipients ) {
                 global $wpdb, $bp;

                 $results = $wpdb->get_results( $wpdb->prepare( "SELECT *
 FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d",
 $this->thread_id ) );

                 foreach ( (array) $results as $recipient ) {
                         $recipients[$recipient->user_id] = $recipient;
                 }

                 wp_cache_set( 'recipients-' . $this->thread_id,
 $recipients, 'bp_messages' );
         }

         /**
          * Filters the recipients of a message thread.
          *
          * @since BuddyPress (2.2.0)
          *
          * @param array $recipients Array of recipient objects.
          * @param int   $thread_id  ID of the current thread.
          */
         return apply_filters( 'bp_messages_thread_get_recipients',
 $recipients, $this->thread_id );
 }
 }}}

 Is there a way to change the recipient in a conversion where we would need
 to add some cache invalidation? I haven't found one, except maybe the
 deletion of an user?

--
Ticket URL: <https://buddypress.trac.wordpress.org/ticket/6220>
BuddyPress Trac <http://buddypress.org/>
BuddyPress Trac


More information about the buddypress-trac mailing list