Repository navigation
Block cache
Some blocks are expensive every time they render: a product grid, a query loop, a list of latest posts. The block cache keeps the rendered HTML of those blocks in Redis and replays it.
It works alongside the page cache. The page cache saves the whole page for guests. The block cache saves the slow parts of a page for requests the page cache cannot serve, and makes a page cache miss cheaper.
It is off by default and needs WordPress 6.5 or later. Turn it on under Caching > Block Cache.
Each group has its own switch:
| Group | Blocks |
|---|---|
| WooCommerce | Product Collection, Handpicked, Newest, On Sale, Best Sellers, Top Rated and Products by Category. Shown only when WooCommerce is active. |
| Query Loop | Query Loop blocks that do not inherit the page's own query |
| Latest Posts | The Latest Posts block |
| Custom | Blocks you add with a filter |
- Blocks that read the page or URL: Query Loops that inherit the page query, related products, upsells, cross-sells, filterable collections, the product catalogue.
- Anything containing a mini-cart, filters, sorting or a comment form.
- Any render that put a security token in its HTML or state.
Anonymous visitors with an empty basket, on plain GET requests. Visitors with something in the basket and signed-in users get a live render.
Cache for logged-in users (off by default) caches per role, and only for blocks whose output can only vary by role. WooCommerce blocks are treated as per-user when role-based, wholesale or membership pricing is active, or when tax depends on the customer's address.
A block is more than its HTML. On a hit the plugin also replays what the render added elsewhere: Interactivity API state and config, styles, scripts, script modules, WooCommerce settings data, block-supports CSS and inline CSS. The visitor's basket state is loaded fresh, so add to basket, quantities, variations and the mini-cart keep working.
Stored copies rename WordPress's per-request numbered classes (wp-elements-N, wp-container-N), so a replayed block can never share a number with another block on the page and pick up its styles.
Every post or product read during a render is recorded against the block. Then:
- Saving, restocking or repricing one of them deletes just the blocks that used it.
- Changes that can alter which posts a block shows (publish, unpublish, trash, term changes, title, date or order, featured, visibility, sale dates) retire that block group.
- Entries expire after the TTL: 12 hours by default.
The pages that showed an affected block are purged through the page cache, and in Varnish when it is in use.
Hits, misses, average render time and database queries are counted per block. The settings panel shows today and the last seven days, with the render time and queries saved.
wp aceredis block-cache stats --days=7Also at GET /wp-json/ace-redis-cache/v1/block-cache/stats for administrators.
wp aceredis block-cache flushOr the Flush block cache button, or POST /wp-json/ace-redis-cache/v1/block-cache/flush. A full cache clear retires every block entry too.
The X-Ace-Block-Cache response header lists hit, miss, skip or live-<reason> for each cached block type on the page. Send the request header X-Ace-Block-Cache-Debug: 1 to include the block names.
The simplest route adds block names to the Custom group:
add_filter( 'aceredis_block_cache_custom_blocks', function ( $blocks ) {
$blocks[] = 'my-plugin/team-list';
return $blocks;
} );For more control, add a profile:
add_filter( 'aceredis_block_cache_profiles', function ( $profiles ) {
$profiles['hub'] = array(
'label' => 'News hub',
'setting' => 'block_cache_custom',
'blocks' => array( 'my-plugin/news-hub' ),
'dep_types' => null,
'role_safe' => true,
'vary_page' => true,
'ttl' => 90,
'retire_on_publish' => false,
);
return $profiles;
} );| Key | Meaning |
|---|---|
blocks |
Block names the profile covers |
setting |
The settings switch that turns it on |
ttl |
Seconds, capped at the global block cache TTL |
vary_page |
The block reads the page, so the key includes the queried object, the post in the loop and the page number |
vary |
A callable given the parsed block, returning extra key parts |
role_safe |
Output can only vary by role, so it may be cached for signed-in users |
retire_on_publish |
false makes the profile rely on its TTL. Use it for busy listings where a short lag is fine. |
A TTL-only profile is still refreshed when a post it showed is edited, and you can retire it yourself:
\AceMedia\RedisCache\BlockCache::instance()->bump( 'hub' );| Switch | Effect |
|---|---|
define( 'ACEREDIS_BLOCK_CACHE', false ); |
Off, whatever the setting says. true forces it on. |
aceredis_block_cache_enabled |
Filter the on/off state |
aceredis_block_cache_cacheable |
Decide per block |
aceredis_block_cache_woo_blocks |
The WooCommerce block list |
aceredis_block_cache_context_blocks |
Blocks that read the page and so are not cached |
aceredis_block_cache_vary |
Extra key parts for every block |
aceredis_block_cache_woo_role_safe, aceredis_block_cache_custom_role_safe
|
Whether a group is safe to cache per role |
Ace Redis Cache: a page cache and object cache for WordPress, backed by Redis or Valkey. Written and maintained by Shane Rounce at AceMedia. Website · Repository · Issues · GPLv2 or later
Setting up
The caches
Your stack
Reference
Help
