PHP Luminova: Template Caching and Static URLs
Cache rendered views and serve eligible requests early using static URL suffixes such as `.html`.
Template caching stores rendered content in writeable/caches/templates/, grouped by application version. Each entry includes its content type, expiration, ETag, and cache-control metadata. You can choose a different directory when constructing ViewCache directly.
| Request | What happens |
|---|---|
/catalog | The router dispatches the request to the controller. The view can reuse cached output. |
/catalog.html | With html configured as a static suffix, boot checks for a valid HTML cache entry before dispatching the request. |
Enable Page Caching
Add these settings to .env to cache eligible views for five minutes.
page.caching = true
page.cache.expiry = 300Cache or Exclude a View
Call cache(300) before rendering to explicitly cache a public view. Call cacheExclude() before rendering when an individual template should be excluded from normal caching.
use Luminova\Base\Http\AbstractController;
class CatalogController extends AbstractController
{
public function index(): int
{
$this->tpl->cache(300);
return $this->view('catalog/index', ['title' => 'Catalog']);
}
public function preview(): int
{
$this->tpl->cacheExclude('catalog/preview');
return $this->view('catalog/preview', ['title' => 'Catalog preview']);
}
}Explicit
cache()takes precedence over the exclusion rules. Keep forced caching off for error pages, login pages, and personalized responses. The normal cache policy already skips the standard4xxand5xxtemplates. Exclude custom error templates with$this->tpl->cacheExclude(['404', 'error']).
Serve Static-Suffix URLs
List the permitted suffixes in page.caching.statics, separated by pipes.
page.caching.statics = html|json|cssUse the corresponding view type when rendering. For example, render $this->view('catalog/index', type: 'html') for the route /catalog to make its cached output eligible for /catalog.html.
| Request | View type |
|---|---|
/catalog.html | html |
/catalog/settings.json | json |
/catalog/style.css | css |
Cache hit. Boot serves the saved response before route middleware and controller methods run. Use static-suffix URLs for public content that can be served without per-request checks.
Cache miss. Boot removes the configured suffix and continues normal routing. The controller determines the response type and whether to cache it. Appending .json does not convert an HTML response to JSON.
The normal URL and its static variant can share a cache key. Keep custom error responses out of the cache so an error produced during a miss cannot replace the intended page. If both URLs are publicly accessible, use one canonical URL in the page’s <head>.
<link rel="canonical" href="https://example.com/catalog">Configure Cache Lookup and Lifetime
| Setting | Effect |
|---|---|
page.cache.expiry | Lifetime in seconds. A value of 0 uses the five-year lifetime in ViewCache. |
page.cache.query.params | Include the query string in the cache key. Enable this when content varies by query parameter, such as /catalog?page=2. |
page.cache.app.versions | Search the listed older version directories when the current version has no matching file. |
page.cache.latest.content | Keep matching URI patterns on the current version. |
page.caching.immutable | Skip the cached entry’s time-based expiration check and add immutable to its Cache-Control header. |
default.cache.control | Default Cache-Control value used when building response headers. |
Reuse Cache From Older Releases
The current application version is checked first. For example, an application on version 1.2.0 can search older caches in this order while keeping selected routes current.
page.cache.app.versions = [1.1.0, 1.0.0]
page.cache.latest.content = ['/', 'blog', 'blog/*/foo', 'foo/*']Versioned URLs can keep distinct releases under separate cache keys. An unversioned URL can reuse the same key in an older release directory when fallback is enabled.
Cache Public Content for a Long Time
Use page.caching.immutable = true for public content whose URL changes when the content changes. The cache uses the entry’s lifetime for max-age. Configure long-lived headers separately for static assets served directly by the web server. Version asset URLs, for example /assets/v-1.2.0/logo.png, so updated assets have a fresh URL.
Performance Example
These measurements come from one example run. Compare the modes with your own application before using the numbers as a baseline.
| Mode | Execution | Database | Memory | Files |
|---|---|---|---|---|
| Cache disabled | 96.41 ms | 11.92 ms | 2.61 MB | 58 |
| Normal view cache | 26.61 ms | 0 ms | 1.26 MB | 38 |
| Static-suffix cache hit | 10.63 ms | 0 ms | 202.21 KB | 22 |