Skip to:
Content

BuddyPress.org

Changeset 9336


Ignore:
Timestamp:
01/09/2015 08:38:37 PM (6 years ago)
Author:
johnjamesjacoby
Message:

XProfile data cache group updates:

  • Switches from dynamic cache group to unique key for $user_id:$field_id.
  • Introduces bp_xprofile_get_non_cached_field_ids() as a helper, based on bp_get_non_cached_field_ids().
  • Registers bp_xprofile and bp_xprofile_data as global cache groups.
  • Updates unit tests to pass with new cache keys.

Props r-a-y. Fixes #6100. See #5733.

Location:
trunk
Files:
5 edited

Legend:

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

    r9318 r9336  
    1212// Exit if accessed directly
    1313if ( !defined( 'ABSPATH' ) ) exit;
     14
     15/**
     16 * Determine which xprofile fields do not have cached values for a user.
     17 *
     18 * @since BuddyPress (2.2.0)
     19 *
     20 * @param int   $user_id   User ID to check
     21 * @param array $field_ids XProfile field IDs.
     22 * @return array
     23 */
     24function bp_xprofile_get_non_cached_field_ids( $user_id = 0, $field_ids = array() ) {
     25    $uncached_fields = array();
     26
     27    foreach ( $field_ids as $field_id ) {
     28        $field_id  = (int) $field_id;
     29        $cache_key = "{$user_id}:{$field_id}";
     30        if ( false === wp_cache_get( $cache_key, 'bp_xprofile_data' ) ) {
     31            $uncached_fields[] = $field_id;
     32        }
     33    }
     34
     35    return $uncached_fields;
     36}
    1437
    1538/**
     
    4164        'group' => array(),
    4265        'field' => array(),
    43         'data'   => array(),
     66        'data'  => array(),
    4467    );
    4568
     
    171194 */
    172195function xprofile_clear_profiledata_object_cache( $data_obj ) {
    173     wp_cache_delete( $data_obj->field_id, 'bp_xprofile_data_' . $data_obj->user_id );
    174 }
    175 add_action( 'xprofile_data_after_save', 'xprofile_clear_profiledata_object_cache' );
     196    wp_cache_delete( "{$data_obj->user_id}:{$data_obj->field_id}", 'bp_xprofile_data' );
     197}
     198add_action( 'xprofile_data_after_save',   'xprofile_clear_profiledata_object_cache' );
    176199add_action( 'xprofile_data_after_delete', 'xprofile_clear_profiledata_object_cache' );
    177200
  • trunk/src/bp-xprofile/bp-xprofile-classes.php

    r9329 r9336  
    12231223        global $wpdb, $bp;
    12241224
    1225         $cache_group = 'bp_xprofile_data_' . $user_id;
    1226         $profiledata = wp_cache_get( $field_id, $cache_group );
     1225        $cache_key   = "{$user_id}:{$field_id}";
     1226        $profiledata = wp_cache_get( $cache_key, 'bp_xprofile_data' );
    12271227
    12281228        if ( false === $profiledata ) {
    1229             $sql = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_id );
     1229            $sql         = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_id );
    12301230            $profiledata = $wpdb->get_row( $sql );
    12311231
    12321232            if ( $profiledata ) {
    1233                 wp_cache_set( $field_id, $profiledata, $cache_group );
     1233                wp_cache_set( $cache_key, $profiledata, 'bp_xprofile_data' );
    12341234            }
    12351235        }
     
    12601260
    12611261        // Check cache first
    1262         $cached = wp_cache_get( $this->field_id, 'bp_xprofile_data_' . $this->user_id );
     1262        $cache_key = "{$this->user_id}:{$this->field_id}";
     1263        $cached    = wp_cache_get( $cache_key, 'bp_xprofile_data' );
    12631264
    12641265        if ( $cached && ! empty( $cached->id ) ) {
     
    14011402        $data = array();
    14021403
    1403         $cache_group = 'bp_xprofile_data_' . $user_id;
    1404 
    1405         $uncached_field_ids = bp_get_non_cached_ids( $field_ids, $cache_group );
     1404        $uncached_field_ids = bp_xprofile_get_non_cached_field_ids( $user_id, $field_ids, 'bp_xprofile_data' );
    14061405
    14071406        // Prime the cache
     
    14271426            foreach ( $uncached_field_ids as $field_id ) {
    14281427
     1428                $cache_key = "{$user_id}:{$field_id}";
     1429
    14291430                // If a value was found, cache it
    14301431                if ( isset( $queried_data[ $field_id ] ) ) {
    1431                     wp_cache_set( $field_id, $queried_data[ $field_id ], $cache_group );
     1432                    wp_cache_set( $cache_key, $queried_data[ $field_id ], 'bp_xprofile_data' );
    14321433
    14331434                // If no value was found, cache an empty item
     
    14411442                    $d->last_updated = '';
    14421443
    1443                     wp_cache_set( $field_id, $d, $cache_group );
     1444                    wp_cache_set( $cache_key, $d, 'bp_xprofile_data' );
    14441445                }
    14451446            }
     
    14481449        // Now that all items are cached, fetch them
    14491450        foreach ( $field_ids as $field_id ) {
    1450             $data[] = wp_cache_get( $field_id, $cache_group );
     1451            $cache_key = "{$user_id}:{$field_id}";
     1452            $data[]    = wp_cache_get( $cache_key, 'bp_xprofile_data' );
    14511453        }
    14521454
     
    14611463     */
    14621464    public static function get_all_for_user( $user_id ) {
    1463         global $wpdb, $bp;
    14641465
    14651466        $groups = bp_xprofile_get_groups( array(
     
    15151516
    15161517            // Check cache first
    1517             $fielddata = wp_cache_get( $field_id, 'bp_xprofile_data_' . $user_id );
     1518            $cache_key = "{$user_id}:{$field_id}";
     1519            $fielddata = wp_cache_get( $cache_key, 'bp_xprofile_data' );
    15181520            if ( false === $fielddata || empty( $fielddata->id ) ) {
    15191521                $fielddata_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_id ) );
     
    15521554        $uncached_ids = array();
    15531555        foreach ( $user_ids as $user_id ) {
    1554             if ( false === wp_cache_get( $field_id, 'bp_xprofile_data_' . $user_id ) ) {
     1556            $cache_key = "{$user_id}:{$field_id}";
     1557            if ( false === wp_cache_get( $cache_key, 'bp_xprofile_data' ) ) {
    15551558                $uncached_ids[] = $user_id;
    15561559            }
     
    15841587                }
    15851588
    1586                 wp_cache_set( $field_id, $d, 'bp_xprofile_data_' . $d->user_id );
     1589                $cache_key = "{$d->user_id}:{$field_id}";
     1590                wp_cache_set( $cache_key, $d, 'bp_xprofile_data' );
    15871591            }
    15881592        }
     
    15911595        $data = array();
    15921596        foreach ( $user_ids as $user_id ) {
    1593             $data[] = wp_cache_get( $field_id, 'bp_xprofile_data_' . $user_id );
     1597            $cache_key = "{$user_id}:{$field_id}";
     1598            $data[]    = wp_cache_get( $cache_key, 'bp_xprofile_data' );
    15941599        }
    15951600
  • trunk/src/bp-xprofile/bp-xprofile-loader.php

    r9289 r9336  
    354354
    355355    /**
     356     * Setup cache groups
     357     *
     358     * @since BuddyPress (2.2.0)
     359     */
     360    public function setup_cache_groups() {
     361
     362        // Global groups
     363        wp_cache_add_global_groups( array(
     364            'bp_xprofile',
     365            'bp_xprofile_data',
     366            //'xprofile_meta'
     367        ) );
     368
     369        parent::setup_cache_groups();
     370    }
     371
     372    /**
    356373     * Adds "Settings > Profile" subnav item under the "Settings" adminbar menu.
    357374     *
  • trunk/tests/phpunit/testcases/members/functions.php

    r9234 r9336  
    184184        $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE user_id = %d AND field_id = 1", $u ) );
    185185        wp_cache_delete( 'bp_user_fullname_' . $u, 'bp' );
    186         wp_cache_delete( 1, 'bp_xprofile_data_' . $u, 'bp' );
     186        wp_cache_delete( "{$u}:1", 'bp_xprofile_data', 'bp' );
    187187
    188188        $this->assertSame( '', xprofile_get_field_data( 1, $u ) );
  • trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-profiledata.php

    r9139 r9336  
    3737        $d = new BP_XProfile_ProfileData( $f, $u );
    3838
    39         wp_cache_delete( $f, 'bp_xprofile_data_' . $u );
     39        wp_cache_delete( "{$u}:{$f}", 'bp_xprofile_data' );
    4040
    4141        $this->assertTrue( $d->exists() );
     
    5757        $c = new stdClass;
    5858        $c->id = 3;
    59         wp_cache_set( $f, $c, 'bp_xprofile_data_' . $u );
     59        wp_cache_set( "{$u}:{$f}", $c, 'bp_xprofile_data' );
    6060
    6161        $this->assertTrue( $d->exists() );
     
    7474
    7575        // Just to be sure
    76         wp_cache_delete( $f, 'bp_xprofile_data_' . $u );
     76        wp_cache_delete( "{$u}:{$f}", 'bp_xprofile_data' );
    7777
    7878        $this->assertEquals( 0, BP_XProfile_ProfileData::get_fielddataid_byid( $f, $u ) );
     
    9797
    9898        // Ensure it's deleted from cache
    99         wp_cache_delete( $f, 'bp_xprofile_data_' . $u );
     99        wp_cache_delete( "{$u}:{$f}", 'bp_xprofile_data' );
    100100
    101101        $this->assertEquals( $d->id, BP_XProfile_ProfileData::get_fielddataid_byid( $f, $u ) );
     
    116116        $d = new stdClass;
    117117        $d->id = 5;
    118         wp_cache_set( $f, $d, 'bp_xprofile_data_' . $u );
     118        wp_cache_set( "{$u}:{$f}", $d, 'bp_xprofile_data' );
    119119
    120120        $this->assertSame( 5, BP_XProfile_ProfileData::get_fielddataid_byid( $f, $u ) );
     
    139139
    140140        // Ensure it's deleted from cache
    141         wp_cache_delete( $f, 'bp_xprofile_data_' . $u );
     141        wp_cache_delete( "{$u}:{$f}", 'bp_xprofile_data' );
    142142
    143143        $this->assertSame( 'foo', BP_XProfile_ProfileData::get_value_byid( $f, $u ) );
     
    180180
    181181        // Ensure it's deleted from cache
    182         wp_cache_delete( $f, 'bp_xprofile_data_' . $u1 );
    183         wp_cache_delete( $f, 'bp_xprofile_data_' . $u2 );
     182        wp_cache_delete( "{$u1}:{$f}", 'bp_xprofile_data' );
     183        wp_cache_delete( "{$u2}:{$f}", 'bp_xprofile_data' );
    184184
    185185        $eu1 = new stdClass;
     
    219219        $d->value = 'foo';
    220220        $d->field_id = $f;
    221         wp_cache_set( $f, $d, 'bp_xprofile_data_' . $u );
     221        wp_cache_set( "{$u}:{$f}", $d, 'bp_xprofile_data' );
    222222
    223223        $this->assertSame( 'foo', BP_XProfile_ProfileData::get_value_byid( $f, $u ) );
     
    253253        $d2->last_updated = $time;
    254254
    255         wp_cache_set( $f, $d1, 'bp_xprofile_data_' . $u1 );
    256         wp_cache_set( $f, $d2, 'bp_xprofile_data_' . $u2 );
     255        wp_cache_set( "{$u1}:{$f}", $d1, 'bp_xprofile_data' );
     256        wp_cache_set( "{$u2}:{$f}", $d2, 'bp_xprofile_data' );
    257257
    258258        $eu1 = new stdClass;
     
    314314
    315315        // Ensure it's deleted from cache
    316         wp_cache_delete( $f1, 'bp_xprofile_data_' . $u );
    317         wp_cache_delete( $f2, 'bp_xprofile_data_' . $u );
     316        wp_cache_delete( "{$u}:{$f1}", 'bp_xprofile_data' );
     317        wp_cache_delete( "{$u}:{$f2}", 'bp_xprofile_data' );
    318318
    319319        $u_obj = new WP_User( $u );
     
    390390        $d2->id = 2;
    391391
    392         wp_cache_set( $f1, $d1, 'bp_xprofile_data_' . $u );
    393         wp_cache_set( $f2, $d2, 'bp_xprofile_data_' . $u );
     392        wp_cache_set( "{$u}:{$f1}", $d1, 'bp_xprofile_data' );
     393        wp_cache_set( "{$u}:{$f2}", $d2, 'bp_xprofile_data' );
    394394
    395395        $u_obj = new WP_User( $u );
Note: See TracChangeset for help on using the changeset viewer.