delblue/laravel-varnish

使Varnish和Laravel协同工作

2.2.3 2018-09-12 12:57 UTC

This package is auto-updated.

Last update: 2024-09-13 02:03:20 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads

本软件包提供了一种简单的方法来在Laravel中使用Varnish 4(或5)。它提供了一个路由中间件,当应用于路由时,将确保Varnish无论何种情况都会缓存响应。该软件包还包含一个从应用程序内部清除Varnish缓存的功能。

安装

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

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

composer require deltablue/laravel-varnish

该软件包将自动为Laravel 5.5+注册自己。

如果您使用的是Laravel < 5.5,您还需要将Varnish\VarnishServiceProvider添加到您的config/app.php providers数组中

\DeltaBlue\Varnish\VarnishServiceProvider::class

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

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

如果使用Lumen,您必须将config/varnish.php文件复制到您的应用程序配置文件夹。

这是发布文件的包含内容

return [
    /*
     * The hostname(s) this Laravel app is listening to.
     */
    'host' => ['example.com'],

    /*
     * The execution type to be used. Allowed values are 'command' or 'socket'.
     *
     * This will determine whether `varnishadm` or the varnish administrative socket
     * is used for a local or remote varnish instance, respectively.
     */
    'execution_type' => 'command',

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

    /*
     * The actual administrative password used in your varnish configuration.
     *
     * When using `execution_type` 'command', use `administrative_secret`
     * instead, as `varnishadm` expects the secret to be a file path.
     *
     * If you are using `execution_type` 'socket', both parameters are supported, but
     * `administrative_secret_string` will take precedence over `administrative_secret`.
     */
    'administrative_secret_string' => '',

    /*
     * The host where the administrative tasks may be sent to when
     * using execution_type 'socket'.
     */
    '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',
];

在发布的varnish.php配置文件中,您应将host键设置为正确值。同时,如果您想使用此命令清除远程Varnish服务器的缓存,请确保将execution_type设置为socket。将execution_type设置为command以在本地系统上使用varnishadm

如果您将execution_type设置为socket,您可以将管理密钥存储在文件中并设置administrative_secret,如varnishadm使用,或者在实际的密钥字符串变量administrative_secret_string中提供实际的密钥字符串。

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

对于Laravel

// app/Http/Kernel.php
protected $routeMiddleware = [
...
   'cacheable' => \DeltaBlue\Varnish\Middleware\CacheWithVarnish::class,
];

如果您使用Lumen,您需要在定义路由中间件之前加载配置文件到您的bootstrap/app.php

$app->configure('varnish');
$app->routeMiddleware([
...
   'cacheable' => \DeltaBlue\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_sessioncookie也将被删除,因此在这些应用了CacheWithVarnish中间件的路径上,会话将不会工作。

从Varnish清除缓存

有一个Artisan命令用于清除缓存。这可以在您的部署脚本中派上用场。

php artisan varnish:flush

如果将 execution_type 设置为 command,则底层刷新缓存时会调用 sudo varnishadm。为了无任何困扰地运行该命令,请确保命令由具有 sudo 权限的 Unix 用户运行。

您也可以在代码中执行此操作以刷新缓存

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

更新日志

请参阅 更新日志 了解最近更改了哪些内容。

测试

$ composer test

贡献

有关详细信息,请参阅 贡献指南

致谢

许可证

MIT 许可证 (MIT)。请参阅 许可证文件 了解更多信息。