Caching


The plugin uses a multi-layer caching strategy to minimize API calls and deliver fast page loads. Caching is largely automatic — most users don’t need to configure anything beyond enabling a persistent object cache.

Admin Menu: Settings & Tools > Settings (Caching section)

⚡ Cache Layers

1. Object Cache (Primary)

The plugin stores computed data (game cards, team pages, standings) in the WordPress object cache. This is the main performance layer.

  • Requires: A persistent object cache plugin (Redis or Memcached)
  • Without object cache: Data is recomputed on each page load — still functional, just slower
  • Cache groups: Organized by entity type (anwphub-game-*, anwphub-team-*, anwphub-season-*, etc.)

Recommended

Install Redis or Memcached for the best performance. Most managed WordPress hosts (Cloudways, GridPane, SpinupWP) include Redis. Check with your host if unsure.

2. Aggregate Stats Cache (Database)

Pre-computed statistics from the API (season averages, league rankings, advanced metrics) are stored in a dedicated database table — not in the object cache.

Why a separate table? Unlike object cache data, aggregate stats:

  • Survive wp cache flush and server restarts
  • Survive API downtime (stale data is always preserved)
  • Are expensive to re-fetch (each type costs an API call)

Stale-while-revalidate: When cached data is past its TTL, the plugin serves the cached version instantly and refreshes in the background. Visitors see data immediately, and it updates silently.

Season StateRefresh IntervalExample
Active (in-progress)Configurable (1-6 hours)NBA 2025/26 during season
Finished (recent)WeeklyMLB 2025 just ended
Archived (past year)MonthlyNBA 2024/25

3. Page Cache Support

If you use a page caching plugin (WP Super Cache, W3 Total Cache, LiteSpeed Cache, WP Rocket, etc.), the plugin can flush relevant cached pages when data updates. Supports 20+ caching plugins.

4. CDN / Browser Cache

REST API responses use immutable URLs with cache hashes, enabling aggressive browser and CDN caching. The hash changes when data is updated, so CDNs automatically serve fresh content.

🌐 Cache REST API with Cloudflare

The plugin loads dynamic content (live scores, standings, player stats, game cards) via the WordPress REST API. Cloudflare can cache these REST API responses at the edge, so visitors worldwide get near-instant responses without hitting your server.

Before Getting Started

  • Your domain must be proxied through Cloudflare (check DNS Records — “Proxy status” should show the orange cloud icon)
  • Cloudflare’s free plan allows up to 3 Page Rules per domain

Go to DNS > Records in the Cloudflare dashboard and verify that your domain’s A record shows Proxied status (orange cloud icon):

Cloudflare DNS settings showing Proxied status with orange cloud icon for the domain A records

Create Page Rule

  1. In the Cloudflare dashboard, navigate to Rules > Page Rules in the sidebar
  2. Click Create Page Rule
Cloudflare Page Rules page showing 0 of 3 available rules with Create Page Rule button highlighted
  1. Enter the URL pattern:
*yourdomain.com/wp-json/anwphub/*
Page Rule creation form with URL field and settings dropdown
  1. Add the following settings (use + Add a Setting to add each one):
  • Browser Cache TTL: 30 minutes
  • Cache Level: Cache Everything
  • Edge Cache TTL: 2 hours
Completed Page Rule form showing Browser Cache TTL 30 minutes, Cache Level Cache Everything, and Edge Cache TTL 2 hours
  1. Click Save and Deploy Page Rule

If you see a warning about “This rule may not apply to your traffic” — this is normal for wildcard URL patterns. Select “Ignore and deploy rule anyway” and click Deploy rule.

Cloudflare warning dialog about wildcard DNS with Ignore and deploy rule anyway option selected

How It Works

The plugin uses time-bucketed URLs for all REST API requests. Instead of requesting the same URL repeatedly, it appends a time-based parameter that changes at regular intervals:

/wp-json/anwphub/v1/calendar-games?date=2026-03-24&_t=56284

The _t parameter changes every few minutes (depending on the content type), so:

  • Within a time window: All visitors hit the Cloudflare cache (fast, no server load)
  • At the boundary: The first request fetches fresh data, Cloudflare caches it again
Content TypeCache Window
Live scores30 seconds
Game cards, calendars, player stats5 minutes
Aggregate stats (season averages, rankings)1–6 hours

This means live scores stay near real-time while static data benefits from long cache periods — all automatically.

Verify It Works

  1. Open your site with game or player pages loaded
  2. Open browser DevTools (F12) → Network tab
  3. Filter by Fetch/XHR
  4. Click any REST API request (URLs containing /wp-json/anwphub/)
  5. Check the Response Headers for Cf-Cache-Status

The first request will show MISS — the server responds and Cloudflare stores the result:

Browser DevTools Network tab showing Cf-Cache-Status MISS on the first REST API request

Reload the page. The same request now shows HIT — served directly from Cloudflare’s edge, no server load:

Browser DevTools Network tab showing Cf-Cache-Status HIT on the second REST API request

Check the Timing tab to see the speed difference — cached responses arrive in milliseconds:

Browser DevTools Timing tab showing fast cached response time in milliseconds
StatusMeaning
HITServed from Cloudflare edge cache (fast)
MISSFetched from your server (first request or new time window)
EXPIREDWas cached but expired, fetching fresh
DYNAMICNot cached — check your Page Rule setup

No Plugin Configuration Needed

The cache-busting URLs are built into the plugin — just set up the Cloudflare Page Rule. Live scores use short time windows (30s), so they update promptly even with edge caching. When you import new data or live scores change, the cache hash updates and Cloudflare serves fresh content automatically.

🔧 Cache Settings

Located in Settings & Tools > Settings under the Caching section.

Plugin Settings Caching section showing Object Cache status, Aggregate Stats Refresh, Full-Page Cache Compatibility, Page Cache Flush, and Flush All Caches button
SettingOptionsDefaultDescription
Object CacheNot Detected / Redis / MemcachedRead-only status indicator
Aggregate Stats RefreshEvery 1 / 2 / 4 / 6 hoursEvery 1 hourHow often aggregate stats refresh from the API
Full-Page Cache CompatibilityOn / OffOffEnable if your site uses full-page caching
Page Cache FlushOff / Per-Page / Full SiteOffAutomatically flush page cache when data changes
Flush All CachesButtonManually clear all plugin caches

Full-Page Cache Compatibility

Enable this if your site uses a full-page caching plugin or CDN. When enabled, the plugin uses a two-request pattern: the first request fetches a content hash, the second fetches actual data only if the hash has changed. This prevents stale content on cached pages.

Page Cache Flush

LevelBehavior
OffNo page cache integration
Per-PageFlushes only affected pages when data changes
Full SiteFlushes the entire page cache on any data change

Choose “Per-Page” for the best balance of freshness and performance. Use “Full Site” only if per-page flushing doesn’t work with your caching plugin.

Cache Invalidation

Cache is automatically flushed when:

  • New games are imported or updated
  • Live scores change
  • Background sync updates data
  • Standings or rosters are refreshed

Note

Aggregate stats (season rankings, team averages) are stored separately and are not cleared by the Flush All Caches button. They refresh on their own schedule based on the Aggregate Stats Refresh setting.

Manual flush: Go to Settings & Tools > Settings > Caching section and click Flush All Caches. Use this after bulk imports or if you notice stale data on the frontend.

📚 Related