Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/07/2015 02:30:31 PM (9 years ago)
Author:
boonebgorges
Message:

Introduce cache support for xprofile fields.

xprofile_get_field() will now look in the cache before returning a
BP_XProfile_Field object (via the new BP_XProfile_Field::get_instance()),
and will populate the cache if necessary. It's strongly recommended to use
xprofile_get_field() when fetching individual existing fields, instead of
creating new BP_XProfile_Field objects directly.

BP_XProfile_Group::get() now leverages the field cache too. Instead of a
SELECT * query, it fetches a list of matching field IDs SELECT id, and
populates the full field objects from the cache, if available. The fields
property of BP_XProfile_Group objects is now an array of BP_XProfile_Field
objects, not stdClass. See #6358.

Props boonebgorges, r-a-y.
See #6638.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/xprofile/functions.php

    r10143 r10198  
    869869        $this->assertEquals( array( 1, $g1, $g3, $g2 ), wp_list_pluck( $field_groups, 'id' ) );
    870870    }
     871
     872    /**
     873     * @ticket BP6638
     874     */
     875    public function test_xprofile_get_field_should_return_bp_xprofile_field_object() {
     876        global $wpdb;
     877
     878        $g = $this->factory->xprofile_group->create();
     879        $f = $this->factory->xprofile_field->create( array(
     880            'field_group_id' => $g,
     881            'type' => 'selectbox',
     882            'name' => 'Foo',
     883        ) );
     884
     885        $field = xprofile_get_field( $f );
     886
     887        $this->assertTrue( $field instanceof BP_XProfile_Field );
     888    }
     889
     890    /**
     891     * @ticket BP6638
     892     * @group cache
     893     */
     894    public function test_xprofile_get_field_should_prime_field_cache() {
     895        global $wpdb;
     896
     897        $g = $this->factory->xprofile_group->create();
     898        $f = $this->factory->xprofile_field->create( array(
     899            'field_group_id' => $g,
     900            'type' => 'selectbox',
     901            'name' => 'Foo',
     902        ) );
     903
     904        $num_queries = $wpdb->num_queries;
     905
     906        // Prime the cache.
     907        $field_1 = xprofile_get_field( $f );
     908        $num_queries++;
     909        $this->assertSame( $num_queries, $wpdb->num_queries );
     910
     911        // No more queries.
     912        $field_2 = xprofile_get_field( $f );
     913        $this->assertEquals( $field_1, $field_2 );
     914        $this->assertSame( $num_queries, $wpdb->num_queries );
     915    }
    871916}
Note: See TracChangeset for help on using the changeset viewer.