Skip to:
Content

BuddyPress.org

Changeset 11851


Ignore:
Timestamp:
02/08/2018 03:20:59 PM (6 years ago)
Author:
boonebgorges
Message:

Notifications: User count query should be separately fetched and cached.

Previously, notification counts were calculated by fetching the entire
notification objects.

Props m_uysl.
See #7130.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-notifications/bp-notifications-cache.php

    r10401 r11851  
    4444function bp_notifications_clear_all_for_user_cache( $user_id = 0 ) {
    4545    wp_cache_delete( 'all_for_user_' . $user_id, 'bp_notifications' );
     46    wp_cache_delete( $user_id, 'bp_notifications_unread_count' );
    4647}
    4748
  • trunk/src/bp-notifications/bp-notifications-functions.php

    r11447 r11851  
    608608 */
    609609function bp_notifications_get_unread_notification_count( $user_id = 0 ) {
    610     $notifications = bp_notifications_get_all_notifications_for_user( $user_id );
    611     $count         = ! empty( $notifications ) ? count( $notifications ) : 0;
     610    if ( empty( $user_id ) ) {
     611        $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
     612    }
     613
     614    $count = wp_cache_get( $user_id, 'bp_notifications_unread_count' );
     615    if ( false === $count ) {
     616        $count = BP_Notifications_Notification::get_total_count( array(
     617            'user_id' => $user_id,
     618            'is_new'  => true,
     619        ) );
     620        wp_cache_set( $user_id, $count, 'bp_notifications_unread_count' );
     621    }
    612622
    613623    /**
  • trunk/tests/phpunit/testcases/notifications/functions.php

    r11737 r11851  
    3636
    3737        $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) );
     38        $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) );
    3839    }
    3940
     
    6263
    6364        $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) );
     65        $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) );
    6466    }
    6567
     
    9294
    9395        $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) );
     96        $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) );
    9497    }
    9598
     
    122125
    123126        $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) );
     127        $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) );
    124128    }
    125129
Note: See TracChangeset for help on using the changeset viewer.