swisnl / laravel-static-request-cache
Cache static responses based on content-type to static files.
Installs: 5 478
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^7.2|^8.0
- laravel/framework: ^6.0|^7.0|^8.0|^9.0|^10.0
Requires (Dev)
- graham-campbell/testbench: ^5.7
- mockery/mockery: ^1.0
- phpunit/phpunit: ^8.0|^9.5
- squizlabs/php_codesniffer: ^2.3
README
🚨 THIS PACKAGE HAS BEEN ABANDONED 🚨
We don't use this package anymore in our own projects — we switched to Nginx with FastCGI Caching — and cannot justify the time needed to maintain it anymore. That's why we have chosen to abandon it. Feel free to fork our code and maintain your own copy or use one of the many alternatives. We suggest using silber/page-cache instead as it is fairly similar.
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.