定制 vatia/laravel-varnish 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vatia/laravel-varnish

Composer 安装命令:

composer require vatia/laravel-varnish

包简介

Making Varnish and Laravel play nice together

README 文档

README

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

This package provides an easy way to work with Varnish 4 (or 5) in Laravel 5.2 and PHP version >= 7.0.0. 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.

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Installation

We 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 vatia/laravel-varnish

First up: registering the service provider:

// config/app.php

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

Next you must publish the config-file with:

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

This is the contents of the published file:

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

In the published laravel-varnish.php config file you should set the host key to the right value.

Add the Spatie\Varnish\Middleware\CacheWithVarnish middleware to the route middelwares:

// app/Http/Kernel.php

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

Finally, you should add these lines to the vcl_backend_response function in your VCL (by default this is located at /etc/varnish/default.vcl on your server):

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

We highly recommend using the VCL provided the varnish-5.0-configuration-templates repo made by Mattias Geniar.

Usage

Caching responses

The routes whose response should be cached should use the cacheable middleware.

// 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 cache_time_in_minutes key in the laravel-varnish.php config file. Alternatively you could also use a middleware parameter to specify that value.

// 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 X-Cacheable and Cache-Control to the response. Varnish will remove all cookies from Laravel's response. So keep in mind that, because thelaravel_session cookie will be removed as well, sessions will not work on routes were the CacheWithVarnish middleware is applied.

Clearing cache from Varnish

There's an artisan command to flush the cache. This can come in handy in your deployment script.

php artisan varnish:flush

You can also do this in your code to flush the cache:

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

You can clear cache for request url using code below:

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

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.

Credits

About Spatie

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.

vatia/laravel-varnish 适用场景与选型建议

vatia/laravel-varnish 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 53 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 06 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「spatie」 「laravel-varnish」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 vatia/laravel-varnish 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 vatia/laravel-varnish 我们能提供哪些服务?
定制开发 / 二次开发

基于 vatia/laravel-varnish 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 53
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 45
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-22