-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Cache API: Cache non-existent users in WP_User::get_data_by() to prevent duplicate queries #11632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MarcinDudekDev
wants to merge
1
commit into
WordPress:trunk
Choose a base branch
from
MarcinDudekDev:trac/46388
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+131
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Tests for WP_User::get_data_by() caching behavior. | ||
| * | ||
| * @group user | ||
| * @group cache | ||
| * | ||
| * @coversDefaultClass WP_User | ||
| */ | ||
| class Tests_User_GetDataBy extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * @ticket 46388 | ||
| * @covers WP_User::get_data_by | ||
| */ | ||
| public function test_nonexistent_user_by_id_does_not_trigger_multiple_queries() { | ||
| global $wpdb; | ||
|
|
||
| $nonexistent_id = PHP_INT_MAX; | ||
|
|
||
| wp_cache_delete( 'notuser_' . $nonexistent_id, 'users' ); | ||
| wp_cache_delete( $nonexistent_id, 'users' ); | ||
|
|
||
| $before = $wpdb->num_queries; | ||
| get_userdata( $nonexistent_id ); | ||
| $after_first_call = $wpdb->num_queries; | ||
|
|
||
| get_userdata( $nonexistent_id ); | ||
| $after_second_call = $wpdb->num_queries; | ||
|
|
||
| $this->assertSame( 1, $after_first_call - $before, 'First call for non-existent user should trigger one DB query.' ); | ||
| $this->assertSame( 0, $after_second_call - $after_first_call, 'Second call for non-existent user should not trigger any DB queries.' ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 46388 | ||
| * @covers WP_User::get_data_by | ||
| */ | ||
| public function test_nonexistent_user_by_id_is_added_to_notuser_cache() { | ||
| $nonexistent_id = PHP_INT_MAX - 1; | ||
|
|
||
| wp_cache_delete( 'notuser_' . $nonexistent_id, 'users' ); | ||
| wp_cache_delete( $nonexistent_id, 'users' ); | ||
|
|
||
| get_userdata( $nonexistent_id ); | ||
|
|
||
| $this->assertNotFalse( | ||
| wp_cache_get( 'notuser_' . $nonexistent_id, 'users' ), | ||
| 'Non-existent user ID should be stored in the notuser cache after a DB miss.' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 46388 | ||
| * @covers WP_User::get_data_by | ||
| */ | ||
| public function test_existing_user_by_id_is_not_added_to_notuser_cache() { | ||
| $user_id = self::factory()->user->create(); | ||
|
|
||
| get_userdata( $user_id ); | ||
|
|
||
| $this->assertFalse( | ||
| wp_cache_get( 'notuser_' . $user_id, 'users' ), | ||
| 'Existing user ID should not be stored in the notuser cache.' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies that clean_user_cache() called with a plain integer (as in wp_insert_user()) | ||
| * clears the notuser cache before constructing WP_User internally, | ||
| * preventing a self-referential cache miss. | ||
| * | ||
| * @ticket 46388 | ||
| * @covers ::clean_user_cache | ||
| */ | ||
| public function test_clean_user_cache_with_integer_clears_notuser_cache() { | ||
| $user_id = self::factory()->user->create(); | ||
|
|
||
| // Manually poison the notuser cache for this user. | ||
| wp_cache_delete( $user_id, 'users' ); | ||
| wp_cache_set( 'notuser_' . $user_id, true, 'users' ); | ||
|
|
||
| // wp_insert_user() calls clean_user_cache() with a plain integer. | ||
| clean_user_cache( $user_id ); | ||
|
|
||
| $this->assertFalse( | ||
| wp_cache_get( 'notuser_' . $user_id, 'users' ), | ||
| 'clean_user_cache() with an integer must clear notuser_ before constructing WP_User.' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies that clean_user_cache() called with a WP_User object (as in wp_update_user()) | ||
| * also clears the notuser cache. | ||
| * | ||
| * @ticket 46388 | ||
| * @covers ::clean_user_cache | ||
| */ | ||
| public function test_clean_user_cache_with_wp_user_object_clears_notuser_cache() { | ||
| $user_id = self::factory()->user->create(); | ||
| $user_obj = get_userdata( $user_id ); | ||
|
|
||
| // Manually poison the notuser cache. | ||
| wp_cache_set( 'notuser_' . $user_id, true, 'users' ); | ||
|
|
||
| // wp_update_user() calls clean_user_cache() with a WP_User object. | ||
| clean_user_cache( $user_obj ); | ||
|
|
||
| $this->assertFalse( | ||
| wp_cache_get( 'notuser_' . $user_id, 'users' ), | ||
| 'clean_user_cache() with a WP_User object must clear the notuser_ cache entry.' | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not too sure about 1 day caching duration. Plus this cache will only be cached if the user has some persistent object cache.