swisnl/laravel-static-request-cache

Cache static responses based on content-type to static files.

1.3.0 2020-09-30 11:11 UTC

This package is auto-updated.

Last update: 2021-11-07 12:45:13 UTC


README

Latest Version on Packagist Software License Buy us a tree Build Status Coverage Status Quality Score Total Downloads Made by SWIS

Install

Via Composer

$ composer require swisnl/laravel-static-request-cache

Setup

Add the middleware to the end of your Http/Kernel.php middleware array.

protected $middleware = [
   \Swis\Laravel\StaticRequestCache\Http\Middleware\CacheMiddleware::class,
];

Add the following snippet into your .htaccess

# Rewrite to html cache if it exists and the request is for a static page
# (no url query params and only get requests)
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{DOCUMENT_ROOT}/static/html%{REQUEST_URI} -f
RewriteRule ^(.*)$  /static/html%{REQUEST_URI} [L]

RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{DOCUMENT_ROOT}/static/html%{REQUEST_URI}/index.html -f
RewriteRule ^(.*)$ /static/html%{REQUEST_URI}/index.html [L]

Disabling

If you want to disable the cache for some reason (the content might be dynamic), you can use the StaticRequestCache singleton in the IoC:

public function __construct(StaticRequestCache $staticRequestCache)
{
    $this->staticRequestCache = $staticRequestCache;
    $this->staticRequestCache->disable();
}

Or use the Facade:

StaticRequestCache::disable();

Please note that this package also checks for Cache-control headers and caches accordingly. You can change this behaviour in the config by editing non_cacheable_cache_control_values.

Clear the files

To clear all the files manually you can use an artisan command.

$ php artisan static-html-cache:clear

Change log

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

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email security@swis.nl instead of using the issue tracker.

Credits

Based on mscharl/laravel-static-html-cache. Added configuration for setting cacheable content-type and non-cacheable cache control values.

License

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

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

SWIS ❤️ Open Source

SWIS is a web agency from Leiden, the Netherlands. We love working with open source software.