ez-php / view-cache
Output-caching decorator for ez-php/view's ViewEngine, with mtime-based invalidation
Requires
- php: ^8.5
- ez-php/view: ^2.0
Requires (Dev)
- ez-php/cache: ^2.0
- ez-php/docker: ^2.0
- friendsofphp/php-cs-fixer: ^3.94
- phpstan/phpstan: ^2.1
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^13.0
Suggests
- ez-php/cache: Needed for CacheViewCacheStore (entries in Redis/Memcached/… instead of files)
Provides
None
Conflicts
None
Replaces
None
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