Make WordPress Core

Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#55620 closed enhancement (fixed)

Prime caches linked objects in menu item REST API

Reported by: spacedmonkey Owned by: spacedmonkey
Priority: normal Milestone: 6.1
Component: REST API Version: 5.9
Severity: normal Keywords: has-patch needs-unit-tests has-dev-note
Cc: Focuses: rest-api, performance

Description

Follow on from [52975].

Ensure that all the linked objects, such as terms and posts that are linked to a menu item, are primed in cache, in the menu item controller.

This will result in far less database queries per page.

Attachments (2)

Screenshot 2022-04-26 at 14.05.55.png​ (698.3 KB ) - added by spacedmonkey 5 years ago.
Before change
Screenshot 2022-04-26 at 14.05.12.png​ (516.1 KB ) - added by spacedmonkey 5 years ago.
After change

Download all attachments as: .zip

Change History (38)

This ticket was mentioned in ​PR #2631 on ​WordPress/wordpress-develop by ​spacedmonkey.


5 years ago
#1

  • Keywords has-patch added; needs-patch removed

#2 @spacedmonkey
5 years ago

  • Owner set to spacedmonkey
  • Status new → assigned

#3 @spacedmonkey
5 years ago

  • Keywords needs-patch added; has-patch removed

Related: #55593 #55592

#4 @spacedmonkey
5 years ago

  • Keywords has-patch needs-unit-tests added; needs-patch removed

#5 @spacedmonkey
5 years ago

I have put together a very simple patch, that needs to work results are promising. See the before and after screenshots. Before 31 queries vs after 19.

This ticket was mentioned in ​Slack in #hosting-community by javier. ​View the logs.


5 years ago

This ticket was mentioned in ​Slack in #hosting-community by javier. ​View the logs.


5 years ago

This ticket was mentioned in ​Slack in #hosting-community by jadonn. ​View the logs.


5 years ago

This ticket was mentioned in ​Slack in #hosting-community by javier. ​View the logs.


5 years ago

This ticket was mentioned in ​Slack in #hosting-community by jadonn. ​View the logs.


5 years ago

​TimothyBJacobs commented on ​PR #2631:


4 years ago
#11

I wonder if it would make sense to add this as a flag in WP_Query itself instead of filtering in like this. For instance update_menu_item_object_cache that would automatically call this new helper function when set. I think that would clean up the controller code a bit and make it more obvious to 3rd party developers that this is a performance option they have available to them.

​spacedmonkey commented on ​PR #2631:


4 years ago
#12

I wonder if it would make sense to add this as a flag in WP_Query itself instead of filtering in like this. For instance update_menu_item_object_cache that would automatically call this new helper function when set. I think that would clean up the controller code a bit and make it more obvious to 3rd party developers that this is a performance option they have available to them.

That does sound interesting, I really do not want more code in WP_Query. That class is really too long and has too many params.

Another idea I had, was moving the query logic to another method in Post Controller and the overriding that. What do we think of that idea.

Like

protected function get_objects( $args ) {
      $query = new WP_Query( $args ); 
      ....
}

​spacedmonkey commented on ​PR #2631:


4 years ago
#13

@TimothyBJacobs I have actioned your feedback. I think the result is cleaner. Can you take a look.

​TimothyBJacobs commented on ​PR #2631:


4 years ago
#14

This looks great to me @spacedmonkey! We just need to make sure to add a changelog entry for the new parameter.

​spacedmonkey commented on ​PR #2631:


4 years ago
#16

@manuelRod @TimothyBJacobs I have come up another lighter weight version of this PR. See what you think - ​https://github.com/WordPress/wordpress-develop/pull/2806/

​TimothyBJacobs commented on ​PR #2631:


4 years ago
#17

Personally I prefer it as a WP_Query option like you have here, but I think either are workable. Though it is a bit odd to have menu item specific code inside of the base posts controller.

#18 @spacedmonkey
4 years ago

  • Resolution → fixed
  • Status assigned → closed

In 53504:

REST API: Prime caches for linked objects in menu item REST API controller.

Add a new parameter to WP_Query called update_menu_item_cache that when set to true, primes the caches for linked terms and posts for menu item post objects. This change moves logic
found in wp_get_nav_menu_items into a new function called update_menu_item_cache. Update the menu item REST API controller, to pass the update_menu_item_cache parameter to the
arguments used for the WP_Query run to get menu items.

Props furi3r, TimothyBlynJacobs, spacedmonkey, peterwilsoncc, mitogh.
Fixes #55620.

--This line, and those below, will be ignored--

M src/wp-includes/class-wp-query.php
M src/wp-includes/nav-menu.php
M src/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php
M tests/phpunit/tests/post/nav-menu.php

#20 @SergeyBiryukov
4 years ago

In 53508:

REST API: Some documentation and test improvements for update_menu_item_cache():

  • Make the function description more specific, for consistency with other similar functions.
  • Add a @since note for the $update_menu_item_cache parameter of WP_Query::parse_query().
  • Add missing @covers tags for the unit tests.

Follow-up to [53504].

See #55620.

#21 @desrosj
4 years ago

  • Milestone Awaiting Review → 6.1

#22 @desrosj
4 years ago

  • Keywords needs-dev-note added

There are a lot of caching related changes in 6.1 and a dev note should be written collecting them all. Marking this for inclusion.

#23 @spacedmonkey
4 years ago

Made a start of dev notes ​here.

This ticket was mentioned in ​Slack in #core-restapi by spacedmonkey. ​View the logs.


4 years ago

#25 @spacedmonkey
4 years ago

Early draft of a dev note can be found ​here.

#26 @spacedmonkey
4 years ago

  • Resolution fixed
  • Status closed → reopened

This ticket was mentioned in ​PR #3403 on ​WordPress/wordpress-develop by ​spacedmonkey.


4 years ago
#27

Trac ticket: https://core.trac.wordpress.org/ticket/55620

Move menu item priming to after meta is primed.

#28 @spacedmonkey
4 years ago

Reopenning as seen an issue. Priming of menu item linked objects after menu item post meta is primed. See ​#3403.

CC @peterwilsoncc @mukesh27

​peterwilsoncc commented on ​PR #3403:


4 years ago
#29

Am I correct in thinking the issue is the meta cache needs to be primed to reduce the queries from these two lines?

​https://github.com/WordPress/wordpress-develop/blob/ec0f508a9ff1de37d9729785841a09250aa22462/src/wp-includes/nav-menu.php#L771-L772

A unit test or two would be dandy to make sure the code isn't later refactored to prime the menus prior to the meta data.

​spacedmonkey commented on ​PR #3403:


4 years ago
#30

A unit test or two would be dandy to make sure the code isn't later refactored to prime the menus prior to the meta data.

Unit tests added.

​spacedmonkey commented on ​PR #3403:


4 years ago
#31

@peterwilsoncc Good call out. I have improves the tests.

This ticket was mentioned in ​Slack in #core by audrasjb. ​View the logs.


4 years ago

#33 @spacedmonkey
4 years ago

In 54410:

Query: Move call to update_menu_item_cache in WP_Query

Move call to update_menu_item_cache in WP_Query to after post meta caches for menu items are primed.

Props spacedmonkey, peterwilsoncc, mukesh27.
See #55620.

#35 @spacedmonkey
4 years ago

  • Resolution → fixed
  • Status reopened → closed
Note: See TracTickets for help on using tickets.