Features Pricing Reviews FAQ
Get Plugin

REST API Cache (Per-Route TTL)

4 min read

xSpeed Cache Pro can cache read-only REST API responses and let you set a different cache lifetime for each route. This is aimed at headless sites and app backends that read the same REST data often.

What this does

The Free cache engine can store REST responses, but it stays off until an add-on turns it on and supplies cache lifetimes. This Pro module is that add-on. It gives you a settings screen on top of the Free REST cache engine.

You set a default lifetime for all routes, then add specific rules for individual routes that should live longer or shorter. Only anonymous GET requests are cached. Authenticated requests and write requests are never cached, and the engine only stores successful responses.

You control the module from the REST API Cache panel inside the Advanced Cache group.

Why use it

Headless front ends and mobile apps often request the same REST data repeatedly. Without caching, each request runs the full WordPress REST handler.

Caching those responses means repeat reads are answered from a stored copy. Per-route lifetimes let you cache stable routes longer while keeping fast-changing routes fresh.

Requirements

  • xSpeed Cache Pro active.

How to set it up

  1. Go to the xSpeed Cache settings and open the Advanced Cache group.
  2. Find the REST API Cache panel.
  3. Turn on Enable REST API caching.
  4. Set the Default TTL (minutes) for routes that have no specific rule.
  5. Add route rules for any routes that need a different lifetime.
  6. Save your settings.

Settings explained

SettingWhat it doesDefault
Enable REST API cachingCaches anonymous GET REST responsesOff
Default TTL (minutes)Lifetime for routes that match no specific rule, from 0 to 1440 minutes (24 hours). 0 means unmatched routes are not cached5
Route rulesA table of route patterns, each with its own lifetime in minutesEmpty

Enable REST API caching

This is the main switch. When it is on, anonymous GET REST responses can be cached. Authenticated and write requests are never cached.

Default TTL (minutes)

This sets the lifetime for any route that does not match one of your specific rules. You can enter any value from 0 to 1440 minutes, and 1440 minutes is 24 hours.

Setting this to 0 means unmatched routes are not cached at all. In that case, only routes with an explicit rule get cached.

Route rules

Each rule pairs a route pattern with a lifetime in minutes. When a request comes in, xSpeed Cache checks the rules in order and the first matching rule wins. If no rule matches, the default TTL is used.

A rule with a wildcard, such as /wp/v2/*, matches using pattern matching. A plain rule with no wildcard, such as /wp/v2/posts, matches that exact route and its sub-paths. For example, /wp/v2/posts matches /wp/v2/posts and /wp/v2/posts/1, but not /myplugin/wp/v2/posts.

Each rule’s lifetime can be from 0 to 1440 minutes.

Note: Changing the enabled switch, the default TTL, or the route rules purges the REST cache. This means a lowered lifetime or a disabled cache takes effect right away instead of waiting for old entries to expire.

Best practices

  • Start with a modest default TTL, then add rules only for routes that need a different lifetime.
  • Give stable routes a longer lifetime and fast-changing routes a shorter one.
  • Set the default TTL to 0 if you want tight control, so only routes you name explicitly are cached.
  • Use a wildcard rule when you want one lifetime to cover a group of related routes.

Troubleshooting

A route is returning stale data. Lower that route’s lifetime, or the default TTL if the route has no rule. Saving the change purges the REST cache, so the update takes effect immediately.

A rule is not matching the route I expected. A plain pattern only matches the exact route and its sub-paths, anchored to the start of the route. If you need broader matching, use a wildcard such as /wp/v2/*.

Authenticated requests are not being cached. That is by design. Only anonymous GET requests are cached. Authenticated and write requests always run live.

FAQ

Does this cache logged-in or write requests? No. Only anonymous GET requests are cached. The engine also only stores successful responses.

What happens when a route matches no rule? It uses the default TTL. If the default is 0, it is not cached.

Which rule wins if more than one matches? The first matching rule in the list wins.

Do I have to purge the cache after changing settings? No. Changing the enabled switch, the default TTL, or the rules purges the REST cache automatically.

  • How to Enable Page Cache
  • Object Cache (Redis / Memcached)
  • Purging the Cache