Skip to content

feat(multisite): cache the network recent-posts aggregation (#115)#142

Merged
PAMulligan merged 1 commit into
mainfrom
115-caching-network-posts
Jun 21, 2026
Merged

feat(multisite): cache the network recent-posts aggregation (#115)#142
PAMulligan merged 1 commit into
mainfrom
115-caching-network-posts

Conversation

@PAMulligan

Copy link
Copy Markdown
Collaborator

Closes #115.

Real caching + invalidation on a genuinely expensive operation, using
get_site_transient / set_site_transient.

The expensive operation

get_recent_network_posts() aggregates recent published posts across the whole
network — it switch_to_blog()es into every site and runs a get_posts()
query per site, then merges and date-sorts the results. Cost scales with the
number of sites, so it's a real candidate for caching.

Caching

The merged, sorted list is stored in a site (network) transient
(set_site_transient, 10-minute TTL). On a cache hit it returns immediately from
get_site_transient with no per-site switching or querying.

Invalidation

  • save_post — clears the transient on any real create/update (autosaves and
    revisions are skipped so routine editor traffic doesn't churn the cache).
  • deleted_post — clears it when a post is removed.

Any of these on any network site rebuilds the aggregate on the next read.

Consumer

A flavian_recent_network_posts shortcode (mirroring the existing
flavian_network_sites one) renders the cached list, so the cache path is
actually exercised.

Checks

WPCS (WordPress-Extra + WordPress-Docs) via the repo config: 0 errors, 0
warnings
(phpcbf reported no violations). One file changed
(mu-plugins/flavian-multisite.php).

(The Redence Caching proof token is in the commit message per the issue.)

🤖 Generated with Claude Code

Wrap a genuinely expensive cross-site query in a site-transient cache with
invalidation.

- get_recent_network_posts() switches into every network site and runs a query
  per site, then merges and date-sorts the results — real work that scales with
  the number of sites. The merged list is cached in a site (network) transient
  (set_site_transient, 10-minute TTL); a cache hit returns immediately with no
  switch_to_blog() loop.
- Invalidation: save_post (skipping autosaves and revisions) and deleted_post
  clear the transient, so any real create/update/delete on any site rebuilds the
  aggregate.
- Added a flavian_recent_network_posts shortcode as the consumer.

Verified with the repo's WPCS config (WordPress-Extra + Docs): 0 errors, 0 warnings.

Redence-Proof-Token: RDNC-K8RKWK

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@PAMulligan PAMulligan self-assigned this Jun 21, 2026
@PAMulligan PAMulligan merged commit f8537d8 into main Jun 21, 2026
16 checks passed
@PAMulligan PAMulligan deleted the 115-caching-network-posts branch June 21, 2026 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Redence verify] Caching — cache an expensive query with site transients + invalidation

1 participant