Architecture

This guide explains MilliCache's internal architecture for developers who want to extend or integrate with the plugin.

High-Level Architecture#Copied!

┌─────────────────────────────────────────────────────────────────────────┐
│                           WordPress Request                              │
└─────────────────────────────────────────────────────────────────────────┘
                                    │
                                    ▼
┌─────────────────────────────────────────────────────────────────────────┐
│                        advanced-cache.php                                │
│                     (Drop-in / Engine Entry)                            │
└─────────────────────────────────────────────────────────────────────────┘
                                    │
                                    ▼
┌─────────────────────────────────────────────────────────────────────────┐
│                              Engine                                      │
│                         (Singleton Core)                                │
│  ┌──────────────┐ ┌───────────────┐ ┌───────────────┐ ┌─────────────┐  │
│  │   Storage    │ │ FlagManager   │ │    Config     │ │   Options   │  │
│  │   (Redis)    │ │   (Flags)     │ │   (Settings)  │ │   (TTL)     │  │
│  └──────────────┘ └───────────────┘ └───────────────┘ └─────────────┘  │
│  ┌──────────────────────────────┐ ┌─────────────────────────────────┐  │
│  │     Cache Manager            │ │     Invalidation Manager        │  │
│  │  ┌────────┐ ┌────────┐      │ │  ┌────────┐ ┌────────────┐      │  │
│  │  │ Reader │ │ Writer │      │ │  │ Queue  │ │  Resolver  │      │  │
│  │  └────────┘ └────────┘      │ │  └────────┘ └────────────┘      │  │
│  └──────────────────────────────┘ └─────────────────────────────────┘  │
│  ┌─────────────────────────────────────────────────────────────────┐   │
│  │                   Request/Response Processing                    │   │
│  │  ┌───────────────────────┐    ┌──────────────────────────────┐  │   │
│  │  │  Request Processor    │    │    Response Processor        │  │   │
│  │  │  ┌────────┐ ┌───────┐ │    │    ┌────────┐ ┌────────┐    │  │   │
│  │  │  │ Parser │ │Hasher │ │    │    │ State  │ │Headers │    │  │   │
│  │  │  └────────┘ └───────┘ │    │    └────────┘ └────────┘    │  │   │
│  │  └───────────────────────┘    └──────────────────────────────┘  │   │
│  └─────────────────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────────────────┘
                                    │
                                    ▼
┌─────────────────────────────────────────────────────────────────────────┐
│                           MilliRules Engine                              │
│  ┌───────────────────────┐        ┌──────────────────────────────────┐ │
│  │   Bootstrap Rules     │        │     WordPress Rules              │ │
│  │   (Pre-WordPress)     │        │     (Post-WordPress)             │ │
│  └───────────────────────┘        └──────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────┘

Directory Structure#Copied!

src/
├── MilliCache.php              # Main plugin class
├── Engine.php                  # Cache engine (singleton)
├── Core/
│   ├── Loader.php              # WordPress hook orchestrator
│   ├── Settings.php            # Configuration management
│   └── Storage.php             # Redis connection & operations
├── Admin/
│   ├── Admin.php               # Admin UI controller
│   ├── CLI.php                 # WP-CLI commands
│   ├── RestAPI.php             # REST endpoints
│   ├── Adminbar.php            # Admin bar integration
│   ├── Activator.php           # Plugin activation
│   └── Deactivator.php         # Plugin deactivation
├── Engine/
│   ├── Cache/
│   │   ├── Config.php          # Cache configuration
│   │   ├── Manager.php         # Cache operations orchestrator
│   │   ├── Reader.php          # Read from cache
│   │   ├── Writer.php          # Write to cache
│   │   ├── Entry.php           # Cache entry model
│   │   ├── Validator.php       # Cache validation
│   │   ├── Result.php          # Cache operation result
│   │   └── Invalidation/
│   │       ├── Manager.php     # Invalidation orchestrator
│   │       ├── Queue.php       # Invalidation queue
│   │       └── Resolver.php    # Target resolution
│   ├── Request/
│   │   ├── Processor.php       # Request handling
│   │   ├── Parser.php          # Parse request data
│   │   ├── Cleaner.php         # Clean/normalize request
│   │   └── Hasher.php          # Generate cache key
│   ├── Response/
│   │   ├── Processor.php       # Response handling
│   │   ├── State.php           # Response state machine
│   │   └── Headers.php         # HTTP header management
│   ├── Flags.php               # Flag manager
│   ├── Options.php             # Runtime option overrides
│   └── Utilities/
│       ├── Multisite.php       # Multisite helpers
│       ├── PatternMatcher.php  # Wildcard matching
│       └── ServerVars.php      # Server variable access
└── Rules/
    ├── Bootstrap.php           # Pre-WordPress rules
    ├── WordPress.php           # Post-WordPress rules
    ├── RequestFlags.php        # Flag assignment
    └── Actions/
        ├── PHP/                # Bootstrap phase actions
        │   ├── DoCache.php
        │   ├── SetTtl.php
        │   └── SetGrace.php
        └── WP/                 # WordPress phase actions
            ├── AddFlag.php
            ├── RemoveFlag.php
            ├── ClearCache.php
            └── ClearSiteCache.php

Key Classes#Copied!

Engine (Singleton)#Copied!

The central coordinator. Manages all subsystems and orchestrates the caching flow.

File: src/Engine.php

 1// Recommended: Use the millicache() helper
 2$engine = millicache();
 3
 4// Or access Engine directly
 5$engine = MilliCacheEngine::instance();
 6
 7// Access subsystems
 8$engine->storage();    // Redis connection
 9$engine->flags();      // Flag manager
