Skip to:
Content

BuddyPress.org

Changeset 7805


Ignore:
Timestamp:
02/05/2014 10:53:30 PM (11 years ago)
Author:
boonebgorges
Message:

Consult cache before querying in BP_XProfile_ProfileData::get_fielddataid_byid()

See #1332

Location:
trunk
Files:
2 edited

Legend:

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

    r7792 r7805  
    13091309
    13101310    /**
    1311      * Get the user's field data id by the id of the xprofile field
     1311     * Get the user's field data id by the id of the xprofile field.
    13121312     *
    13131313     * @param int $field_id
     
    13211321            $fielddata_id = 0;
    13221322        } else {
    1323             $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 ) );
     1323
     1324            // Check cache first
     1325            $fielddata = wp_cache_get( $field_id, 'bp_xprofile_data_' . $user_id );
     1326            if ( false === $fielddata || empty( $fielddata->id ) ) {
     1327                $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 ) );
     1328            } else {
     1329                $fielddata_id = $fielddata->id;
     1330            }
    13241331        }
    13251332
  • trunk/tests/testcases/xprofile/class-bp-xprofile-profiledata.php

    r7792 r7805  
    5959        $this->assertTrue( $d->exists() );
    6060    }
     61
     62    /**
     63     * @group get_fielddataid_byid
     64     */
     65    public function test_get_fielddataid_byid_when_doesnt_exist() {
     66        $u = $this->create_user();
     67        $g = $this->factory->xprofile_group->create();
     68        $f = $this->factory->xprofile_field->create( array(
     69            'type' => 'textbox',
     70            'field_group_id' => $g->id,
     71        ) );
     72
     73        // Just to be sure
     74        wp_cache_delete( $f->id, 'bp_xprofile_data_' . $u );
     75
     76        $this->assertEquals( 0, BP_XProfile_ProfileData::get_fielddataid_byid( $f->id, $u ) );
     77    }
     78
     79    /**
     80     * @group get_fielddataid_byid
     81     */
     82    public function test_get_fielddataid_byid_when_exists_uncached() {
     83        $u = $this->create_user();
     84        $g = $this->factory->xprofile_group->create();
     85        $f = $this->factory->xprofile_field->create( array(
     86            'type' => 'textbox',
     87            'field_group_id' => $g->id,
     88        ) );
     89
     90        $d = new BP_XProfile_ProfileData();
     91        $d->user_id = $u;
     92        $d->field_id = $f->id;
     93        $d->value = 'foo';
     94        $d->save();
     95
     96        // Ensure it's deleted from cache
     97        wp_cache_delete( $f->id, 'bp_xprofile_data_' . $u );
     98
     99        $this->assertEquals( $d->id, BP_XProfile_ProfileData::get_fielddataid_byid( $f->id, $u ) );
     100    }
     101
     102    /**
     103     * @group get_fielddataid_byid
     104     */
     105    public function test_get_fielddataid_byid_when_exists_in_cache() {
     106        $u = $this->create_user();
     107        $g = $this->factory->xprofile_group->create();
     108        $f = $this->factory->xprofile_field->create( array(
     109            'type' => 'textbox',
     110            'field_group_id' => $g->id,
     111        ) );
     112
     113        // Fake the cache
     114        $d = new stdClass;
     115        $d->id = 5;
     116        wp_cache_set( $f->id, $d, 'bp_xprofile_data_' . $u );
     117
     118        $this->assertSame( 5, BP_XProfile_ProfileData::get_fielddataid_byid( $f->id, $u ) );
     119    }
    61120}
Note: See TracChangeset for help on using the changeset viewer.