WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 7 years ago

#14713 closed defect (bug) (fixed)

Comment cache is never invalidated when new comments are approved or added

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

Description

I've recently started using memcached on my high traffic wordpress site.

On line 214 of wp-includes/comment.php there is this block of code:

	$last_changed = wp_cache_get('last_changed', 'comment');
	if ( !$last_changed ) {
		$last_changed = time();
		wp_cache_set('last_changed', $last_changed, 'comment');
	}
	$cache_key = "get_comments:$key:$last_changed";

	if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
		return $cache;
	}

Which seems to determine the cache key for caching the comments in the wordpress cache. The value of key last_changed is used as part of the comment cache key, and is set to be the current time if it is not set.

However, last_changed is never updated - meaning when new comments are added the cache for the old comments is still used (as there is value in the key referenced by $cache_key which isn't invalidated).

This wouldn't normally be a problem in the normal object cache, but using memcached means the cache is more persistent between page loads which causing the problem of comments not updating.

The cache key "last_changed" needs to be deleted whenever comments are changed, something like:

/**
 * Fix for comment cache never getting invalidated. We need to remove the last updated cache 
 */
function updateLastCommentUpdated(){
    wp_cache_delete('last_changed', 'comment');
}
add_action('wp_set_comment_status', 'updateLastCommentUpdated');
add_action('comment_post', 'updateLastCommentUpdated');

Which I have added to my functions.php file, but should presumably be in core code somewhere.

Change History (4)

#1 @Denis-de-Bernardy
7 years ago

I'd say that, even more importantly, it ought to get a timeout.

Related: #11431

#2 @ryan
7 years ago

It can be set in clean_comment_cache() similar to how clean_term_cache() handles it.

#3 @ryan
7 years ago

  • Milestone changed from Awaiting Review to 3.1

#4 @ryan
7 years ago

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

(In [15550]) Update last_changed when cleaning comment cache. fixes #14713

Note: See TracTickets for help on using tickets.