vatia/laravel-varnish

让 Varnish 和 Laravel 顺利协同工作

2.0.2 2017-10-17 16:51 UTC

This package is auto-updated.

Last update: 2024-09-08 22:32:38 UTC


README

Latest Version on Packagist Build Status SensioLabsInsight Quality Score StyleCI Total Downloads

本软件包为在 Laravel 5.2 和 PHP 版本 >= 7.0.0 中使用 Varnish 4(或 5)提供了一种简单的方法。它提供了一个路由中间件,当应用于路由时,将确保 Varnish 缓存响应。该软件包还包含一个从应用程序内部刷新 Varnish 缓存的功能。

Postcardware

您可以自由使用此软件包(它遵循MIT 许可证),但如果它进入您的生产环境,我们非常欢迎您从家乡寄给我们一张明信片,注明您正在使用我们的哪些软件包。

我们的地址是:Spatie,Samberstraat 69D,2060 安特卫普,比利时。

最佳明信片将被发布在我们的网站开源页面上。

安装

我们假设您已经在服务器上安装了 Varnish。如果没有,请阅读这篇博客文章了解如何安装它。

您可以通过 composer 安装此软件包

composer require vatia/laravel-varnish

首先:注册服务提供者

// config/app.php

'providers' => [
    ...
    Spatie\Varnish\VarnishServiceProvider::class,
];

接下来,您必须使用以下命令发布配置文件:

php artisan vendor:publish --provider="Spatie\Varnish\VarnishServiceProvider" --tag="config"

这是发布文件的內容

return [

    /*
     * The location of the file containing the administrative password.
     */
    'administrative_secret' => '/etc/varnish/secret',

    /*
     * The host where the administrative tasks may be sent to.
     */
    'administrative_host' => '127.0.0.1',


    /*
     * 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',
];

在发布的 laravel-varnish.php 配置文件中,您应将 host 键设置为正确的值。

Spatie\Varnish\Middleware\CacheWithVarnish 中间件添加到路由中间件

// app/Http/Kernel.php

protected $routeMiddleware = [
...
   'cacheable' => \Spatie\Varnish\Middleware\CacheWithVarnish::class,
];

最后,您应该在您的 VCL 中的 vcl_backend_response 函数中添加以下行(默认情况下,它位于您的服务器上的 /etc/varnish/default.vcl

if (beresp.http.X-Cacheable ~ "1") {
    unset beresp.http.set-cookie;
}

我们强烈推荐使用由 varnish-5.0-configuration-templates 仓库 提供的 VCL,由 Mattias Geniar 制作。

用法

缓存响应

应该缓存响应的路由应使用 cacheable 中间件。

// 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');

配置 Varnish 缓存此内容的分钟数可以在 laravel-varnish.php 配置文件中的 cache_time_in_minutes 键中进行。或者,您也可以使用中间件参数来指定此值。

// Varnish will cache the responses of the routes inside the group for 15 minutes
Route::group(['middleware' => 'cacheable:15'], function() {
   ...
)};

在幕后,中间件将向响应添加 X-CacheableCache-Control。Varnish 将从 Laravel 的响应中删除所有 cookie。因此,请注意,由于 laravel_session cookie 也将被删除,因此在这些路由上应用 CacheWithVarnish 中间件时,会话将不起作用。

从 Varnish 清除缓存

有一个 artisan 命令用于刷新缓存。这可以在您的部署脚本中很有用。

php artisan varnish:flush

您也可以在代码中这样做来刷新缓存

(new Spatie\Varnish\Varnish())->flush();

您可以使用以下代码清除请求 URL 的缓存

(new Spatie\Varnish\Varnish())->flush("http://example.com/page");

变更日志

请参阅CHANGELOG 以获取有关最近更改的更多信息。

测试

$ composer test

贡献

请参阅CONTRIBUTING 了解详细信息。

安全

如果您发现任何与安全相关的问题,请通过电子邮件freek@spatie.be 而不是使用问题跟踪器。

鸣谢

关于 Spatie

斯派蒂是一家位于比利时的安特卫普的网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述在这里

许可协议

MIT 许可协议(MIT)。有关更多信息,请参阅许可文件