From 96eef0d270ba5952b3322ffda8edf0604bfb2802 Mon Sep 17 00:00:00 2001 From: Osk Date: Wed, 2 Oct 2024 17:44:16 -0300 Subject: [PATCH] Sitemap: Make sitemap storing more efficient by not querying the full post content before saving (#39572) * Make sitemap storing more efficient but not querying the full post content when storing the sitemap This update tries to remediate a problem where the persistent object cache gets huge with data that we will not use in the end. We just need to get the id of the current sitemap post and if it exists, we update it. Before, we were using get_post and this polluted the cache running into OOM situations --- projects/plugins/jetpack/changelog/ | 4 +++ .../modules/sitemaps/sitemap-librarian.php | 30 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 projects/plugins/jetpack/changelog/ diff --git a/projects/plugins/jetpack/changelog/ b/projects/plugins/jetpack/changelog/ new file mode 100644 index 0000000000000..b49c0a2c00caf --- /dev/null +++ b/projects/plugins/jetpack/changelog/ @@ -0,0 +1,4 @@ +Significance: patch +Type: enhancement + +Make sitemap storing more efficient but not querying the full post content when storing the updated sitemap diff --git a/projects/plugins/jetpack/modules/sitemaps/sitemap-librarian.php b/projects/plugins/jetpack/modules/sitemaps/sitemap-librarian.php index 97e171b5a0581..53b7fb09d24c2 100644 --- a/projects/plugins/jetpack/modules/sitemaps/sitemap-librarian.php +++ b/projects/plugins/jetpack/modules/sitemaps/sitemap-librarian.php @@ -71,6 +71,11 @@ public function read_sitemap_data( $name, $type ) { * If a sitemap with that type and name does not exist, create it. * If a sitemap with that type and name does exist, update it. * + * This method uses get_current_sitemap_post_id() for efficiency, + * as it only retrieves the post ID, which will be typically cached in the persistent object cache. + * This approach avoids loading unnecessary data (like post content) into memory, + * unlike using read_sitemap_data() which would retrieve the full post object. + * * @access public * @since 4.8.0 * @@ -82,9 +87,9 @@ public function read_sitemap_data( $name, $type ) { public function store_sitemap_data( $index, $type, $contents, $timestamp ) { $name = jp_sitemap_filename( $type, $index ); - $the_post = $this->read_sitemap_data( $name, $type ); + $post_id = $this->get_current_sitemap_post_id( $name, $type ); - if ( null === $the_post ) { + if ( null === $post_id ) { // Post does not exist. wp_insert_post( array( @@ -98,7 +103,7 @@ public function store_sitemap_data( $index, $type, $contents, $timestamp ) { // Post does exist. wp_insert_post( array( - 'ID' => $the_post['id'], + 'ID' => $post_id, 'post_title' => $name, 'post_content' => base64_encode( $contents ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode 'post_type' => $type, @@ -108,6 +113,25 @@ public function store_sitemap_data( $index, $type, $contents, $timestamp ) { } } + /** + * Get the current sitemap post ID. + * + * @param string $name The name of the sitemap. + * @param string $type The type of the sitemap. + * @return int|null The post ID if it exists, null otherwise. + */ + private function get_current_sitemap_post_id( $name, $type ) { + $args = array( + 'post_type' => $type, + 'post_status' => 'draft', + 'posts_per_page' => 1, + 'title' => $name, + 'fields' => 'ids', + ); + + $query = new WP_Query( $args ); + return $query->posts ? $query->posts[0] : null; + } /** * Delete a sitemap by name and type. *