Skip to content

Cache

config/cache.php

The cache configuration file defines which cache drivers are active and what flush rules fire on WordPress events.

List of cache driver classes to use. Each driver implements CacheDriverContract (flushPost, flushUrl, flushAll).

W3TCDriver — flushes via W3 Total Cache (w3tc_flush_post, w3tc_flush_url, w3tc_flush_all). No-ops gracefully if W3TC is not active.

TransientDriver — deletes WordPress transients by prefix, then calls wp_cache_flush(). Construct with a prefix:

new TransientDriver('my_prefix')

flushPost and flushUrl are no-ops by default — subclass and override if your transients are keyed by post ID or URL.

Implement App\Lib\Contracts\CacheDriverContract:

use App\Lib\Contracts\CacheDriverContract;
class MyDriver implements CacheDriverContract
{
public function flushPost(int $postId): void {}
public function flushUrl(string $url): void {}
public function flushAll(): void {}
}

Then add the class to drivers:

config/cache.php
return [
'drivers' => [
MyDriver::class,
// or pass an already-constructed instance
new TransientDriver('my_prefix'),
],
];

Each rule maps a WordPress event (trigger) to one or more flush targets (flush). An optional filters key narrows which events fire the rule.

TriggerWordPress hookfilters keys
post_savedsave_post / save_post_{post_type}post_type
post_deletedbefore_delete_postpost_type
post_trashedtrashed_postpost_type
term_savededited_{taxonomy}, created_{taxonomy}taxonomy (required)
term_deletedpre_delete_termtaxonomy (required)
options_savedacf/save_post (options page only)

Autosaves and revisions are automatically skipped for post_saved.

Each entry in flush is either a string shorthand or an array selector.

String shorthands

ValueWhat flushes
'self'The saved/deleted post itself (flushPost($postId))
'all'Everything (flushAll())
'term_archive'The term’s archive URL (flushUrl(get_term_link($termId)))

Array selectors

['template' => 'news'] // all pages using templates/news.php
['template' => 'blog.php'] // pass full filename to skip auto-prefix
['url' => '/some/path'] // absolute or relative URL
['url' => 'https://...'] // full URL
['post_id' => 42] // specific post by ID
['page_slug' => 'contact'] // page looked up via get_page_by_path()
config/cache.php
return [
'drivers' => [
W3TCDriver::class,
],
'rules' => [
// flush the post itself + the news listing page when a post is saved
[
'trigger' => 'post_saved',
'filters' => ['post_type' => PostTypeConstant::POST],
'flush' => [
'self',
['template' => 'news'],
],
],
// flush a specific URL when any page is saved
[
'trigger' => 'post_saved',
'filters' => ['post_type' => 'page'],
'flush' => [
'self',
['url' => '/sitemap.xml'],
],
],
// flush the term's archive when a category is edited or created
[
'trigger' => 'term_saved',
'filters' => ['taxonomy' => TaxonomyConstant::CATEGORY],
'flush' => ['term_archive'],
],
// flush everything when ACF options are saved
[
'trigger' => 'options_saved',
'flush' => ['all'],
],
],
];