WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 7 months ago

#15565 new enhancement

More context for clean_post_cache()

Reported by: mdawaffe Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.1
Component: Cache API Keywords: 3.2-early needs-patch
Focuses: Cc:

Description

I'd like more context to be available when the clean_post_cache hook is run.

Scenario: I have a plugin caches the post_IDs of most post queries that go through WP_Query. Invalidating that cache is done via the clean_post_cache hook, but requires a bunch of fragile hacks to prevent cache invalidation for things like comment inserts, which update the post's comment_count (which could, in theory, affect a WP_Query, but that's another story).

Option 1: Add extra actions to provide context. This is the simpler option. Patch 1 does this for the above scenario.

Option 2: Add an optional context parameter to clean_post_cache(). This is more general, but I can't think of anyplace else WordPress uses as similar approach. Patch two.

Attachments (3)

15565.1.diff (597 bytes) - added by mdawaffe 3 years ago.
Option 1
15565.2.diff (2.3 KB) - added by mdawaffe 3 years ago.
Option 2
15565.diff (1.7 KB) - added by wonderboymusic 8 months ago.

Download all attachments as: .zip

Change History (15)

mdawaffe3 years ago

Option 1

mdawaffe3 years ago

Option 2

comment:1 mdawaffe3 years ago

Option 2 also provides a way to detect if clean_post_cache() is being run recursively, another case that the scenario I mentioned above has to hack around.

comment:2 scribu3 years ago

We have a $context arg for escaping functions like sanitize_term(), but it's just a string.

comment:3 westi3 years ago

  • Keywords 3.2-early added
  • Milestone changed from Awaiting Review to Future Release

Option 2 looks good.

We should add context to all call to clean_post_cache so that a caching plugin can always know what is going on.

comment:4 batmoo22 months ago

  • Cc batmoo@… added

comment:5 johnjamesjacoby22 months ago

Note: this would be great for bbPress and anywhere custom post-types are being used creatively. If you invalidate tens of thousands of child-post caches, you're going to have a bad time.

comment:6 nacin20 months ago

I like this in theory. What would all of the contexts look like, for the various uses of clean_post_cache()?

comment:7 nacin20 months ago

  • Type changed from feature request to enhancement

comment:9 ryan18 months ago

Related: #22176

wonderboymusic8 months ago

comment:10 wonderboymusic8 months ago

  • Milestone changed from Future Release to 3.7

This may no longer be a thing since nacin fixed clean_post_cache() in 3.5, but I refreshed the patch against trunk - let's make a decision on it in 3.7

comment:11 nacin8 months ago

It might still be helpful to include a context for some uses of clean_post_cache(), but it no longer needs to be an array as it is no longer called in recursive fashion.

It seems like the only context you may wish to know is comment_count. Other kinds of cache clears (post and attachment CRUD, set_post_type(), publishing a post) should really not be messed with. Even post reassignments in wp_delete_user() should probably be cleared, though those have the potential to be expensive so that might be the only other situation where context is desired.

comment:12 nacin7 months ago

  • Keywords needs-patch added; has-patch removed
  • Milestone changed from 3.7 to Future Release
Note: See TracTickets for help on using tickets.