Skip to:
Content

BuddyPress.org

Changeset 11352


Ignore:
Timestamp:
12/29/2016 02:16:02 AM (8 years ago)
Author:
boonebgorges
Message:

XProfile: Add caching for fetching a field ID by name.

Fixes #7404.

Location:
trunk
Files:
3 edited

Legend:

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

    r10434 r11352  
    307307add_action( 'xprofile_field_after_save', 'bp_xprofile_clear_field_cache' );
    308308
     309/**
     310 * Clear the field-name cache.
     311 *
     312 * @since 2.8.0
     313 */
     314function bp_xprofile_reset_fields_by_name_cache_incrementor() {
     315    bp_core_reset_incrementor( 'bp_xprofile_fields_by_name' );
     316}
     317add_action( 'xprofile_field_before_save', 'bp_xprofile_reset_fields_by_name_cache_incrementor' );
     318
    309319// List actions to clear super cached pages on, if super cache is installed.
    310320add_action( 'xprofile_updated_profile', 'bp_core_clear_cache' );
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php

    r11316 r11352  
    918918        }
    919919
    920         $sql = $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s AND parent_id = 0", $field_name );
    921 
    922         $query = $wpdb->get_var( $sql );
    923 
    924         return is_numeric( $query ) ? (int) $query : $query;
     920        $id = bp_core_get_incremented_cache( $field_name, 'bp_xprofile_fields_by_name' );
     921        if ( false === $id ) {
     922            $sql = $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s AND parent_id = 0", $field_name );
     923            $id = $wpdb->get_var( $sql );
     924            bp_core_set_incremented_cache( $field_name, 'bp_xprofile_fields_by_name', $id );
     925        }
     926
     927        return is_numeric( $id ) ? (int) $id : $id;
    925928    }
    926929
  • trunk/tests/phpunit/testcases/xprofile/cache.php

    r10198 r11352  
    165165        $this->assertSame( 'Bar', $field_2->name );
    166166    }
     167
     168    /**
     169     * @ticket BP7407
     170     */
     171    public function test_get_field_id_from_name_should_be_cached() {
     172        global $wpdb;
     173
     174        $g = $this->factory->xprofile_group->create();
     175        $f = $this->factory->xprofile_field->create( array(
     176            'field_group_id' => $g,
     177            'name' => 'Foo',
     178        ) );
     179
     180        // Prime cache.
     181        BP_XProfile_Field::get_id_from_name( 'Foo' );
     182
     183        $num_queries = $wpdb->num_queries;
     184
     185        $this->assertSame( $f, BP_XProfile_Field::get_id_from_name( 'Foo' ) );
     186        $this->assertSame( $num_queries, $wpdb->num_queries );
     187    }
     188
     189    /**
     190     * @ticket BP7407
     191     */
     192    public function test_get_field_id_from_name_cache_should_be_invalidated_on_field_update() {
     193        $g = $this->factory->xprofile_group->create();
     194        $f1 = $this->factory->xprofile_field->create( array(
     195            'field_group_id' => $g,
     196            'name' => 'Foo',
     197        ) );
     198        $f2 = $this->factory->xprofile_field->create( array(
     199            'field_group_id' => $g,
     200            'name' => 'Bar',
     201        ) );
     202
     203        // Prime cache.
     204        xprofile_get_field_id_from_name( 'Foo' );
     205        xprofile_get_field_id_from_name( 'Bar' );
     206
     207        // Free up the name 'Bar'.
     208        $field2 = xprofile_get_field( $f2 );
     209        $field2->name = 'Quz';
     210        $field2->save();
     211
     212        // Take the name 'Bar'.
     213        $field1 = xprofile_get_field( $f1 );
     214        $field1->name = 'Bar';
     215        $field1->save();
     216
     217        $this->assertSame( $f1, BP_XProfile_Field::get_id_from_name( 'Bar' ) );
     218    }
     219
     220    /**
     221     * @ticket BP7407
     222     */
     223    public function test_get_field_id_from_name_cache_should_be_invalidated_on_field_deletion() {
     224        $g = $this->factory->xprofile_group->create();
     225        $f = $this->factory->xprofile_field->create( array(
     226            'field_group_id' => $g,
     227            'name' => 'Foo',
     228        ) );
     229
     230        // Prime cache.
     231        xprofile_get_field_id_from_name( 'Foo' );
     232
     233        xprofile_delete_field( $f );
     234
     235        $this->assertNull( BP_XProfile_Field::get_id_from_name( 'Bar' ) );
     236    }
    167237}
Note: See TracChangeset for help on using the changeset viewer.