Skip to content
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

Fast follow: post parent ID cache V2 #5420

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/wp-includes/class-wp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3191,7 +3191,7 @@ public function get_posts() {

return $this->posts;
} elseif ( 'id=>parent' === $q['fields'] ) {
_prime_post_parents_caches( $post_ids );
_prime_post_parent_id_caches( $post_ids );

/** @var int[] */
$post_parents = wp_cache_get_multiple( $post_ids, 'post_parent' );
Expand Down
1 change: 1 addition & 0 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@
add_action( 'admin_menu', '_add_post_type_submenus' );
add_action( 'before_delete_post', '_reset_front_page_settings_for_post' );
add_action( 'wp_trash_post', '_reset_front_page_settings_for_post' );
add_action( 'save_post', '_set_post_parent_cache', 10, 2 );
add_action( 'change_locale', 'create_initial_post_types' );

// Post Formats.
Expand Down
18 changes: 17 additions & 1 deletion src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -3559,6 +3559,22 @@ function _reset_front_page_settings_for_post( $post_id ) {
unstick_post( $post->ID );
}

/**
* Set or update the post parent cache for a specific post.
*
* This function sets or updates the post parent cache for a given post ID.
* The post parent cache is used to store the parent ID of a post for quick retrieval.
*
* @since 6.4.0
* @access private
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be public as with the other cache warming functions.

*
* @param int $post_id The ID of the post for which to set or update the parent cache.
* @param WP_Post $post The post object representing the post.
*/
function _set_post_parent_cache( $post_id, $post ) {
wp_cache_set( $post_id, $post->post_parent );
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Moves a post or page to the Trash
*
Expand Down Expand Up @@ -7805,7 +7821,7 @@ function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache
*
* @param int[] $ids ID list.
*/
function _prime_post_parents_caches( array $ids ) {
function _prime_post_parent_id_caches( array $ids ) {
global $wpdb;

$non_cached_ids = _get_non_cached_ids( $ids, 'post_parent' );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php
/**
* Test `_prime_post_parents_caches()`.
* Test `_prime_post_parent_id_caches()`.
*
* @package WordPress
*/

/**
* Test class for `_prime_post_parents_caches()`.
* Test class for `_prime_post_parent_id_caches()`.
*
* @group post
* @group cache
*
* @covers ::_prime_post_parents_caches
* @covers ::_prime_post_parent_id_caches
*/
class Tests_Post_PrimePostParentsCaches extends WP_UnitTestCase {
class Tests_Post_PrimePostParentIdCaches extends WP_UnitTestCase {

/**
* Post IDs.
Expand All @@ -34,11 +34,11 @@ public static function wpSetupBeforeClass( WP_UnitTest_Factory $factory ) {
/**
* @ticket 59188
*/
public function test_prime_post_parents_caches() {
public function test_prime_post_parent_id_caches() {
$post_id = self::$posts[0];

$before_num_queries = get_num_queries();
_prime_post_parents_caches( array( $post_id ) );
_prime_post_parent_id_caches( array( $post_id ) );
$num_queries = get_num_queries() - $before_num_queries;

$this->assertSame( 1, $num_queries, 'Unexpected number of queries.' );
Expand All @@ -48,9 +48,9 @@ public function test_prime_post_parents_caches() {
/**
* @ticket 59188
*/
public function test_prime_post_parents_caches_multiple() {
public function test_prime_post_parent_id_caches_multiple() {
$before_num_queries = get_num_queries();
_prime_post_parents_caches( self::$posts );
_prime_post_parent_id_caches( self::$posts );
$num_queries = get_num_queries() - $before_num_queries;

$this->assertSame( 1, $num_queries, 'Unexpected number of queries.' );
Expand All @@ -60,10 +60,10 @@ public function test_prime_post_parents_caches_multiple() {
/**
* @ticket 59188
*/
public function test_prime_post_parents_caches_multiple_runs() {
_prime_post_parents_caches( self::$posts );
public function test_prime_post_parent_id_caches_multiple_runs() {
_prime_post_parent_id_caches( self::$posts );
$before_num_queries = get_num_queries();
_prime_post_parents_caches( self::$posts );
_prime_post_parent_id_caches( self::$posts );
$num_queries = get_num_queries() - $before_num_queries;

$this->assertSame( 0, $num_queries, 'Unexpected number of queries.' );
Expand All @@ -72,15 +72,15 @@ public function test_prime_post_parents_caches_multiple_runs() {
/**
* @ticket 59188
*/
public function test_prime_post_parents_caches_update() {
public function test_prime_post_parent_id_caches_update() {
$page_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_parent' => self::$posts[0],
)
);
$before_num_queries = get_num_queries();
_prime_post_parents_caches( array( $page_id ) );
_prime_post_parent_id_caches( array( $page_id ) );
$num_queries = get_num_queries() - $before_num_queries;

$this->assertSame( 1, $num_queries, 'Unexpected number of queries on first run' );
Expand All @@ -94,7 +94,7 @@ public function test_prime_post_parents_caches_update() {
);

$before_num_queries = get_num_queries();
_prime_post_parents_caches( array( $page_id ) );
_prime_post_parent_id_caches( array( $page_id ) );
$num_queries = get_num_queries() - $before_num_queries;

$this->assertSame( 1, $num_queries, 'Unexpected number of queries on second run' );
Expand All @@ -104,7 +104,7 @@ public function test_prime_post_parents_caches_update() {
/**
* @ticket 59188
*/
public function test_prime_post_parents_caches_delete() {
public function test_prime_post_parent_id_caches_delete() {
$parent_page_id = self::factory()->post->create(
array(
'post_type' => 'page',
Expand All @@ -117,7 +117,7 @@ public function test_prime_post_parents_caches_delete() {
)
);
$before_num_queries = get_num_queries();
_prime_post_parents_caches( array( $page_id ) );
_prime_post_parent_id_caches( array( $page_id ) );
$num_queries = get_num_queries() - $before_num_queries;

$this->assertSame( 1, $num_queries, 'Unexpected number of queries on first run' );
Expand Down