Skip to:
Content

BuddyPress.org

Changeset 7813


Ignore:
Timestamp:
02/06/2014 08:33:50 PM (10 years ago)
Author:
boonebgorges
Message:

Object caching for the more frequently used items in bp-notifications

Several notifications queries are made on every pageload:

  • an unread count to set up navigation
  • an unread count for the toolbar
  • the notification items themselves for the toolbar

These three queries actually result in the identical SQL query, so we can
eliminate two of them by caching the value by the pageload. With persistent
caching, the performance benefits are obviously even greater.

The chosen technique for caching here is 'all_for_user_'. This makes
invalidation fairly straightforward, because we can simply bust the
'all_for_user_x' cache whenever a save or delete takes place on a notification
item that is related to user_x. For more complex parts of the notification
component (such as the notifications page itself), if caching is desired,
it'll probably make sense to split the query and cache individual items.

Fixes #5377

Location:
trunk
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-notifications/bp-notifications-classes.php

    r7711 r7813  
    123123        $data_format = array( '%d', '%d', '%d', '%s', '%s', '%s', '%d' );
    124124
     125        do_action_ref_array( 'bp_notification_before_save', array( &$this ) );
     126
    125127        // Update
    126128        if ( ! empty( $this->id ) ) {
     
    139141            $retval   = $wpdb->insert_id;
    140142        }
     143
     144        do_action_ref_array( 'bp_notification_after_save', array( &$this ) );
    141145
    142146        // Return the result
     
    674678        $where = self::get_query_clauses( $args );
    675679
     680        do_action( 'bp_notification_before_delete', $args );
     681
    676682        return self::_delete( $where['data'], $where['format'] );
    677683    }
  • trunk/bp-notifications/bp-notifications-functions.php

    r7732 r7813  
    122122
    123123    // Setup local variables
    124     $bp                    = buddypress();
    125     $notifications         = BP_Notifications_Notification::get( array(
    126         'user_id' => $user_id
    127     ) );
     124    $bp = buddypress();
     125
     126    // Get notifications out of the cache, or query if necessary
     127    $notifications = wp_cache_get( 'all_for_user_' . $user_id, 'bp_notifications' );
     128    if ( false === $notifications ) {
     129        $notifications = BP_Notifications_Notification::get( array(
     130            'user_id' => $user_id
     131        ) );
     132        wp_cache_set( 'all_for_user_' . $user_id, $notifications, 'bp_notifications' );
     133    }
     134
    128135    $grouped_notifications = array(); // Notification groups
    129136    $renderable            = array(); // Renderable notifications
     
    500507
    501508    // Get the notifications, and count them
    502     $notifications = BP_Notifications_Notification::get( array(
    503         'user_id' => $user_id,
    504     ) );
     509    $notifications = wp_cache_get( 'all_for_user_' . $user_id, 'bp_notifications' );
     510    if ( false === $notifications ) {
     511        $notifications = BP_Notifications_Notification::get( array(
     512            'user_id' => $user_id,
     513        ) );
     514        wp_cache_set( 'all_for_user_' . $user_id, $notifications, 'bp_notifications' );
     515    }
    505516
    506517    $count = ! empty( $notifications ) ? count( $notifications ) : 0;
  • trunk/bp-notifications/bp-notifications-loader.php

    r7758 r7813  
    5050            'template',
    5151            'functions',
     52            'cache',
    5253        );
    5354
Note: See TracChangeset for help on using the changeset viewer.