Changeset 9482
- Timestamp:
- 02/13/2015 05:24:33 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
-
src/bp-messages/bp-messages-cache.php (modified) (1 diff)
-
src/bp-messages/bp-messages-classes.php (modified) (1 diff)
-
tests/phpunit/testcases/messages/class.bp-messages-thread.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-messages/bp-messages-cache.php
r9351 r9482 85 85 add_action( 'messages_notice_after_save', 'bp_notices_clear_cache' ); 86 86 add_action( 'messages_notice_before_delete', 'bp_notices_clear_cache' ); 87 88 /** 89 * Invalidate thread recipient cache on message update. 90 * 91 * @since BuddyPress (2.3.0) 92 * 93 * @param BP_Messages_Message $message Message object. 94 */ 95 function bp_messages_clear_message_thread_recipient_cache_on_message_sent( BP_Messages_Message $message ) { 96 wp_cache_delete( 'thread_recipients_' . $message->thread_id, 'bp_messages' ); 97 } 98 add_action( 'messages_message_sent', 'bp_messages_clear_message_thread_recipient_cache_on_message_sent' ); 99 100 /** 101 * Invalidate thread recipient cache on thread deletion. 102 * 103 * @since BuddyPress (2.3.0) 104 * 105 * @param int|array $thread_ids IDs of deleted threads. 106 */ 107 function bp_messages_clear_message_thread_recipient_cache_on_thread_delete( $thread_ids ) { 108 foreach ( (array) $thread_ids as $thread_id ) { 109 wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' ); 110 } 111 } 112 add_action( 'messages_delete_thread', 'bp_messages_clear_message_thread_recipient_cache_on_thread_delete' ); -
trunk/src/bp-messages/bp-messages-classes.php
r9471 r9482 214 214 global $wpdb; 215 215 216 $bp = buddypress(); 217 218 $recipients = array(); 219 $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $this->thread_id ) ); 220 221 foreach ( (array) $results as $recipient ) { 222 $recipients[$recipient->user_id] = $recipient; 216 $recipients = wp_cache_get( 'thread_recipients_' . $this->thread_id, 'bp_messages' ); 217 if ( false === $recipients ) { 218 $bp = buddypress(); 219 220 $recipients = array(); 221 $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $this->thread_id ) ); 222 223 foreach ( (array) $results as $recipient ) { 224 $recipients[ $recipient->user_id ] = $recipient; 225 } 226 227 wp_cache_set( 'thread_recipients_' . $this->thread_id, $recipients, 'bp_messages' ); 223 228 } 224 229 -
trunk/tests/phpunit/testcases/messages/class.bp-messages-thread.php
r9302 r9482 93 93 $this->assertSame( $expected, $found ); 94 94 } 95 96 /** 97 * @group get_recipients 98 * @group cache 99 */ 100 public function test_get_recipients_should_cache_its_values() { 101 global $wpdb; 102 103 $u1 = $this->factory->user->create(); 104 $u2 = $this->factory->user->create(); 105 106 $t1 = $this->factory->message->create( array( 107 'sender_id' => $u1, 108 'recipients' => array( $u2 ), 109 'subject' => 'Foo', 110 ) ); 111 112 $thread = new BP_Messages_Thread( $t1 ); 113 $recipients = $thread->get_recipients(); 114 115 $num_queries = $wpdb->num_queries; 116 $recipients_cached = $thread->get_recipients(); 117 118 $this->assertEquals( $recipients, $recipients_cached ); 119 $this->assertEquals( $num_queries, $wpdb->num_queries ); 120 } 121 122 /** 123 * @group get_recipients 124 * @group cache 125 */ 126 public function test_get_recipients_cache_should_be_busted_when_thread_message_is_sent() { 127 global $wpdb; 128 129 $u1 = $this->factory->user->create(); 130 $u2 = $this->factory->user->create(); 131 132 $t1 = $this->factory->message->create( array( 133 'sender_id' => $u1, 134 'recipients' => array( $u2 ), 135 'subject' => 'Foo', 136 ) ); 137 138 $thread = new BP_Messages_Thread( $t1 ); 139 $recipients = $thread->get_recipients(); 140 141 // Verify that the cache is populated. 142 $num_queries = $wpdb->num_queries; 143 $recipients_cached = $thread->get_recipients(); 144 $this->assertEquals( $num_queries, $wpdb->num_queries ); 145 146 messages_new_message( array( 147 'sender_id' => $u2, 148 'thread_id' => $t1, 149 'recipients' => array( $u1 ), 150 'subject' => 'Bar', 151 'content' => 'Baz', 152 ) ); 153 154 // Cache should be empty. 155 $num_queries = $wpdb->num_queries; 156 $recipients_uncached = $thread->get_recipients(); 157 $this->assertEquals( $num_queries + 1, $wpdb->num_queries ); 158 } 159 160 /** 161 * @group get_recipients 162 * @group cache 163 */ 164 public function test_get_recipients_cache_should_be_busted_when_single_thread_is_deleted() { 165 global $wpdb; 166 167 $u1 = $this->factory->user->create(); 168 $u2 = $this->factory->user->create(); 169 170 $t1 = $this->factory->message->create( array( 171 'sender_id' => $u1, 172 'recipients' => array( $u2 ), 173 'subject' => 'Foo', 174 ) ); 175 176 $thread = new BP_Messages_Thread( $t1 ); 177 $recipients = $thread->get_recipients(); 178 179 // Verify that the cache is populated. 180 $num_queries = $wpdb->num_queries; 181 $recipients_cached = $thread->get_recipients(); 182 $this->assertEquals( $num_queries, $wpdb->num_queries ); 183 184 messages_delete_thread( $t1 ); 185 186 // Cache should be empty. 187 $this->assertFalse( wp_cache_get( 'thread_recipients_' . $t1, 'bp_messages' ) ); 188 } 189 190 /** 191 * @group get_recipients 192 * @group cache 193 */ 194 public function test_get_recipients_cache_should_be_busted_when_array_of_threads_is_deleted() { 195 global $wpdb; 196 197 $u1 = $this->factory->user->create(); 198 $u2 = $this->factory->user->create(); 199 200 $t1 = $this->factory->message->create( array( 201 'sender_id' => $u1, 202 'recipients' => array( $u2 ), 203 'subject' => 'Foo', 204 ) ); 205 206 $thread = new BP_Messages_Thread( $t1 ); 207 $recipients = $thread->get_recipients(); 208 209 // Verify that the cache is populated. 210 $num_queries = $wpdb->num_queries; 211 $recipients_cached = $thread->get_recipients(); 212 $this->assertEquals( $num_queries, $wpdb->num_queries ); 213 214 messages_delete_thread( array( $t1 ) ); 215 216 // Cache should be empty. 217 $this->assertFalse( wp_cache_get( 'thread_recipients_' . $t1, 'bp_messages' ) ); 218 } 95 219 }
Note: See TracChangeset
for help on using the changeset viewer.