Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#40306 closed defect (bug) (fixed)

Term cache isn't cleared completely when setting and removing object terms

Reported by: mboynes's profile mboynes Owned by: boonebgorges's profile boonebgorges
Milestone: 4.7.4 Priority: normal
Severity: normal Version: 4.7
Component: Taxonomy Keywords: reporter-feedback
Focuses: Cc:

Description

This problem is easiest explained and understood through a unit test; see attached for that plus the patch fixing it.

wp_set_object_terms() and wp_remove_object_terms() delete the object cache entry for the object ID in the group {$taxonomy}_relationships. However, Term_Query, which ultimately queries for those terms, has its own cache that doesn't get invalidated.Term_Query's cache key depends on the terms:last_changed cache entry, so deleting that cache entry will invalidate the cache.

This could/should be a bigger issue than it actually is. The reason it isn't is that the cache gets cleared incidentally via wp_update_term_count_now(), which performs a pretty greedy cache clear. If one is using an external object cache and disables term counting, the term relationship gets set when the default empty post is created on the "Write" screen and is never erased, and it will forever appear as though a post has no terms (until some other operation invalidates the Term_Query cache).

Attachments (2)

40306.diff (2.1 KB) - added by mboynes 7 years ago.
Invalidate Term_Query cache on wp_set_object_terms and wp_remove_object_terms
40306.2.diff (3.0 KB) - added by boonebgorges 7 years ago.

Download all attachments as: .zip

Change History (7)

@mboynes
7 years ago

Invalidate Term_Query cache on wp_set_object_terms and wp_remove_object_terms

#1 @boonebgorges
7 years ago

  • Milestone changed from Awaiting Review to 4.7.4
  • Version changed from trunk to 4.7

Thanks for the patch, @mboynes. This never came up in the past, because term queries were never affected by term relationships. Since 4.7, when wp_get_object_terms() was refactored to use WP_Term_Query, the terms:last_changed incrementor started applying to object-term queries as well, and this bit of invalidation wasn't caught.

@boonebgorges
7 years ago

#2 @boonebgorges
7 years ago

  • Keywords reporter-feedback added

40306.2.diff reworks the tests so that they're a bit more precise. I'm guessing that your "default post to edit" trick was to work around the "Uncategorized" category. We can eliminate that complication by using a different taxonomy. I've also changed your wp_update_post() call to wp_set_object_terms() so that there are fewer variables at play, and added a parallel test for wp_remove_object_terms().

Can you give the tests a look to see if they still capture the bug as you've reported it? (The fix itself that you provided seems fine to me.)

#3 @mboynes
7 years ago

This looks good to me, thanks @boonebgorges!

#4 @boonebgorges
7 years ago

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

In 40353:

Invalidate term query caches when setting or deleting term relationships.

Prior to 4.7, term relationships - as set by wp_set_object_terms() or
wp_remove_object_terms() - did not affect the term query cache. The
introduction of the 'object_ids' parameter in 4.7 means that the query
cache must be aware of object-term relationships. As such, the
'last_changed' incrementor is now invalidated when term relationships
are modified.

This bug only reared its head when delaying term counting, because term
counting performs its own term query cache invalidation.

Props mboynes.
Fixes #40306.

#5 @boonebgorges
7 years ago

In 40354:

Invalidate term query caches when setting or deleting term relationships.

Prior to 4.7, term relationships - as set by wp_set_object_terms() or
wp_remove_object_terms() - did not affect the term query cache. The
introduction of the 'object_ids' parameter in 4.7 means that the query
cache must be aware of object-term relationships. As such, the
'last_changed' incrementor is now invalidated when term relationships
are modified.

This bug only reared its head when delaying term counting, because term
counting performs its own term query cache invalidation.

Merges [40353] to the 4.7 branch.

Props mboynes.
Fixes #40306.

Note: See TracTickets for help on using tickets.