Search by

ez-php / view-cache

AU9500

Output-caching decorator for ez-php/view's ViewEngine, with mtime-based invalidation

Package info

github.com/ez-php/view-cache

pkg:composer/ez-php/view-cache

Statistics

Installs: 25

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

2.5.6 2026-09-30 18:37 UTC

This package is auto-updated.

Last update: 2026-10-04 02:27:23 UTC


README

Output-caching decorator for ez-php/view's ViewEngine. Caches a rendered template's output to a file, keyed by template name and render data, and invalidates it based on the source template file's mtime.

Installation

composer require ez-php/view-cache

Usage

use EzPhp\View\ViewEngine;
use EzPhp\ViewCache\CachingViewEngine;

$viewPath = __DIR__ . '/resources/views';
$engine = new CachingViewEngine(
    new ViewEngine($viewPath),
    $viewPath,
    __DIR__ . '/storage/view-cache',
);

echo $engine->render('home', ['title' => 'Welcome']);

render() mirrors ViewEngine::render()'s signature, so CachingViewEngine is a drop-in substitute anywhere a plain ViewEngine is used directly. A cached entry is served back as long as the source template file's mtime hasn't changed since it was written; editing the template invalidates it automatically.

Invalidating on partial and layout changes

By default only the top-level template's mtime is checked, so editing a partial or layout alone does not refresh a cached parent. Pass trackDependencies: true to record every layout and partial a render pulls in (recursively) and re-render when any of them changes:

$engine = new CachingViewEngine(new ViewEngine($viewPath), $viewPath, $cachePath, trackDependencies: true);

// Share rendered output between servers: any ez-php/cache driver instead of a directory
use EzPhp\ViewCache\CacheViewCacheStore;

$engine = new CachingViewEngine(new ViewEngine($viewPath), $viewPath, new CacheViewCacheStore($cache, ttl: 3600));

Requires an ez-php/view release that provides ViewEngine::onResolve().

Wiring this into an application's dependency container (e.g. binding CachingViewEngine instead of ViewEngine behind an environment flag) is left to the application — this module has no framework dependency beyond ez-php/view itself.

License

MIT