Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/06/2014 01:39:48 AM (11 years ago)
Author:
boonebgorges
Message:

Implement persistent caching in BP_XProfile_ProfileData::get_value_byid()

This change should mean significant performance improvements, as the method
is used a number of places throughout BuddyPress:

  • bp_get_the_profile_field_options() (to check/select the saved values)
  • xprofile_get_field_data()
  • when swapping out blog commenter names with BP display names

Introduces unit tests for the method.

See #1332

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/testcases/xprofile/class-bp-xprofile-profiledata.php

    r7805 r7806  
    118118        $this->assertSame( 5, BP_XProfile_ProfileData::get_fielddataid_byid( $f->id, $u ) );
    119119    }
     120
     121    /**
     122     * @group get_value_byid
     123     */
     124    public function test_get_value_byid_singleuser_uncached() {
     125        $u = $this->create_user();
     126        $g = $this->factory->xprofile_group->create();
     127        $f = $this->factory->xprofile_field->create( array(
     128            'type' => 'textbox',
     129            'field_group_id' => $g->id,
     130        ) );
     131
     132        $d = new BP_XProfile_ProfileData();
     133        $d->user_id = $u;
     134        $d->field_id = $f->id;
     135        $d->value = 'foo';
     136        $d->save();
     137
     138        // Ensure it's deleted from cache
     139        wp_cache_delete( $f->id, 'bp_xprofile_data_' . $u );
     140
     141        $this->assertSame( 'foo', BP_XProfile_ProfileData::get_value_byid( $f->id, $u ) );
     142    }
     143
     144    /**
     145     * @group get_value_byid
     146     */
     147    public function test_get_value_byid_multipleusers_uncached() {
     148        $u1 = $this->create_user();
     149        $u2 = $this->create_user();
     150        $g = $this->factory->xprofile_group->create();
     151        $f = $this->factory->xprofile_field->create( array(
     152            'type' => 'textbox',
     153            'field_group_id' => $g->id,
     154        ) );
     155
     156        $time = bp_core_current_time();
     157
     158        $d1 = new BP_XProfile_ProfileData();
     159        $d1->user_id = $u1;
     160        $d1->field_id = $f->id;
     161        $d1->value = 'foo';
     162        $d1->last_updated = $time;
     163        $d1->save();
     164
     165        $d2 = new BP_XProfile_ProfileData();
     166        $d2->user_id = $u2;
     167        $d2->field_id = $f->id;
     168        $d2->value = 'bar';
     169        $d2->last_updated = $time;
     170        $d2->save();
     171
     172        // Ensure it's deleted from cache
     173        wp_cache_delete( $f->id, 'bp_xprofile_data_' . $u1 );
     174        wp_cache_delete( $f->id, 'bp_xprofile_data_' . $u2 );
     175
     176        $eu1 = new stdClass;
     177        $eu1->user_id = $u1;
     178        $eu1->value = 'foo';
     179        $eu1->id = $d1->id;
     180        $eu1->field_id = $f->id;
     181        $eu1->last_updated = $time;
     182
     183        $eu2 = new stdClass;
     184        $eu2->user_id = $u2;
     185        $eu2->value = 'bar';
     186        $eu2->id = $d2->id;
     187        $eu2->field_id = $f->id;
     188        $eu2->last_updated = $time;
     189
     190        $expected = array( $eu1, $eu2 );
     191
     192        $this->assertEquals( $expected, BP_XProfile_ProfileData::get_value_byid( $f->id, array( $u1, $u2 ) ) );
     193    }
     194
     195    /**
     196     * @group get_value_byid
     197     */
     198    public function test_get_value_byid_singleuser_cached() {
     199        $u = $this->create_user();
     200        $g = $this->factory->xprofile_group->create();
     201        $f = $this->factory->xprofile_field->create( array(
     202            'type' => 'textbox',
     203            'field_group_id' => $g->id,
     204        ) );
     205
     206        $time = bp_core_current_time();
     207
     208        // Fake the cache
     209        $d = new stdClass;
     210        $d->value = 'foo';
     211        $d->field_id = $f->id;
     212        wp_cache_set( $f->id, $d, 'bp_xprofile_data_' . $u );
     213
     214        $this->assertSame( 'foo', BP_XProfile_ProfileData::get_value_byid( $f->id, $u ) );
     215    }
     216
     217    /**
     218     * @group get_value_byid
     219     */
     220    public function test_get_value_byid_multipleusers_cached() {
     221        $u1 = $this->create_user();
     222        $u2 = $this->create_user();
     223        $g = $this->factory->xprofile_group->create();
     224        $f = $this->factory->xprofile_field->create( array(
     225            'type' => 'textbox',
     226            'field_group_id' => $g->id,
     227        ) );
     228
     229        // Fake the cache
     230        $d1 = new stdClass;
     231        $d1->id = 10;
     232        $d1->user_id = $u1;
     233        $d1->field_id = $f->id;
     234        $d1->value = 'foo';
     235        $d1->last_updated = $time;
     236
     237        $d2 = new stdClass;
     238        $d1->id = 21;
     239        $d2->user_id = $u2;
     240        $d2->field_id = $f->id;
     241        $d2->value = 'bar';
     242        $d2->last_updated = $time;
     243
     244        wp_cache_set( $f->id, $d1, 'bp_xprofile_data_' . $u1 );
     245        wp_cache_set( $f->id, $d2, 'bp_xprofile_data_' . $u2 );
     246
     247        $eu1 = new stdClass;
     248        $eu1->id = 10;
     249        $eu1->user_id = $u1;
     250        $eu1->field_id = $f->id;
     251        $eu1->value = 'foo';
     252        $eu1->last_updated = $time;
     253
     254        $eu2 = new stdClass;
     255        $eu1->id = 21;
     256        $eu2->user_id = $u2;
     257        $eu2->field_id = $f->id;
     258        $eu2->value = 'bar';
     259        $eu2->last_updated = $time;
     260
     261        $expected = array( $eu1, $eu2 );
     262
     263        $this->assertEquals( $expected, BP_XProfile_ProfileData::get_value_byid( $f->id, array( $u1, $u2 ) ) );
     264    }
     265
    120266}
Note: See TracChangeset for help on using the changeset viewer.