Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
1111830
Make the wp_count_posts() cache key flushable upon post changes.
dd32 Dec 14, 2022
bae2bf5
Remove the 'counts' group from being non-persistent.
dd32 Dec 14, 2022
6a4aaae
Move get_adjacent_post() from using the 'counts' group to it's own ca…
dd32 Dec 14, 2022
c42fa24
Use wp_cache_get_last_changed('comments') to flush the comments count…
dd32 Dec 14, 2022
6844bac
Clear the Privacy table counts based on the posts last-changed.
dd32 Dec 14, 2022
1b5d0c8
The 'counts' group is now persistent.
dd32 Dec 14, 2022
4bd0f87
Bucket the wp_count_attachments() cache on the posts last-changed date.
dd32 Dec 14, 2022
3acd044
Persistently cache get_adjacent_post() and clear it with the posts la…
dd32 Dec 14, 2022
2bec4e1
Clean the comment cache upon Trashing/Untrashing/Spamming/Unspamming …
dd32 Dec 14, 2022
eed810a
Tests: wp_count_attachments() Generate the expected cache key after t…
dd32 Dec 14, 2022
5004f8e
Fix a typo in c42fa246385c4124402b39a0f57d15262c433f18, the last_chan…
dd32 Dec 14, 2022
17fb0d8
Move the last_changed in wp_update_comment_count_now() to a more appr…
dd32 Dec 14, 2022
a2404f9
Revert changes to get_adjacent_post() in preference for PR #3366
dd32 Dec 19, 2022
9a70726
Use the more common : cache delimiter.
dd32 Dec 19, 2022
4e06240
Don't make the 'counts' group non-persistent, as other plugins may us…
dd32 Dec 19, 2022
c497ca7
Merge branch 'refs/heads/trunk' into try/persistent-count-group
May 6, 2024
9891003
Merge branch 'refs/heads/trunk' into try/persistent-count-group
May 6, 2024
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
7 changes: 4 additions & 3 deletions src/wp-admin/includes/class-wp-privacy-requests-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ protected function get_default_primary_column_name() {
protected function get_request_counts() {
global $wpdb;

$cache_key = $this->post_type . '-' . $this->request_type;
$counts = wp_cache_get( $cache_key, 'counts' );
// This duplicates wp_count_posts() with an additional post_name clause.
$cache_key = wp_cache_get_last_changed( 'posts' ) . ':' . $this->post_type . '-' . $this->request_type;
$counts = wp_cache_get( $cache_key, 'persistent-counts' );

if ( false !== $counts ) {
return $counts;
Expand All @@ -131,7 +132,7 @@ protected function get_request_counts() {
}

$counts = (object) $counts;
wp_cache_set( $cache_key, $counts, 'counts' );
wp_cache_set( $cache_key, $counts, 'persistent-counts' );

return $counts;
}
Expand Down
19 changes: 14 additions & 5 deletions src/wp-includes/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,9 @@ function wp_count_comments( $post_id = 0 ) {
return $filtered;
}

$count = wp_cache_get( "comments-{$post_id}", 'counts' );
$cache_key = wp_cache_get_last_changed( 'comment' ) . ":comments-{$post_id}";

$count = wp_cache_get( $cache_key, 'persistent-counts' );
if ( false !== $count ) {
return $count;
}
Expand All @@ -1424,7 +1426,7 @@ function wp_count_comments( $post_id = 0 ) {
unset( $stats['awaiting_moderation'] );

$stats_object = (object) $stats;
wp_cache_set( "comments-{$post_id}", $stats_object, 'counts' );
wp_cache_set( $cache_key, $stats_object, 'persistent-counts' );

return $stats_object;
}
Expand Down Expand Up @@ -1549,6 +1551,8 @@ function wp_trash_comment( $comment_id ) {
add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved );
add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() );

clean_comment_cache( $comment->comment_ID );

/**
* Fires immediately after a comment is sent to Trash.
*
Expand Down Expand Up @@ -1600,6 +1604,8 @@ function wp_untrash_comment( $comment_id ) {
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );

clean_comment_cache( $comment->comment_ID );

/**
* Fires immediately after a comment is restored from the Trash.
*
Expand Down Expand Up @@ -1648,6 +1654,8 @@ function wp_spam_comment( $comment_id ) {
add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved );
add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() );

clean_comment_cache( $comment->comment_ID );

/**
* Fires immediately after a comment is marked as Spam.
*
Expand Down Expand Up @@ -1699,6 +1707,8 @@ function wp_unspam_comment( $comment_id ) {
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );

clean_comment_cache( $comment->comment_ID );

/**
* Fires immediately after a comment is unmarked as Spam.
*
Expand Down Expand Up @@ -2723,9 +2733,6 @@ function wp_update_comment_count_now( $post_id ) {
return false;
}

wp_cache_delete( 'comments-0', 'counts' );
wp_cache_delete( "comments-{$post_id}", 'counts' );

$post = get_post( $post_id );

if ( ! $post ) {
Expand Down Expand Up @@ -2755,6 +2762,8 @@ function wp_update_comment_count_now( $post_id ) {

clean_post_cache( $post );

wp_cache_set_comments_last_changed();

/**
* Fires immediately after a post's comment count is updated in the database.
*
Expand Down
18 changes: 7 additions & 11 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -3084,7 +3084,7 @@ function unstick_post( $post_id ) {
* @return string The cache key.
*/
function _count_posts_cache_key( $type = 'post', $perm = '' ) {
$cache_key = 'posts-' . $type;
$cache_key = wp_cache_get_last_changed( 'posts' ) . ':posts-' . $type;

if ( 'readable' === $perm && is_user_logged_in() ) {
$post_type_object = get_post_type_object( $type );
Expand Down Expand Up @@ -3126,7 +3126,7 @@ function wp_count_posts( $type = 'post', $perm = '' ) {

$cache_key = _count_posts_cache_key( $type, $perm );

$counts = wp_cache_get( $cache_key, 'counts' );
$counts = wp_cache_get( $cache_key, 'persistent-counts' );
if ( false !== $counts ) {
// We may have cached this before every status was registered.
foreach ( get_post_stati() as $status ) {
Expand Down Expand Up @@ -3161,7 +3161,7 @@ function wp_count_posts( $type = 'post', $perm = '' ) {
}

$counts = (object) $counts;
wp_cache_set( $cache_key, $counts, 'counts' );
wp_cache_set( $cache_key, $counts, 'persistent-counts' );

/**
* Filters the post counts by status for the current post type.
Expand Down Expand Up @@ -3197,11 +3197,12 @@ function wp_count_attachments( $mime_type = '' ) {
global $wpdb;

$cache_key = sprintf(
'attachments%s',
'%s:attachments%s',
wp_cache_get_last_changed( 'posts' ),
! empty( $mime_type ) ? ':' . str_replace( '/', '_', implode( '-', (array) $mime_type ) ) : ''
);

$counts = wp_cache_get( $cache_key, 'counts' );
$counts = wp_cache_get( $cache_key, 'persistent-counts' );
if ( false == $counts ) {
$and = wp_post_mime_type_where( $mime_type );
$count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A );
Expand All @@ -3212,7 +3213,7 @@ function wp_count_attachments( $mime_type = '' ) {
}
$counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and" );

wp_cache_set( $cache_key, (object) $counts, 'counts' );
wp_cache_set( $cache_key, (object) $counts, 'persistent-counts' );
}

/**
Expand Down Expand Up @@ -7622,11 +7623,6 @@ function _transition_post_status( $new_status, $old_status, $post ) {
}
}

if ( $new_status !== $old_status ) {
wp_cache_delete( _count_posts_cache_key( $post->post_type ), 'counts' );
wp_cache_delete( _count_posts_cache_key( $post->post_type, 'readable' ), 'counts' );
}

// Always clears the hook in case the post status bounced from future to draft.
wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) );
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/includes/abstract-testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public static function flush_cache() {
)
);

wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) );
wp_cache_add_non_persistent_groups( array( 'plugins', 'theme_json' ) );
}

/**
Expand Down
7 changes: 4 additions & 3 deletions tests/phpunit/tests/post/wpCountAttachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class Tests_Post_wpCountAttachments extends WP_UnitTestCase {
*/
public function test_wp_count_attachments_should_cache_the_result() {
$mime_type = 'image/jpeg';
$cache_key = 'attachments:image_jpeg';

self::factory()->post->create_many(
3,
Expand All @@ -25,8 +24,10 @@ public function test_wp_count_attachments_should_cache_the_result() {
'post_mime_type' => $mime_type,
)
);
$expected = wp_count_attachments( $mime_type );
$actual = wp_cache_get( $cache_key, 'counts' );

$expected = wp_count_attachments( $mime_type );
$cache_key = wp_cache_get_last_changed( 'posts' ) . ':attachments:image_jpeg';
$actual = wp_cache_get( $cache_key, 'persistent-counts' );

$this->assertEquals( $expected, $actual );
}
Expand Down