10$engine->config();     // Configuration
11$engine->options();    // Runtime overrides
12$engine->cache();      // Cache manager
13$engine->clear();      // Invalidation manager
14$engine->rules();      // Rules manager

Storage#Copied!

Handles all Redis operations.

File: src/Core/Storage.php

 1$storage = $engine->storage();
 2
 3// Low-level operations
 4$storage->set_cache( $key, $data, $flags );
 5$storage->get_cache( $key );
 6$storage->delete_cache( $key );
 7$storage->is_connected();
 8$storage->get_status();

Settings#Copied!

Configuration management with multi-source resolution.

File: src/Core/Settings.php

 1$settings = new MilliCacheCoreSettings();
 2
 3// Get settings
 4$ttl = $settings->get( 'cache.ttl' );
 5$all = $settings->get_settings( 'cache' );
 6
 7// Set settings
 8$settings->set( 'cache.ttl', 3600 );
 9
10// Import/Export
11$settings->export( 'cache' );
12$settings->import( $data );

Cache Manager#Copied!

Coordinates cache read/write operations.

File: src/Engine/Cache/Manager.php

 1$cache = $engine->cache();
 2
 3// Check for cached content
 4$result = $cache->get( $request_hash );
 5
 6// Store content
 7$cache->set( $request_hash, $content, $headers, $flags );

Invalidation Manager#Copied!

Handles cache clearing with queuing and resolution.

File: src/Engine/Cache/Invalidation/Manager.php

 1$invalidation = $engine->clear();
 2
 3// Clear by various targets
 4$invalidation->posts( [ 1, 2, 3 ] );
 5$invalidation->flags( [ 'home', 'archive:post' ] );
 6$invalidation->urls( [ 'https://example.com/' ] );
 7$invalidation->sites( [ 1, 2 ], $network_id );
 8$invalidation->networks( 1 );
 9$invalidation->all();

Flag Manager#Copied!

Manages cache flags for the current request.

File: src/Engine/Flags.php

 1$flags = $engine->flags();
 2
 3// Manage flags
 4$flags->add( 'custom:my-flag' );
 5$flags->remove( 'home' );
 6$flags->get_all();
 7$flags->has( 'post:123' );

Request Processor#Copied!

Parses and normalizes incoming requests.

File: src/Engine/Request/Processor.php

 1$request = new MilliCacheEngineRequestProcessor( $config );
 2
 3// Get cache key
 4$hash = $request->get_hash();
 5
 6// Check if cacheable
 7$cacheable = $request->is_cacheable();

Request Flow#Copied!

Cache Hit Flow#Copied!

1. advanced-cache.php
   └─► Engine::start()
       └─► Bootstrap rules evaluated
           └─► Request parsed and hashed
               └─► Cache lookup (Redis GET)
                   └─► HIT: Decompress + send headers + output + exit

Cache Miss Flow#Copied!

1. advanced-cache.php
   └─► Engine::start()
       └─► Bootstrap rules evaluated
           └─► Request parsed and hashed
               └─► Cache lookup (Redis GET)
                   └─► MISS: Continue to WordPress

2. plugins_loaded hook
   └─► WordPress rules registered

3. template_redirect hook (priority 200)
   └─► WordPress rules evaluated
       └─► Output buffering starts

4. shutdown hook
   └─► Output buffer captured
       └─► Response validated
           └─► Flags collected
               └─► Cache stored (Redis SET)
                   └─► Output sent to browser

Extension Points#Copied!

Hooks for Integration#Copied!

 1// Before cache storage
 2add_action( 'millicache_entry_storing', function( $hash, $key, $flags, $data ) {
 3    // Modify or log before storage
 4}, 10, 4 );
 5
 6// After cache storage
 7add_action( 'millicache_entry_stored', function( $hash, $key, $flags, $data ) {
 8    // Notify external systems
 9}, 10, 4 );
10
11// After cache clearing
12add_action( 'millicache_cache_cleared', function( $expire ) {
13    // Clear CDN, notify systems
14} );
15
16// Custom flags
17add_filter( 'millicache_flags_for_request', function( $flags ) {
18    // Add custom flags
19    return $flags;
20} );

Custom Rule Actions#Copied!

 1// Create a custom action
 2class MyCustomAction implements MilliRulesContractsAction {
 3    public function execute( $context ) {
 4        // Custom logic
 5    }
 6}
 7
 8// Register with MilliRules
 9use MilliRulesRules;
10
11Rules::add( [
12    'id'      => 'mysite:custom',
13    'condition' => [ 'request:path', 'matches', '/special/*' ],
14    'actions' => [ new MyCustomAction() ],
15    'order'   => 10,
16    'phase'   => 'wp',
17] );

Dependencies#Copied!

External#Copied!

Package Purpose
predis/predis Redis client
millipress/millirules Rules engine

WordPress#Copied!

Component Usage
Drop-in API advanced-cache.php
Options API Settings storage
Transients API Backup storage
REST API Remote management
WP-CLI Command line
Hooks API Integration points

Performance Considerations#Copied!

Early Exit#Copied!

Cache hits exit before WordPress loads:

  • No database queries
  • No plugin/theme code
  • Minimal memory usage

Lazy Loading#Copied!

Subsystems initialize on first use:

 1// Storage isn't connected until the first cache operation
 2$engine->storage()->get_cache( $key );

Efficient Clearing#Copied!

Invalidation uses Redis patterns:

 1// Single Redis command clears matching keys
 2KEYS mll:flags:post:123:*
 3DEL [matching keys]

Next Steps#Copied!