Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/15/2015 10:44:55 PM (10 years ago)
Author:
r-a-y
Message:

Messages: Cache calls to BP_Messages_Thread class.

This commit:

  • Introduces static method - BP_Messages_Thread::get_messages() - caches fetching messages by thread ID and introduces the 'bp_messages_threads' cache group.
  • Converts BP_Messages_Thread::check_access() and BP_Messages_Thread::is_valid() methods from using direct DB queries to reference the cache.
  • Includes unit tests.
  • Consolidates cache clearing functions in bp-messages-cache.php.

This handles the majority of uncached message DB queries. For the inbox
page, if there are 10 message threads in the loop, we save 20 DB queries.
On a single message thread, we save 5 DB queries.

Fixes #6221.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/messages/class.bp-messages-thread.php

    r9714 r9753  
    55 */
    66class BP_Tests_BP_Messages_Thread extends BP_UnitTestCase {
     7
     8    /**
     9     * @group cache
     10     */
     11    public function test_construct_cache() {
     12        $u1 = $this->factory->user->create();
     13        $u2 = $this->factory->user->create();
     14
     15        $t1 = $this->factory->message->create( array(
     16            'sender_id' => $u1,
     17            'recipients' => array( $u2 ),
     18            'subject' => 'Foo',
     19        ) );
     20
     21        // prime cache
     22        new BP_Messages_Thread( $t1 );
     23
     24        // Cache should exist
     25        $this->assertThat(
     26            wp_cache_get( $t1, 'bp_messages_threads' ),
     27            $this->logicalNot( $this->equalTo( false ) ),
     28            'Message thread cache should exist.'
     29        );
     30    }
     31
     32    /**
     33     * @group order
     34     */
     35    public function test_construct_order_desc() {
     36        $u1 = $this->factory->user->create();
     37        $u2 = $this->factory->user->create();
     38
     39        // create thread
     40        $t1 = $this->factory->message->create( array(
     41            'sender_id' => $u1,
     42            'recipients' => array( $u2 ),
     43            'subject' => 'Foo',
     44        ) );
     45        // save message ID
     46        $thread = new BP_Messages_Thread( $t1 );
     47        $m1 = wp_list_pluck( $thread->messages, 'id' );
     48        $m1 = array_pop( $m1 );
     49
     50        // create reply
     51        $t2 = $this->factory->message->create( array(
     52            'thread_id' => $t1,
     53            'sender_id' => $u1,
     54            'recipients' => array( $u2 ),
     55            'content' => 'Bar'
     56        ) );
     57        // save message ID
     58        $thread = new BP_Messages_Thread( $t1 );
     59        $m2 = wp_list_pluck( $thread->messages, 'id' );
     60        $m2 = array_pop( $m2 );
     61
     62        // now get thread by DESC
     63        $thread = new BP_Messages_Thread( $t1, 'DESC' );
     64
     65        // assert!
     66        $this->assertEquals(
     67            array( $m2, $m1 ),
     68            wp_list_pluck( $thread->messages, 'id' )
     69        );
     70    }
     71
    772    /**
    873     * @group get_current_threads_for_user
     
    287352        $this->set_current_user( $current_user );
    288353    }
     354
     355    /**
     356     * @group check_access
     357     */
     358    public function test_check_access_valid_thread() {
     359        $u1 = $this->factory->user->create();
     360        $u2 = $this->factory->user->create();
     361
     362        $t1 = $this->factory->message->create( array(
     363            'sender_id' => $u1,
     364            'recipients' => array( $u2 ),
     365            'subject' => 'Foo',
     366        ) );
     367
     368        // save recipient ID
     369        $thread = new BP_Messages_Thread( $t1 );
     370        $r1 = wp_list_pluck( $thread->recipients, 'id' );
     371        $r1 = array_pop( $r1 );
     372
     373        $this->assertEquals( $r1, BP_Messages_Thread::check_access( $t1, $u1 ) );
     374    }
     375
     376    /**
     377     * @group check_access
     378     */
     379    public function test_check_access_invalid_thread() {
     380        $this->assertEquals( null, BP_Messages_Thread::check_access( 999, 1 ) );
     381    }
     382
     383    /**
     384     * @group is_valid
     385     */
     386    public function test_is_valid_valid_thread() {
     387        $u1 = $this->factory->user->create();
     388        $u2 = $this->factory->user->create();
     389
     390        $t1 = $this->factory->message->create( array(
     391            'sender_id' => $u1,
     392            'recipients' => array( $u2 ),
     393            'subject' => 'Foo',
     394        ) );
     395
     396        $this->assertEquals( $t1, BP_Messages_Thread::is_valid( $t1 ) );
     397    }
     398
     399    /**
     400     * @group is_valid
     401     */
     402    public function test_is_valid_invalid_thread() {
     403        $this->assertEquals( null, BP_Messages_Thread::is_valid( 999 ) );
     404    }
    289405}
Note: See TracChangeset for help on using the changeset viewer.