General: Improve caching in get_calendar function#11547
General: Improve caching in get_calendar function#11547MarcinDudekDev wants to merge 1 commit intoWordPress:trunkfrom
Conversation
Replace the monolithic calendar cache (single array storing all variations) with individual cache keys per calendar variation, reducing deserialization overhead on cache reads. Switch cache invalidation from `save_post` and `delete_post` hooks to `clean_post_cache`, which avoids unnecessary cache flushes on autosaves, revision cleanup, and draft saves. Use `wp_cache_flush_group()` to invalidate all calendar cache entries, with a generation counter fallback for object cache implementations that do not support group flushing. Replace MySQL date arithmetic query with PHP native `gmdate()` to eliminate an unnecessary database roundtrip for week-to-month calculation. Props spacedmonkey, narenin. Fixes #61343. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Summary
save_post/delete_posttoclean_post_cache— avoids unnecessary flushes on autosaves, revisions, and draft saveswp_cache_flush_group('calendar')for invalidation with a generation counter fallback for object cache implementations lacking group flush supportDATE_FORMAT/DATE_ADD) with PHP nativegmdate()— eliminates unnecessary DB roundtripTechnical Details
Cache Architecture (before → after)
Before: All calendar variations stored in one serialized array under key
get_calendar. Every read deserializes all months/post-types. Every write re-serializes the entire array.delete_get_calendar_cache()nukes everything viawp_cache_delete.After: Each variation gets its own cache key (
get_calendar_{hash}). Reads fetch only the requested entry.delete_get_calendar_cache()useswp_cache_flush_group('calendar')to clear all entries.Persistent Cache Compatibility
Object cache implementations that don't support
flush_groupget a generation counter fallback:delete_get_calendar_cache()increments a generation value, and the cache key includes that generation — effectively invalidating all previous entries without needing to enumerate them.Raw SQL Queries Preserved
The ticket suggested replacing
$wpdbqueries withWP_Query, butSELECT DISTINCT DAYOFMONTH(post_date)returns max 31 rows whileWP_Querywould load all posts in the month. The existing PR #6708 makes this replacement and introduces a performance regression. We keep the efficient raw queries.Trac ticket: https://core.trac.wordpress.org/ticket/61343
Supersedes: #6708
Test plan
delete_get_calendar_cache()flush_groupclears all variations at oncedelete_get_calendar_cache()function signature unchangedAI assistance: Yes
Tool(s): Claude Code (Anthropic)
Model(s): Claude Opus 4.6
Used for: Performance analysis, identifying regression in existing PR #6708, implementation of cache restructure with generation counter fallback, and writing unit tests. All code was reviewed and tested.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.
🤖 Generated with Claude Code