Download the PHP package joostvanveen/laravel-litespeedcache without Composer
On this page you can find all versions of the php package joostvanveen/laravel-litespeedcache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download joostvanveen/laravel-litespeedcache
More information about joostvanveen/laravel-litespeedcache
Files in joostvanveen/laravel-litespeedcache
Package laravel-litespeedcache
Short Description Laravel implementation of joostvanveen/litespeedcache.
License MIT
Informations about the package laravel-litespeedcache
joostvanveen/laravel-litespeedcache
A Laravel wrapper for joostvanveen/litespeedcache. If you wish to use Litespeed Cache outside of Laravel check out this framework agnostic https://github.com/joostvanveen/litespeedcache
Installation
Require the package using composer:
Enable the Litespeed in your .htaccess file.
Usage
This package adds a LitespeedCache
cache facade for easy use and sets middleware that automatically caches all requests, according to the config settings.
You can set which URLs (for instance any adminconsole*
) and query strings not to cache in config.
If you want to preview a page without cache, simply add ?cache_bypass=1
to a URL, or set a cache_bypass
cookie with a vale of 1.
If you want to set your own middleware, you can set the package to not use the default middleware.
Configuration
To be able to adjust the configuration for this package, publish the configuration files to your project's /config
folder like so:
The config file adds the following settings to your Laravel configuration:
Facade
The package registers \Joostvanveen\Litespeedcache\Cache
as a facade and sets default config values for
litespeedcache.defaults.enabled
,
litespeedcache.defaults.type
,
litespeedcache.defaults.lifetime
,
litespeedcache.defaults.excludedUris
,
and litespeedcache.defaults.excludedQueryStrings
.
You can use all methods from \Joostvanveen\Litespeedcache\Cache
, see https://github.com/joostvanveen/litespeedcache/blob/master/README.md for full documentation.
Some examples:
Middleware
By default, the package contains a middleware that caches all pages (except cli).
You can find this middleware at src/Middlewares/Cache.php
.
If you want to use your own middleware, you can disable the default middleware by setting the config value litespeedcache.defaults.use_middleware
to false
-
Publish the config settings to your Laravel project.
-
Set
use_middleware
tofalse
-
Create a middleware class
- Edit your middleware
Cache and csrf tokens in form requests
Laravel has built in security to guard against csrf attacks, using a csrf token. A token check is done on every POST request.
Typically, you provide the csrf token as a hidden field in a form, using csrf_field()
or in a POST ajax request,
using something like $.post(route('my.route'), $('#form').serialize() + "&_token={{ csrf_token() }});
This poses a problem when used with a full page cache like Litespeed Cache or Varnish. The csrf is unique and should not be stored in cache. If it is you will be posting a cached csrf token instead of a fresh one, resulting in a 'This page has expired' error on doing a POST request. However, you do want to store the rest of the page in cache. You need some way to do hole punching in your cached page. ESI to the rescue.
This package provides two ways to use csrf tokens in you pages as an ESI block. On constructing the cached page, the ESI block will be replaced by the actual, uncached csrf token. This way you can have a fully cached paged, but with a uncached token.
Use the following code to include a hidden field in a form on a cached page, instead of csrf_field()
. The 'litespeedcache.routes.field' route
is part of this package an will return a hidden field with csrf token.
ESI helper functions
This package comes with an easy helper functions to display an ESI block in your form.
This will display the proper ESI block if ESI is enabled, and a regular crsf_field()
if ESI is not enabled.
Sometimes you need just the token, for instance for ajax POST requests. That's why there is also a special helper function that returns just the token.
This will display the proper ESI block if ESI is enabled, and a regular crsf_token()
if ESI is not enabled.
Of course, you are free to manually code the ESI blocks into your forms as well.
ESI not supported by your server?
Not all Litespeed servers support ESI. If you are on a Litespeed server that does not support ESI, you can insert an uncached token into your form using ajax, since ajax requests are never cached by joostvanveen/litespeecache.
joostvanveen/litespeedcache documentation
You can find the Litespeed Cache documentation here https://github.com/joostvanveen/litespeedcache/blob/master/README.md
Litespeed documentation
You can find the Litespeed Cache documentation here https://www.litespeedtech.com/support/wiki/doku.php/litespeed_wiki:cache:developer_guide:response_headers
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
Changelog
Changelog
License
MIT