Performance & Debugging

Add Caching to a PHP Website

Choose safe cache layers and define expiration and invalidation for PHP application data.

Overview

Caching can reduce repeated computation and database load, but it introduces stale-data and invalidation problems. Start by identifying expensive work that is repeated and whose results can tolerate a defined freshness window.

Choose the right layer

HTTP caching can let browsers or proxies reuse public assets and safe public responses. Application caching can store computed data or query results. Opcode caching such as OPcache reduces PHP compilation overhead. These layers solve different problems and can be combined.

Define cache keys carefully

A cache key must include every input that changes the result: locale, filters, page number, permissions or tenant where relevant. Never share personalized data under a public cache key. Avoid putting secrets or raw personal data in cache keys.

Expiration and invalidation

Use a time-to-live as a safety boundary, but invalidate immediately when a critical update must become visible. Versioned keys can simplify broad invalidation. Document what happens when the cache is unavailable; the database or source of truth should remain authoritative.

HTTP caching basics

For public static assets, use long-lived cache headers with versioned filenames. For user-specific pages, use private or no-store directives as appropriate. Do not apply public caching to authenticated responses without a careful privacy review.

Measure the result

Track hit rate, latency and backend load. A cache with a low hit rate or expensive invalidation may add complexity without meaningful benefit.

Official references

PHP OPcache HTTP caching – MDN

Choose the cache layer deliberately

Opcode caching such as OPcache reduces PHP compilation work. Application caching stores computed values. HTTP caching lets browsers or intermediaries reuse responses. These solve different problems and should not be treated as interchangeable.

Define cache keys and freshness

A cache key must include every input that changes the result: locale, user permissions, query filters and relevant content version. Use a bounded TTL and explicit invalidation when data changes. Never share personalized responses through a public cache.

Cache only safe data

Good candidates include expensive public lookups, stable reference data and computed aggregates. Avoid caching authorization decisions longer than their security policy permits. Treat cache contents as disposable; the database remains the source of truth.

Prevent stampedes

When many requests miss the same key, they may all recalculate it. Use a short lock, stale-while-revalidate strategy or background refresh where appropriate. Keep a fallback path for cache outages so the website can continue operating.

Verify correctness

Test cache hits, misses, expiration, invalidation, user separation and behavior when the cache server is unavailable. A fast stale or cross-user response is still a bug.