Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/05/2014 06:11:16 PM (11 years ago)
Author:
r-a-y
Message:

Add object caching when fetching a single user's last activity.

Previously, BP_Core_User::get_last_activity() was not being cached.
This resulted in an unnecessary query being run on every page load due
to the usage of bp_core_record_activity() on the 'wp_head' action.

This commit caches calls to BP_Core_User::get_last_activity() when a
single user ID is passed and resets the cache when the user's last
activity is updated on BP_Core_User::update_last_activity().

See #5128

File:
1 edited

Legend:

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

    r8025 r8047  
    13401340        }
    13411341
     1342        // get cache for single user only
     1343        if ( ! is_array( $user_id ) ) {
     1344            $cache = wp_cache_get( $user_id, 'bp_last_activity' );
     1345
     1346            if ( false !== $cache ) {
     1347                return $cache;
     1348            }
     1349        }
     1350
    13421351        $bp = buddypress();
    13431352
     
    13771386        $table_name = buddypress()->members->table_name_last_activity;
    13781387
    1379         $existing = self::get_last_activity( $user_id );
    1380 
    1381         if ( ! empty( $existing ) ) {
    1382             $data = array(
    1383                 'date_recorded' => $time,
    1384             );
    1385 
    1386             $data_format = array(
    1387                 '%s',
    1388             );
    1389 
    1390             $where = array(
    1391             );
    1392 
    1393             $where_format = array(
    1394                 '%d',
    1395             );
    1396 
     1388        $activity = self::get_last_activity( $user_id );
     1389
     1390        if ( ! empty( $activity ) ) {
    13971391            $updated = $wpdb->update(
    13981392                $table_name,
     
    14051399                // WHERE
    14061400                array(
    1407                     'id' => $existing[ $user_id ]['activity_id'],
     1401                    'id' => $activity[ $user_id ]['activity_id'],
    14081402                ),
    14091403
     
    14181412                )
    14191413            );
     1414
     1415            // add new date to existing activity entry for caching
     1416            $activity[ $user_id ]['date_recorded'] = $time;
     1417
    14201418        } else {
    14211419            $updated = $wpdb->insert(
     
    14461444                )
    14471445            );
    1448         }
     1446
     1447            // setup activity array for caching
     1448            // view the foreach loop in the get_last_activity() method for format
     1449            $activity = array();
     1450            $activity[ $user_id ] = array(
     1451                'user_id'       => $user_id,
     1452                'date_recorded' => $time,
     1453                'activity_id'   => $wpdb->insert_id,
     1454            );
     1455        }
     1456
     1457        // set cache
     1458        wp_cache_set( $user_id, $activity, 'bp_last_activity' );
    14491459
    14501460        return $updated;
     
    14821492            )
    14831493        );
     1494
     1495        wp_cache_delete( $user_id, 'bp_last_activity' );
    14841496
    14851497        return $deleted;
Note: See TracChangeset for help on using the changeset viewer.