Make WordPress Core

Opened 5 months ago

Closed 5 months ago

Last modified 5 months ago

#62031 closed defect (bug) (worksforme)

Term cache is not properly cleaned when using persistent object caching

Reported by: dekadinious's profile Dekadinious Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Taxonomy Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

When persistent object caching is used, the term cache is not properly cleaned when inserting a term as a child under another term using wp_insert_term. I am not sure if it happens when inserting terms in general. This has created quite a problem for our site which imports terms from another system. Many duplicates are made on each import.

Let's say you have a parent term with ID 34758. The import process inserts a child term by doing something like

wp_insert_term( 'Child term', 'taxonomy', array( 'parent' => 34758) );

The wp_insert_term documentation states "The term cache is cleaned.".

However, if we do:

get_terms(
    array(
	'taxonomy'   => 'taxonomy',
	'hide_empty' => false,
	'parent'     => 34758,
    )
);

we get an empty array. Doing these two over and over again always successfully inserts a child term, but always returns an empty array. The child terms only appear after flushing the entire object cache.

I would expect wp_insert_term to flush the cache in such a way that get_terms returns the newly inserted child even when using persistent object caching. Otherwise one will not be able to group imported objects under the same child term because a new one will always be created since it can't find the newly created existing one that both objects should be connected to.

Change History (10)

#1 @Dekadinious
5 months ago

This also applies to get_term_children which will be empty until the object cache is flushed.

This ticket was mentioned in PR #7324 on WordPress/wordpress-develop by @peterwilsoncc.


5 months ago
#2

  • Keywords has-patch has-unit-tests added

#3 @peterwilsoncc
5 months ago

@Dekadinious I'm unable to reproduce this issue, what plugin are you using for persistent caching?

I've created a pull request with a test of the caching that runs get_terms() before and after the child term has been inserted.

The associated test run is passing both for systems with and without a persistent cache.

#4 @Dekadinious
5 months ago

Thank you for getting on top of this so quickly, @peterwilsoncc!

We are using version 1.21.2 of Object Cache Pro with Redis on the server. I can confirm that this does not happen with the same codebase and setup on our staging server. This leads me to believe that there is an issue outside of the WordPress environment.

I'll try to debug this with my host and get back to you when I have the results.

#5 @Dekadinious
5 months ago

Well, before running this through the host i deactivated Object Caching, thus removing the drop-in, and updated all plugins and WP to 6.6.2. After enabling the drop-in again everything works.

I can't reproduce the error anymore and will file this under caching quirks!

#6 @peterwilsoncc
5 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed
  • Version 6.6.1 deleted

Thanks for following up, it's very helpful.

I'll close this ticket of as worksforme (unable to reproduce) but feel free to follow up on this ticket if you discover it is a bug in WordPress.

@peterwilsoncc commented on PR #7324:


5 months ago
#7

Reporter followed up to say they were unable to reproduce consistently.

However, I'll add this test to tests/phpunit/tests/term/cache.php as it doesn't appear to be in the tested use cases.

#8 @peterwilsoncc
5 months ago

I've decided to add the test anyway, as I couldn't find the use case in the term caching tests so it can't hurt to add them.

#9 @peterwilsoncc
5 months ago

In 59015:

Taxonomy: Test inserting a child term flushes queries by term ID.

Adds a test to ensure that interting a child term invalidates the cache of a get_terms() query by the parent ID.

Props Dekadinious, peterwilsoncc.
See #62031, #61530.

Note: See TracTickets for help on using tickets.