Skip to content

[codex] Cache prepared get_blocks results per request#372

Merged
nk-o merged 4 commits into
masterfrom
codex/request-cache-get-blocks
Jun 10, 2026
Merged

[codex] Cache prepared get_blocks results per request#372
nk-o merged 4 commits into
masterfrom
codex/request-cache-get-blocks

Conversation

@Fellan-91

Copy link
Copy Markdown
Contributor

Summary

  • cache prepared get_blocks() results for the current PHP request
  • keep $no_cache = true as a full bypass and clear the request cache when it is used
  • clear the request cache when programmatic blocks are added, removed, or when the existing block cache is cleared
  • document the performance fix in the changelog and readme

Problem

Sites that register many blocks programmatically with lazyblocks()->add_block( $block_config ) can call get_blocks() repeatedly during a single request. Each call prepared the same user blocks again, so pages with 50+ blocks could spend significant time rebuilding identical block data.

Solution

The plugin now stores the prepared get_blocks() result in an in-memory request-level cache keyed by db_only and keep_duplicates. The public lzb/get_blocks filter still runs on every return, while the heavier lzb/block_data preparation work is reused within the request.

$no_cache = true continues to bypass the cache and now clears any prepared request cache so forced rebuilds remain fresh.

Validation

  • npm run test:unit:php -- --filter BlockRegistrationTest
  • npm run test:unit:php
  • checked compatibility against Lazy Blocks Pro relationship rebuilds
  • manually profiled a LocalWP site with the Pro core patched temporarily: repeated lzb/block_data preparation dropped from about 380 calls per request to about 19

Note: PHPUnit still reports existing config validation warnings for processUniqueFiles and <filter>.

@Fellan-91 Fellan-91 marked this pull request as ready for review May 12, 2026 13:30
@nk-o nk-o merged commit 7a88a61 into master Jun 10, 2026
7 of 8 checks passed
@nk-o nk-o deleted the codex/request-cache-get-blocks branch June 10, 2026 13:39
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.

2 participants