Skip to:
Content

Changeset 7771


Ignore:
Timestamp:
02/03/2014 02:05:25 AM (4 years ago)
Author:
boonebgorges
Message:

Use persistent cache for individual activity items

Fixes #5350

Location:
trunk/bp-activity
Files:
2 edited

Legend:

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

    r7396 r7771  
    5454    }
    5555}
     56
     57/**
     58 * Clear a cached activity item when that item is updated.
     59 *
     60 * @since 2.0
     61 *
     62 * @param BP_Activity_Activity $activity
     63 */
     64function bp_activity_clear_cache_for_activity( $activity ) {
     65    wp_cache_delete( 'activity_' . $activity->id, 'bp' );
     66}
     67add_action( 'bp_activity_after_save', 'bp_activity_clear_cache_for_activity' );
     68
     69/**
     70 * Clear cached data for deleted activity items.
     71 *
     72 * @since 2.0
     73 *
     74 * @param array $deleted_ids IDs of deleted activity items.
     75 */
     76function bp_activity_clear_cache_for_deleted_activity( $deleted_ids ) {
     77    foreach ( (array) $deleted_ids as $deleted_id ) {
     78        wp_cache_delete( 'activity_' . $deleted_id, 'bp' );
     79    }
     80}
     81add_action( 'bp_activity_deleted_activities', 'bp_activity_clear_cache_for_deleted_activity' );
  • trunk/bp-activity/bp-activity-classes.php

    r7765 r7771  
    138138        global $wpdb, $bp;
    139139
    140         if ( $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE id = %d", $this->id ) ) ) {
     140        $row = wp_cache_get( 'activity_' . $this->id, 'bp' );
     141
     142        if ( false === $row ) {
     143            $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE id = %d", $this->id ) );
     144        }
     145
     146        if ( ! empty( $row ) ) {
    141147            $this->id                = $row->id;
    142148            $this->item_id           = $row->item_id;
     
    477483        $bp = buddypress();
    478484
    479         // Format the activity ID's for use in the query below
    480         $activity_ids_sql = implode( ',', wp_parse_id_list( $activity_ids ) );
    481 
    482         // First fetch data from activity table, preserving order
    483         $activities = $wpdb->get_results( "SELECT * FROM {$bp->activity->table_name} WHERE id IN ({$activity_ids_sql}) ORDER BY FIELD( id, {$activity_ids_sql} )");
     485        $activities = array();
     486        $query_aids = array();
     487
     488        foreach ( $activity_ids as $activity_id ) {
     489
     490            // If cached data is found, use it
     491            if ( $activity_data = wp_cache_get( 'activity_' . $activity_id, 'bp' ) ) {
     492                $activities[ $activity_id ] = $activity_data;
     493
     494            // Otherwise leave a placeholder so we don't lose the order
     495            } else {
     496                $activities[ $activity_id ] = '';
     497
     498                // Add to the list to be queried
     499                $query_aids[] = $activity_id;
     500            }
     501        }
     502
     503        // Fetch activity data from the DB if necessary
     504        if ( ! empty( $query_aids ) ) {
     505            // Format the activity ID's for use in the query below
     506            $query_aids_sql = implode( ',', wp_parse_id_list( $query_aids ) );
     507
     508            // Fetch data from activity table, preserving order
     509            $queried_adata = $wpdb->get_results( "SELECT * FROM {$bp->activity->table_name} WHERE id IN ({$query_aids_sql}) ORDER BY FIELD( id, {$query_aids_sql} )");
     510
     511            // Put that data into the placeholders created earlier,
     512            // and add it to the cache
     513            foreach ( (array) $queried_adata as $adata ) {
     514                $activities[ $adata->id ] = $adata;
     515                wp_cache_set( 'activity_' . $adata->id, $adata, 'bp' );
     516            }
     517        }
     518
     519        // Reset indexes
     520        $activities = array_values( $activities );
    484521
    485522        // Then fetch user data
Note: See TracChangeset for help on using the changeset viewer.