[wp-trac] [WordPress Trac] #14751: Comment count cache is never invalidated using memcache

WordPress Trac wp-trac at lists.automattic.com
Wed Sep 1 08:06:01 UTC 2010

#14751: Comment count cache is never invalidated using memcache
 Reporter:  TomUK         |       Owner:                          
     Type:  defect (bug)  |      Status:  new                     
 Priority:  normal        |   Milestone:  Awaiting Review         
Component:  Comments      |     Version:  3.0.1                   
 Severity:  major         |    Keywords:  memcached comments cache
 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

         $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

 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
 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?

Ticket URL: <http://core.trac.wordpress.org/ticket/14751>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software

More information about the wp-trac mailing list