Introduction to Rules
Rules are MilliCache's second core feature. While Cache Flags handle what to clear, rules control when to cache.
Together, flags and rules make MilliCache incredibly flexible:
- Rules decide: Should this request be cached? For how long?
- Flags decide: When content changes, which cache entries are affected?
Why Rules?#Copied!
Every caching decision in MilliCache is a rule. This means you can:
- Override any default behavior
- Add your own conditions
- Customize for your specific use case
Think of it like smart home automation for your cache:
| Smart Home | MilliCache |
|---|---|
| "Turn off heating when I leave home" | "Bypass cache when user is logged in" |
| "Turn on lights when it's dark and motion detected" | "Bypass cache when it's a POST request and URL contains /checkout" |
| "Set temperature to 18° when it's after 10pm" | "Set TTL to 5 minutes when page is a product archive" |
How Rules Work#Copied!
Every rule has three parts:
flowchart LR
C[Condition] --> |matches| A[Action]
C --> |doesn't match| S[Skip rule]
A --> R[Continue to next rule]
| Component | Description | Example |
|---|---|---|
| Condition | When should this rule apply? | "If user is logged in" |
| Action | What should happen? | "Do not cache" |
| Priority | When to evaluate (lower = earlier) | 0/1 (built-in), 10+ (custom) |
The Fluent API#Copied!
Rules use a readable, chainable syntax powered by MilliRules:
1millicache()->rules()->create( 'mysite:example-rule' ) // Create rule with an ID
2 ->order( 10 ) // Set priority
3 ->when() // Start conditions
4 ->request_url( '/news/*' ) // Match URL pattern
5 ->then() // Start actions
6 ->set_ttl( 1800 ) // Set 30-minute TTL
7 ->register(); // Register the rule
The phase is picked for you: MilliCache reads the conditions and actions you used, and a rule that could only have run before WordPress is moved to the WordPress phase, since that earlier phase is over by the time your code runs.
One thing is on you: register after MilliCache has loaded. In a plugin or a theme
that is already the case. Only a file that runs earlier, such as a must-use plugin,
needs the registration wrapped in add_action( 'plugins_loaded', … ); see
where to put the code.
Prefer building rules without code? MilliCache Pro includes a visual Rules Builder: create, edit, and reorder caching rules directly in the settings screen, in addition to the PHP API.
Two Execution Phases#Copied!
MilliCache rules execute in two distinct phases:
flowchart TB
R[Request] --> A[advanced-cache.php]
A --> B[Bootstrap Rules<br/><i>PHP-Only Phase</i>]
B --> |Bypass| WP1[WordPress loads]
B --> |Continue| C{Cache Hit?}
C --> |Yes| S[Serve cached HTML]
C --> |No| WP2[WordPress loads]
WP2 --> D[WordPress Rules<br/><i>Full Context Phase</i>]
D --> |Bypass| E[Don't cache response]
D --> |Continue| F[Cache response]
Bootstrap Phase (php)#Copied!
Runs before WordPress loads:
- Instant decisions with minimal overhead
- No database queries
- Can only check: URL, cookies, headers, constants
This phase runs inside advanced-cache.php, before any plugin or theme exists, so
it cannot be reached from code. Bootstrap rules come from the settings, which the
drop-in reads from the database. Writing them takes
MilliCache Pro: either its
Rules Builder
or wp millicache rules import.
WordPress Phase (wp)#Copied!
Runs after WordPress loads:
- Full WordPress context available
- Can check: user roles, post types, templates, etc.
- More powerful but slightly later in the request
Built-in Rules#Copied!
MilliCache includes sensible defaults that you can override:
- Never cache POST requests
- Never cache logged-in users
- Never cache admin/CLI/REST/AJAX requests
- Respect
DONOTCACHEPAGEconstant - Honor excluded cookies and paths from settings
See Built-in Rules for the complete list.
What You Can Do#Copied!
With rules, for example, you can:
Control caching decisions:
1->then()->do_cache( false, 'Reason' ) // Bypass cache
2->then()->do_cache( true ) // Force cache (override previous)
Adjust cache timing:
1->then()->set_ttl( 3600 ) // Cache for 1 hour
2->then()->set_grace( 86400 ) // Allow stale for 1 day
Manage flags:
1->then()->add_flag( 'custom:flag' )
2->then()->remove_flag( 'home' )
Clear cache:
1->then()->clear_cache( ['post:123', 'home'] )
2->then()->clear_site_cache()
Learn More#Copied!
For deep documentation on the rules engine, conditions, actions, and patterns:
To manage rules visually instead, see the Rules Builder in MilliCache Pro.
Next Steps#Copied!
- Built-in Rules — All default rules and when they run
- Examples — Practical MilliCache rule examples
- Cache Flags — The partner feature to rules
Last updated:
Release updates
Get notified when new versions and guides ship. No spam.
By subscribing you agree to our Privacy Policy. Double opt-in, no spam, unsubscribe anytime.