Make WordPress Core

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#14751 closed defect (bug) (worksforme)

Comment count cache is never invalidated using memcache

Reported by: TomUK Owned by:
Milestone: Priority: normal
Severity: major Version: 3.0.1
Component: Comments Keywords: memcached comments cache
Focuses: Cc:


This ticket is loosely related to my previous ticket (which has now been fixed) at #14713.

The comment counts (most noticeable is the count of comments pending moderation in the Admin area) are cached in the WordPress cache - but the cache for each of these never expires and is never updated.

This causes problems when using memcached as the cache is persistent.

function wp_count_comments in wp-includes/comment.php uses the cache as follows:

	$count = wp_cache_get("comments-{$post_id}", 'counts');

	if ( false !== $count )
		return $count;

If the cache doesn't exist, it counts the comments then sets the cache using:

wp_cache_set("comments-{$post_id}", $stats, 'counts');

As far as I can see, this cache never gets changed, and never expires. This means that the comment counts across WordPress never get updated, so for example the admins on my site don't know if there are any pending comments as the count will be stuck at whatever it was when I first started memcached.

I have added the following fix to my functions.php:

function fixCommentCountCache($postID){
    wp_cache_delete('comments-'.$postID, 'counts'); //delete the count cache for this comment
    wp_cache_delete('comments-0', 'counts'); //delete the global count cache
add_action('wp_update_comment_count', 'fixCommentCountCache');
add_action('comment_post', 'fixCommentCountCache');

I've attached it to the action to update the count of comments in the database for a particular post - which handles updating (deleting) the cache when a comment is moderated, and also to the comment_post action which deletes the cache when a comment is added so that the pending moderation count will be updated.

I'm not sure what functions to put this in core code? Perhaps wp_update_comment_count_now() and wp_new_comment(), where the above two actions are called?

Change History (2)

#1 @markjaquith
11 years ago

  • Resolution set to worksforme
  • Status changed from new to closed

Counts are non-persistent. They only last for the current page load.

wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );

Your Memcache backend must not be designating it as non-persistent.

#2 @nacin
11 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.