Skip to:
Content

BuddyPress.org

Changeset 7994


Ignore:
Timestamp:
02/26/2014 09:37:39 PM (10 years ago)
Author:
r-a-y
Message:

Add object caching when fetching the active notice.

This commit:

  • Introduces a new hook - 'messages_notice_before_delete' to do something before a notice is deleted
  • Adds object caching to the BP_Messages_Notice::get_active() method
  • Adds a cache invalidation function to bp-messages-cache.php
  • Adds a unit test

Fixes #5419

Location:
trunk
Files:
2 added
2 edited

Legend:

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

    r6342 r7994  
    2323add_action( 'messages_screen_sentbox', 'bp_core_clear_cache' );
    2424add_action( 'messages_screen_inbox',   'bp_core_clear_cache' );
     25
     26/**
     27 * Invalidates cache for notices.
     28 *
     29 * Currently, invalidates active notice cache.
     30 *
     31 * @since BuddyPress (2.0.0)
     32 */
     33function bp_notices_clear_cache( $notice ) {
     34    wp_cache_delete( 'active_notice', 'bp_messages' );
     35}
     36add_action( 'messages_notice_after_save',    'bp_notices_clear_cache' );
     37add_action( 'messages_notice_before_delete', 'bp_notices_clear_cache' );
  • trunk/bp-messages/bp-messages-classes.php

    r7969 r7994  
    578578        global $wpdb, $bp;
    579579
     580        do_action( 'messages_notice_before_delete', $this );
     581
    580582        $sql = $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_notices} WHERE id = %d", $this->id );
    581583
     
    644646     */
    645647    public static function get_active() {
    646         global $wpdb, $bp;
    647 
    648         $notice_id = $wpdb->get_var( "SELECT id FROM {$bp->messages->table_name_notices} WHERE is_active = 1" );
    649 
    650         return new BP_Messages_Notice( $notice_id );
     648        $notice = wp_cache_get( 'active_notice', 'bp_messages' );
     649
     650        if ( false === $notice ) {
     651            global $wpdb, $bp;
     652
     653            $notice_id = $wpdb->get_var( "SELECT id FROM {$bp->messages->table_name_notices} WHERE is_active = 1" );
     654            $notice    = new BP_Messages_Notice( $notice_id );
     655
     656            wp_cache_set( 'active_notice', $notice, 'bp_messages' );
     657        }
     658
     659        return $notice;
    651660    }
    652661}
Note: See TracChangeset for help on using the changeset viewer.