在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):spatie/laravel-varnish开源软件地址(OpenSource Url):https://github.com/spatie/laravel-varnish开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):Making Varnish and Laravel play nice togetherThis package provides an easy way to work with Varnish 4 (or 5) in Laravel. It provides a route middleware that, when applied to a route, will make sure Varnish will cache the response no matter what. The package also contains a function to flush the Varnish cache from within the application. Support usWe invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products. We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall. InstallationWe assume that you've already installed Varnish on your server. If not read this blogpost to learn how to install it. You can install the package via composer: composer require spatie/laravel-varnish The package will automatically register itself for Laravel 5.5+. If you are using Laravel < 5.5, you also need to add \Spatie\Varnish\VarnishServiceProvider::class Next if you use Laravel you must publish the config-file with: php artisan vendor:publish --provider="Spatie\Varnish\VarnishServiceProvider" --tag="config" and if you use Lumen, you must copy This is the contents of the published file: return [
/*
* The hostname this Laravel app is listening to.
*/
'host' => 'example.com',
/*
* The location of the file containing the administrative password.
*/
'administrative_secret' => '/etc/varnish/secret',
/*
* The port where the administrative tasks may be sent to.
*/
'administrative_port' => 6082,
/*
* The default amount of minutes that content rendered using the `CacheWithVarnish`
* middleware should be cached.
*/
'cache_time_in_minutes' => 60 * 24,
/*
* The name of the header that triggers Varnish to cache the response.
*/
'cacheable_header_name' => 'X-Cacheable',
]; In the published Add the For Laravel: // app/Http/Kernel.php
protected $routeMiddleware = [
...
'cacheable' => \Spatie\Varnish\Middleware\CacheWithVarnish::class,
]; If you are using Lumen, you need to load config file before route middleware definition to your $app->configure('varnish');
$app->routeMiddleware([
...
'cacheable' => \Spatie\Varnish\Middleware\CacheWithVarnish::class,
]); Finally, you should add these lines to the
We highly recommend using the VCL provided the varnish-5.0-configuration-templates repo made by Mattias Geniar. UsageCaching responsesThe routes whose response should be cached should use the // your routes file
//will be cached by Varnish
Route::group(['middleware' => 'cacheable'], function() {
Route::get('/', 'HomeController@index');
Route::get('/contact', 'ContactPageController@index');
});
//won't be cached by Varnish
Route::get('do-not-cache', 'AnotherController@index'); The amount of minutes that Varnish should cache this content can be configured in the // Varnish will cache the responses of the routes inside the group for 15 minutes
Route::group(['middleware' => 'cacheable:15'], function() {
...
}); Behind the scenes the middleware will add an Clearing cache from VarnishThere's an artisan command to flush the cache. This can come in handy in your deployment script. php artisan varnish:flush Under the hood flushing the cache will call the You can also do this in your code to flush the cache: (new Spatie\Varnish\Varnish())->flush(); ChangelogPlease see CHANGELOG for more information what has changed recently. Testing$ composer test ContributingPlease see CONTRIBUTING for details. SecurityIf you've found a bug regarding security please mail [email protected] instead of using the issue tracker. CreditsLicenseThe MIT License (MIT). Please see License File for more information. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论