API Reference

This reference documents all public PHP functions and REST API endpoints.

PHP Functions#Copied!

All functions are defined in functions.php and are available when the MilliCache Engine loads, even before WordPress.

Cache Clearing Functions#Copied!

millicache_reset_cache()

Clear all cache entries.

 1millicache_reset_cache( bool $expire = false ): bool

Parameters:

  • $expire - If true, expire entries (serve stale during regen) instead of delete

Returns: bool - Success status

Example:

 1// Delete all cache immediately
 2millicache_reset_cache();
 3
 4// Expire all cache (serve stale while regenerating)
 5millicache_reset_cache( true );

millicache_clear_cache()

Clear cache by mixed targets.

 1millicache_clear_cache( string|array $targets, bool $expire = false ): bool

Parameters:

  • $targets - Flag(s), URL(s), or post ID(s) as string or array
  • $expire - Expire instead of delete

Returns: bool - Success status

Example:

 1// Clear by single flag
 2millicache_clear_cache( 'home' );
 3
 4// Clear by multiple targets
 5millicache_clear_cache( [ 'post:123', 'home', 'archive:post' ] );
 6
 7// Clear by URL
 8millicache_clear_cache( 'https://example.com/about/' );

millicache_clear_cache_by_urls()

Clear cache by URLs.

 1millicache_clear_cache_by_urls( array $urls, bool $expire = false ): bool

Parameters:

  • $urls - Array of URLs to clear
  • $expire - Expire instead of delete

Returns: bool - Success status

Example:

 1millicache_clear_cache_by_urls( [
 2    'https://example.com/',
 3    'https://example.com/about/',
 4    'https://example.com/contact/',
 5] );

millicache_clear_cache_by_post_ids()

Clear cache by post IDs.

 1millicache_clear_cache_by_post_ids( array $post_ids, bool $expire = false ): bool

Parameters:

  • $post_ids - Array of post IDs
  • $expire - Expire instead of delete

Returns: bool - Success status

Example:

 1millicache_clear_cache_by_post_ids( [ 1, 2, 3 ] );
 2
 3// With expiration
 4millicache_clear_cache_by_post_ids( [ 123 ], true );

millicache_clear_cache_by_flags()

Clear cache by flags.

 1millicache_clear_cache_by_flags(
 2    array $flags,
 3    bool $expire = false,
 4    bool $add_prefix = false
 5): bool

Parameters:

  • $flags - Array of cache flags
  • $expire - Expire instead of delete
  • $add_prefix - Add site/network prefix (multisite)

Returns: bool - Success status

Example:

 1// Clear by flags
 2millicache_clear_cache_by_flags( [ 'home', 'archive:post' ] );
 3
 4// With site prefix (multisite)
 5millicache_clear_cache_by_flags( [ 'home' ], false, true );

millicache_clear_cache_by_site_ids()

Clear cache by site IDs (multisite).

 1millicache_clear_cache_by_site_ids(
 2    array $site_ids,
 3    int $network_id = null,
 4    bool $expire = false
 5): bool

Parameters:

  • $site_ids - Array of site IDs
  • $network_id - Specific network ID (optional)
  • $expire - Expire instead of delete

Returns: bool - Success status

Example:

 1// Clear specific sites
 2millicache_clear_cache_by_site_ids( [ 1, 2, 3 ] );
 3
 4// Clear sites in specific network
 5millicache_clear_cache_by_site_ids( [ 1, 2 ], 1 );

millicache_clear_cache_by_network_id()

Clear cache for entire network (multisite).

 1millicache_clear_cache_by_network_id( int $network_id, bool $expire = false ): bool

Parameters:

  • $network_id - Network ID to clear
  • $expire - Expire instead of delete

Returns: bool - Success status

Example:

 1millicache_clear_cache_by_network_id( 1 );

Flag Management Functions#Copied!

millicache_add_flag()

Add a flag to the current request.

 1millicache_add_flag( string $flag ): void

Parameters:

  • $flag - Flag to add

Example:

 1// In theme or plugin
 2if ( is_product() ) {
 3    millicache_add_flag( 'woo:product' );
 4    millicache_add_flag( 'woo:product:' . get_the_ID() );
 5}

millicache_remove_flag()

Remove a flag from the current request.

 1millicache_remove_flag( string $flag ): void

Parameters:

  • $flag - Flag to remove

Example:

 1// Remove home flag from the custom homepage
 2if ( is_front_page() && get_option( 'custom_homepage' ) ) {
 3    millicache_remove_flag( 'home' );
 4}

millicache_prefix_flags()

Add prefix to multiple flags.

 1millicache_prefix_flags(
 2    array $flags,
 3    int $site_id = null,
 4    int $network_id = null
 5): array

Parameters:

  • $flags - Array of flags to prefix
  • $site_id - Site ID
  • $network_id - Network ID

Returns: array - Prefixed flags

