定制 bagisto/bagisto-varnish 二次开发

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

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

bagisto/bagisto-varnish

Composer 安装命令:

composer require bagisto/bagisto-varnish

包简介

A Bagisto package that integrates Varnish Cache for enhanced performance with full-page caching and dynamic content handling.

README 文档

README

This package integrates Varnish Cache with Bagisto to boost site performance by delivering cached pages quickly, while still supporting dynamic components through ESI (Edge Side Includes) or AJAX-based dynamic views.

📌 Features

  • Full-page caching via Varnish
  • 🔄 Automatic cache purging on product, category, or content updates
  • 🔍 ESI support for dynamic blocks
  • 🖱 AJAX-based dynamic view loading for improved Core Web Vitals
  • 🛠 Artisan commands for cache management
  • 🖥 Admin tools for purging cache & exporting VCL
  • 🎨 Theme-ready Blade components
  • 🛡 Middleware for cache headers (cacheable & non-cacheable routes)

🔄 Request Flow

image

Explanation:

  • 443 (HTTPS)Nginx handles SSL termination and forwards traffic.
  • 80 (HTTP)Varnish Proxy caches and serves pages, or passes the request to the backend.
  • 8080Bagisto (Laravel app) generates fresh content when needed.

Flow Summary: Browser → Nginx → Varnish Proxy → Bagisto → Response (cached or fresh)

📦 Installation

1. Install via Composer

composer require bagisto/bagisto-varnish

2. Register the Service Provider

In bootstrap/providers.php:

Note: Autoloading via Composer’s package auto-discovery is not possible for this provider. The registry order matters—VarnishServiceProvider must be listed after the Shop package or at the end of the providers array. Auto-discovery would load it too early, which can cause issues.

'providers' => [
    Webkul\Varnish\Providers\VarnishServiceProvider::class,
],

3. Publish Assets & Config

php artisan vendor:publish --provider="Webkul\Varnish\Providers\VarnishServiceProvider"

⚙️ Varnish Server Configuration

  1. Install Varnish 6.x on your server.

  2. Replace /etc/varnish/default.vcl with the provided file:

    Varnish/vcls/6.0.vcl
    
  3. Restart Varnish:

    sudo systemctl restart varnish

🎨 Theme Integration

You can integrate dynamic content in two ways:

1 – Define Dynamic Views / Fragments

In config/varnish.php, define a key (identifier) and its corresponding Blade view path:

return [
    'esi' => [
        'views' => [
            ...

            'customer-desktop-dropdown' => 'varnish::shop.components.layouts.header.desktop.customer-dropdown',

            ...
        ]
    ],
];
  • Key → Used in ESI or AJAX call (customer-desktop-dropdown)
  • Path → Full Blade view path to render (varnish::...)

2 – ESI Include

<esi:include src="/esi?tag=customer-desktop-dropdown" />
  • Injects content at the Varnish level (server-side).
  • Appears immediately on page load.
  • May affect LCP/FCP if the backend is slow.

3 – AJAX Dynamic View (Recommended for LCP)

<x-varnish::dynamic-view view="customer-desktop-dropdown" />
  • Loads via AJAX after user interaction.
  • Improves LCP/FCP.
  • Ideal for non-critical dropdowns, modals, and menus.

🗂 Cache-Control Headers

For routes that should NOT be cached by Varnish:

Cache-Control: no-cache, no-store, must-revalidate

For routes that should be cached:

Cache-Control: public, max-age=604800, s-maxage=604800

(Example: 7 days)

Middleware for Cache Headers in Bagisto

We’ve created a middleware Webkul\Varnish\Http\Middleware\VarnishCache to handle cache headers.

Attach it to routes like this:

Route::get('/', [HomeController::class, 'index'])
    ->name('shop.home.index')
    ->middleware('cache.response');

🛠 UI Configuration (Export VCL)

Navigate to: Admin → Configuration → Cache Management → Varnish → Configuration

Select Varnish as the cache application, then provide the following:

  1. Access List – IPs allowed to purge the cache (e.g., localhost).
  2. Varnish Host URL – Varnish server IP and port for purging/banning cache via UI.
  3. Backend Host URL – Laravel Bagisto server IP used in the exported VCL.
  4. Backend Host Port – Laravel Bagisto server port used in the exported VCL.
  5. Grace Period – Duration for serving stale content if the backend is slow or unavailable.

🛠 Cache Management

Navigate to: Admin → Configuration → Cache Management → Varnish → Purge Cache

  1. Purge by URLs – Enter full URLs (comma-separated) to clear specific cache entries. Paths and domains must match exactly.
  2. Purge Everything – Clears all cache entries from Varnish. Use with caution, as it may temporarily affect performance.

🛡 Automatic Cache Purging

The package automatically purges cache when:

  • Products, categories, pages, orders, reviews, refunds, or theme changes occur.

You can also manually trigger purging by adding your own events in EventServiceProvider and calling:

VarnishCache::forget($urls);

🖥 Admin Panel Tools

  • Purge Full Cache
  • Purge by URL
  • Export VCL

🚀 Best Practices

  • Use ESI for small, critical personalized blocks (e.g., login status, cart count).
  • Use AJAX dynamic views for non-critical interactive elements to improve Core Web Vitals.
  • Set Cache-Control headers with appropriate TTL values to control caching behavior.
  • Always test on a staging environment before deploying to production.
  • Monitor LCP, FCP, and TTFB after enabling Varnish.

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 9
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-27