wedevelopnl/silverstripe-fpc-purge 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

wedevelopnl/silverstripe-fpc-purge

Composer 安装命令:

composer require wedevelopnl/silverstripe-fpc-purge

包简介

FPC purge module

README 文档

README

This module adds some cache purging capabilities to the website, to support FPC in nginx or apache.

Requirements

  • See composer.json requirements
  • nginx with Lua module

Installation

  • composer require wedevelopnl/silverstripe-fpc-purge

Configuring nginx

Install the latest version of ngx_cache_purge.

Then update your server configuration:

fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=fastcgicache:100m max_size=5g inactive=60m use_temp_path=off;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

server {
    location = /purge-cache {
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_cache fastcgicache;
        fastcgi_cache_purge PURGE purge_all from 127.0.0.1;
        cache_purge_response_type text;
    }

    location /index.php {
        fastcgi_buffer_size 32k;
        fastcgi_busy_buffers_size 64k;
        fastcgi_buffers 4 32k;
        fastcgi_keep_conn on;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;

        # fastcgi caching
        fastcgi_cache fastcgicache;
        fastcgi_cache_valid 200 301 302 60m;
        fastcgi_cache_use_stale error timeout updating invalid_header http_500 http_503;
        fastcgi_cache_min_uses 1;
        fastcgi_cache_lock on;
        fastcgi_no_cache $cookie_PHPSESSID;
        fastcgi_cache_bypass $cookie_PHPSESSID;
        fastcgi_ignore_headers Set-Cookie;

        add_header X-Cache $upstream_cache_status;
    }

    # ...
}

NOTE: Consider randomizing or otherwise protecting your URL to prevent abuse.

Configuring the module

---
Name: 'fpc-purge-config'
Only:
  environment: 'live'
---
WeDevelop\FPCPurge\FPCPurgeConfig:
  enabled: true
  endpoints:
    # Purging locally over HTTP
    - host: localhost:80
      method: PURGE
      path: /purge-cache
    # Purging locally over HTTPS
    - host: tls://localhost:443
      http_host: example.com # Required to tell nginx or apache what virtual host you want to connect to
      method: PURGE
      path: /purge-cache
    # Purging externally over HTTPS
    - host: tls://example.com:443
      method: PURGE
      path: /purge-cache
    # Purging a specific server (useful when load balancing and purging all servers)
    - host: tls://10.0.0.5:443
      http_host: example.com # Required to tell nginx or apache what virtual host you want to connect to
      method: PURGE
      path: /purge-cache

Page:
  extensions:
    - WeDevelop\FPCPurge\Extensions\FPCPurgeExtension

Here you can enable the module and configure the endpoint used to purge.

You can test this configuration by going into the SilverStripe admin, then click FPC Purge in the sidebar and click the Purge Cache button. It should tell you if it was successful.

We also add an extension to Page to purge the cache after publishing a page.

NOTE: The purge after publishing opens a connection, then sends a non-blocking request, should have little to no impact on publishing performance depending on the endpoints.

If you're using dnadesign/silverstripe-elemental don't forget to also apply the extension on BaseElement to make sure cache is purged after publishing an element.

DNADesign\Elemental\Models\BaseElement:
  extensions:
    - WeDevelop\FPCPurge\Extensions\FPCPurgeExtension

Setting up Cache Control

All of the above will not cache anything until you setup cache control. You can either follow the official SilverStripe docs, or use the extension included in this module for an easier foolproof implementation.

PageController:
    extensions:
        - WeDevelop\FPCPurge\Extensions\FPCPurgeControllerExtension

Now you have to add a updateCacheControl() method to your PageController and configure the CacheControl headers.

public function updateCacheControl(): void
{
    HTTPCacheControlMiddleware::singleton()
        ->enableCache()
        ->setSharedMaxAge(3600)
        ->setMaxAge(60);
}

Shared Max Age: the amount of time in seconds this page is allowed to be cached in your FPC (nginx, apache, etc.)
Max Age: the amount of time in seconds this page is allowed to be cached in the browser

If you have another controller that extends the PageController but serves more dynamic data from an API for example, you can override the CacheControl headers in that controller by overriding the updateCacheControl method.

public function updateCacheControl(): void
{
    HTTPCacheControlMiddleware::singleton()
        ->enableCache()
        ->setSharedMaxAge(600)
        ->setMaxAge(0);
}

Here we set the max age to 0 to prevent it from being cached by the browser, and a relatively low shared max age. This way cache can only be stale for 10 minutes.

Sessions and CSRF tokens

It's important not to cache pages that are generated within the context of a session, for example a logged in user or a CSRF token. Luckily, there are two things protecting us from this mistake.

  1. SilverStripe will overrule our cache control headers when a session is active.
  2. The nginx configuration triggers a bypass when a PHPSESSID is found.

Default configuration

WeDevelop\FPCPurge\FPCPurgeConfig:
  enabled: false
  endpoints: []

License

See License

Maintainers

Development and contribution

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. See read our contributing document for more information.

Getting started

We advise to use Docker/Docker compose for development.
We also included a Makefile to simplify some commands

Our development container contains some built-in tools like PHPCSFixer.

Getting development container up

make build to build the Docker container and then run detached.
If you want to only get the container up, you can simply type make up.

You can SSH into the container using make sh.

All make commands

You can run make help to get a list with all available make commands.

wedevelopnl/silverstripe-fpc-purge 适用场景与选型建议

wedevelopnl/silverstripe-fpc-purge 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 199 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 01 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 wedevelopnl/silverstripe-fpc-purge 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 5
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-01