swiss-devjoy/blade-staticcache-directive

Put chunks of your Blade template into immutable static cache files

v1.0.0 2025-04-16 15:42 UTC

This package is auto-updated.

Last update: 2025-04-16 19:36:34 UTC


README

Latest Version on Packagist Total Downloads

Increase performance by putting chunks of your Blade template into immutable static cache files. This package provides a directive for Blade templates that allows you to easily create static cache files for specific sections of your views. With OPCache enabled, this can significantly reduce the time it takes to render your views, especially for large and complex templates.

Installation

You can install the package via composer:

composer require swiss-devjoy/blade-staticcache-directive

You can publish the config file with:

php artisan vendor:publish --tag="blade-staticcache-directive-config"

This is the contents of the published config file:

return [
    'enabled' => env('BLADE_STATICCACHE_DIRECTIVE_ENABLED', true),

    // Cache profile which generates the unique key for the cache entry
    'cache_profile' => \SwissDevjoy\BladeStaticcacheDirective\CacheProfile::class,
];

Usage

This is a blade template.

@staticcache('my-cache-key')
    {{ $this->aVeryExpensiveMethod() }}
@endstaticcache

To clear the cache you can run the following command:

$ php artisan blade-staticcache:clear

To include some additional stats in your response about cached/uncached blade chunks, you can use the provided middleware:

For laravel 11.x and newer:

Add the middleware definition to the bootstrap app.

// bootstrap/app.php


->withMiddleware(function (Middleware $middleware) {
    ...
    $middleware->web(append: [
        ...
        \SwissDevjoy\BladeStaticcacheDirective\BladeStaticcacheStatsMiddleware::class,
    ]);
})

For laravel 10.x and earlier:

Add the middleware definition to the http kernel.

// app/Http/Kernel.php

...

protected $middlewareGroups = [
   'web' => [
       ...
       \SwissDevjoy\BladeStaticcacheDirective\BladeStaticcacheStatsMiddleware::class,
   ],

Cache Profile

The cache profile is responsible for generating the unique key for the cache entry. By default, it uses the \SwissDevjoy\BladeStaticcacheDirective\CacheProfile class, which generates a key based on the cache key parameter passed to the @staticcache directive AND the current locale.

Inspiration

The main idea came from a tweet (https://x.com/dgurock/status/1577314908982706176) and the following package: https://github.com/ryangjchandler/blade-cache-directive

I did some basic benchmarks with a huge template and a lot of data.

Using Ryan's package and redis as a cache driver, I got 85 req/s. Using Ryan's package and file as a cache driver, I got 99 req/s. Using my package, I got 110 req/s.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.