Example:

 1$flags = [ 'post:123', 'home' ];
 2$prefixed = millicache_prefix_flags( $flags, 2 );
 3// Result: [ '2:post:123', '2:home' ]

Cache Configuration Functions#Copied!

millicache_set_ttl()

Override TTL for current request.

 1millicache_set_ttl( int $ttl ): void

Parameters:

  • $ttl - Time-to-live in seconds

Example:

 1// Short TTL for dynamic page
 2if ( is_page( 'live-scores' ) ) {
 3    millicache_set_ttl( 60 );  // 1 minute
 4}

millicache_set_grace()

Override grace period for current request.

 1millicache_set_grace( int $grace ): void

Parameters:

  • $grace - Grace period in seconds

Example:

 1// Long grace for important pages
 2if ( is_front_page() ) {
 3    millicache_set_grace( 86400 * 7 );  // 7 days
 4}

REST API#Copied!

All endpoints require authentication via nonce (X-WP-Nonce header).

GET /wp-json/millicache/v1/status#Copied!

Get plugin and cache status.

Capability Required: manage_options

Parameters:

  • network (optional) - Set to "true" for network stats in multisite

Response:

 1{
 2    "plugin_name": "millicache",
 3    "version": "1.0.0",
 4    "cache": {
 5        "entries": 142,
 6        "size": 2856432,
 7        "size_h": "2.7 MB"
 8    },
 9    "storage": {
10        "connected": true,
11        "version": "7.2.4",
12        "memory": {
13            "used": 12500000,
14            "max": 268435456
15        }
16    },
17    "dropin": {
18        "status": "symlink",
19        "outdated": false
20    },
21    "settings": {
22        "has_defaults": false,
23        "has_backup": true
24    }
25}

POST /wp-json/millicache/v1/cache#Copied!

Perform cache actions.

Capability Required: Filter millicache_clear_cache_capability (default: publish_pages)

Actions:

clear

Clear all cache.

 1{
 2    "action": "clear",
 3    "is_network_admin": false
 4}

clear_current

Clear current page by flags.

 1{
 2    "action": "clear_current",
 3    "request_flags": ["post:123", "home"]
 4}

clear_targets

Clear by mixed targets.

 1{
 2    "action": "clear_targets",
 3    "targets": ["post:123", "https://example.com/page/"]
 4}

Response:

 1{
 2    "success": true,
 3    "message": "The site cache has been cleared.",
 4    "action": "clear",
 5    "timestamp": 1699900000
 6}

POST /wp-json/millicache/v1/settings#Copied!

Perform settings actions.

Capability Required: manage_options

Actions:

reset

Reset settings to defaults.

 1{
 2    "action": "reset"
 3}

restore

Restore from backup.

 1{
 2    "action": "restore"
 3}

Response:

 1{
 2    "success": true,
 3    "message": "Settings reset successfully.",
 4    "action": "reset",
 5    "timestamp": 1699900000
 6}

Code Examples#Copied!

Complete Cache Integration#Copied!

 1<?php
 2/**
 3 * Custom cache integration for a membership site.
 4 */
 5
 6// Add custom flags for membership content
 7add_filter( 'millicache_flags_for_request', function( $flags ) {
 8    if ( is_singular() && has_membership_content() ) {
 9        $flags[] = 'membership:content';
10        $flags[] = 'membership:level:' . get_membership_level();
11    }
12    return $flags;
13} );
14
15// Clear membership content when the level changes
16add_action( 'membership_level_changed', function( $user_id, $new_level ) {
17    millicache_clear_cache_by_flags( [
18        'membership:level:' . $new_level
19    ] );
20}, 10, 2 );
21
22// Short TTL for dynamic membership pages
23add_action( 'template_redirect', function() {
24    if ( is_page( 'member-dashboard' ) ) {
25        millicache_set_ttl( 300 );  // 5 minutes
26    }
27} );
28
29// Clear cache via REST API
30add_action( 'rest_api_init', function() {
31    register_rest_route( 'my-plugin/v1', '/clear-membership-cache', [
32        'methods'  => 'POST',
33        'callback' => function() {
34            millicache_clear_cache_by_flags( [ 'membership:*' ] );
35            return [ 'success' => true ];
36        },
37        'permission_callback' => function() {
38            return current_user_can( 'manage_options' );
39        },
40    ] );
41} );

Bulk Operations#Copied!

 1<?php
 2/**
 3 * Bulk cache operations.
 4 */
 5function clear_category_cache( $category_id ) {
 6    $flags = [
 7        "archive:category:{$category_id}",
 8        'archive:post',
 9        'home',
10    ];
11
12    // Get posts in category
13    $posts = get_posts( [
14        'category'       => $category_id,
15        'posts_per_page' => -1,
16        'fields'         => 'ids',
17    ] );
18
19    foreach ( $posts as $post_id ) {
20        $flags[] = "post:{$post_id}";
21    }
22
23    millicache_clear_cache_by_flags( array_unique( $flags ) );
24}

Next Steps#Copied!