Skip to:
Content

BuddyPress.org

Changeset 8053


Ignore:
Timestamp:
03/06/2014 01:28:03 AM (10 years ago)
Author:
r-a-y
Message:

Cache the unread inbox count for a user.

This commit:

  • Caches calls to BP_Messages_Thread::get_inbox_count()
  • Invalidates the cache when a new message is sent or deleted
  • Adds a unit test

See #5414

Location:
trunk
Files:
1 added
2 edited

Legend:

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

    r7994 r8053  
    2525
    2626/**
     27 * Clears unread count cache for each recipient after a message is sent.
     28 *
     29 * @since BuddyPress (2.0.0)
     30 *
     31 * @param BP_Messages_Message $message
     32 */
     33function bp_messages_clear_unread_count_cache_on_message_save( BP_Messages_Message $message ) {
     34    foreach ( (array) $message->recipients as $recipient ) {
     35        wp_cache_delete( $recipient->user_id, 'bp_messages_unread_count' );
     36    }
     37}
     38add_action( 'messages_message_after_save', 'bp_messages_clear_unread_count_cache_on_message_save' );
     39
     40/**
     41 * Clears unread count cache for the logged-in user after a message is deleted.
     42 *
     43 * @since BuddyPress (2.0.0)
     44 *
     45 * @param int|array $thread_ids If single thread, the thread ID. Otherwise, an
     46 *  array of thread IDs
     47 */
     48function bp_messages_clear_unread_count_cache_on_message_delete( $thread_ids ) {
     49    wp_cache_delete( bp_loggedin_user_id(), 'bp_messages_unread_count' );
     50}
     51add_action( 'messages_before_delete_thread', 'bp_messages_clear_unread_count_cache_on_message_delete' );
     52
     53/**
    2754 * Invalidates cache for notices.
    2855 *
  • trunk/bp-messages/bp-messages-classes.php

    r8049 r8053  
    301301        $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 0 WHERE user_id = %d AND thread_id = %d", bp_loggedin_user_id(), $thread_id );
    302302        $wpdb->query($sql);
     303
     304        wp_cache_delete( bp_loggedin_user_id(), 'bp_messages_unread_count' );
    303305    }
    304306
     
    315317        $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 1 WHERE user_id = %d AND thread_id = %d", bp_loggedin_user_id(), $thread_id );
    316318        $wpdb->query($sql);
     319
     320        wp_cache_delete( bp_loggedin_user_id(), 'bp_messages_unread_count' );
    317321    }
    318322
     
    383387
    384388    /**
    385      * Gets the inbox message count for a user.
     389     * Gets the unread message count for a user.
    386390     *
    387391     * @since BuddyPress (1.0.0)
     
    397401        }
    398402
    399         $sql = $wpdb->prepare( "SELECT SUM(unread_count) FROM {$bp->messages->table_name_recipients} WHERE user_id = %d AND is_deleted = 0 AND sender_only = 0", $user_id );
    400         $unread_count = $wpdb->get_var( $sql );
    401 
    402         if ( empty( $unread_count ) || is_wp_error( $unread_count ) ) {
    403             return 0;
     403        $unread_count = wp_cache_get( $user_id, 'bp_messages_unread_count' );
     404
     405        if ( false === $unread_count ) {
     406            $unread_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT SUM(unread_count) FROM {$bp->messages->table_name_recipients} WHERE user_id = %d AND is_deleted = 0 AND sender_only = 0", $user_id ) );
     407
     408            wp_cache_set( $user_id, $unread_count, 'bp_messages_unread_count' );
    404409        }
    405410
Note: See TracChangeset for help on using the changeset viewer.