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?#

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#

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) -10 (early), 0 (normal), 10+ (custom)

The Fluent API#

Rules use a readable, chainable syntax powered by MilliRules:

 1use MilliCacheDepsMilliRulesRules;
 2
 3Rules::create( 'mysite:example-rule', 'php' )  // Create rule with ID and phase
 4    ->order( 10 )                              // Set priority
 5    ->when()                                   // Start conditions
 6        ->request_url( '/news/*' )             // Match URL pattern
 7    ->then()                                   // Start actions
 8        ->set_ttl( 1800 )                      // Set 30-minute TTL
 9    ->register();                              // Register the rule

Two Execution Phases#

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)#

Runs before WordPress loads:

  • Instant decisions with minimal overhead
  • No database queries
  • Can only check: URL, cookies, headers, constants

WordPress Phase (wp)#

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#

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 DONOTCACHEPAGE constant
  • Honor excluded cookies and paths from settings

See Built-in Rules for the complete list.

What You Can Do#

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#

For deep documentation on the rules engine, conditions, actions, and patterns:

MilliRules Documentation

Next Steps#