-
Notifications
You must be signed in to change notification settings - Fork 823
Connection: memoize connection owner's ID #44282
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
Conversation
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! |
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.
Pull Request Overview
This PR introduces memoization for the connection owner ID to avoid redundant database queries, and ensures the cached value is cleared whenever the owner changes or is removed.
- Add a private static
$connection_owner_id
to cache the owner ID and updateget_connection_owner_id()
to use it. - Hook into option updates and key methods (
update_connection_owner
,disconnect_user
,delete_all_connection_tokens
) to reset the cached ID when the owner is modified. - Add a unit test to verify that the memoized value is reset correctly and update the changelog.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
projects/packages/connection/src/class-manager.php | Introduce $connection_owner_id , update methods to set/reset it, and add invalidation hooks. |
projects/packages/connection/tests/php/ManagerTest.php | Add test test_update_connection_owner_with_memoization_reset to verify cache invalidation logic. |
projects/packages/connection/changelog/add-connection-owner-id-memoization | Document the new memoization feature. |
Comments suppressed due to low confidence (2)
projects/packages/connection/tests/php/ManagerTest.php:606
- [nitpick] Consider renaming this test method to something more concise and descriptive, e.g.,
test_update_connection_owner_resets_memoization
.
public function test_update_connection_owner_with_memoization_reset() {
projects/packages/connection/tests/php/ManagerTest.php:678
- Add tests for
disconnect_user()
anddelete_all_connection_tokens()
to ensure the memoized connection owner ID is cleared in those scenarios.
$this->assertEquals( $admin3_id, $this->manager->get_connection_owner_id() );
Code Coverage SummaryCoverage changed in 1 file.
Full summary · PHP report · JS report If appropriate, add one of these labels to override the failing coverage check:
Covered by non-unit tests
|
Co-authored-by: Copilot <[email protected]>
ceab7db
to
cdbae24
Compare
cdbae24
to
9bdb753
Compare
bfb9986
to
14343af
Compare
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.
Looks good and works as advertised. Thank you for taking care of this!
Proposed changes:
WordPress is efficient in caching user data, so it doesn't rerun database queries to fetch user data if it's already been fetched.
The problems appear when there's a mismatch, and connection owner ID does not match any user accounts. In that case WordPress will not be able to cache the results of the user data query, so it will be queried multiple times.
Memoizing connection ID prevents this kind of situation by storing
0
if there's no connection owner, or if its invalid (e.g. mismatch described above). Therefore, the user info query will not be repeated.Other information:
Does this pull request change what data or activity we track or use?
No.
Testing instructions:
Confirm the issue is fixed:
trunk
:SELECT * FROM wp_users WHERE ID = '12345' LIMIT 1
)add/connection-owner-id-memoization
.Play around with the connection, confirm it continues to work as expected: