Skip to content

Commit

Permalink
Sitemap: Make sitemap storing more efficient by not querying the full…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
oskosk authored Oct 2, 2024
1 parent 43181eb commit 96eef0d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/
Original file line number Diff line number Diff line change
@@ -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
30 changes: 27 additions & 3 deletions projects/plugins/jetpack/modules/sitemaps/sitemap-librarian.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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.
*
Expand Down

0 comments on commit 96eef0d

Please sign in to comment.