somardigital/silverstripe-cloudfront 问题修复 & 功能扩展

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

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

somardigital/silverstripe-cloudfront

Composer 安装命令:

composer require somardigital/silverstripe-cloudfront

包简介

SilverStripe extension to automatically invalidate CloudFront cache when pages and files are published, unpublished, or deleted

README 文档

README

Invalidate CloudFront cache when pages and files are published, unpublished, or deleted in Silverstripe CMS.

Problem Statement

When editors publish, unpublish, or delete pages and files, the public-facing site does not update because CloudFront is still serving the old, cached version. This requires manual cache clearing, which is inefficient and error-prone.

Solution

This module provides:

  • InvalidateCacheExtension - Hooks into Silverstripe ORM events to invalidate matching dataobject
  • ElementalInvalidateCacheExtension - On elemental block publish clear page cache
  • MemberSessionCookieExtension - Sets a cookie to bypass standard cached pages
  • CloudfrontService - Handles AWS CloudFront invalidation requests
  • CloudFrontClientFactory - Creates and configures the AWS CloudFront client

When a page or file is published, unpublished, or deleted, the extension automatically sends invalidation requests to CloudFront. All operations are logged using PSR-3 LoggerInterface for proper observability.

Requirements

  • Silverstripe Framework ^6
  • Silverstripe CMS ^6
  • AWS SDK for PHP ^3.379 (installed automatically via Composer)
  • AWS CloudFront distribution with valid credentials

Installation

Via Composer (recommended)

composer require "somardigital/silverstripe-cloudfront"

Or for local development add this to composer.json:

{
    "repositories": [
        {
            "type": "path",
            "url": "../silverstripe-cloudfront"
        }
    ]
}

Then run:

composer require "somardigital/silverstripe-cloudfront:@dev"

Manual Installation

  1. Clone this repository into your project
  2. Run composer install in your project root
  3. Run vendor/bin/sake dev/build flush=1

Usage

The extension is automatically applied to SiteTree and File classes via YAML configuration. No additional setup is required.

When an editor:

  • Publishes a page or file → CloudFront cache is invalidated
  • Unpublishes a page or file → CloudFront cache is invalidated
  • Deletes a page or file → CloudFront cache is invalidated

Configuration

AWS Credentials

Add the following to your .env file:

# AWS CloudFront Configuration
AWS_ACCESS_KEY_ID=your_access_key_here
AWS_SECRET_ACCESS_KEY=your_secret_key_here
CLOUDFRONT_DISTRIBUTION_ID=your_distribution_id_here

How It Works

  1. The extension hooks into Silverstripe's ORM events:

    • onAfterPublish
    • onAfterUnpublish
    • onAfterDelete
  2. When triggered, it retrieves the object's public URL:

    • For SiteTree: uses Link()
    • For File: uses Link()
  3. Sends two invalidation paths to CloudFront:

    • The exact path: /about-us
    • Wildcard path with query params: /about-us?*

    This ensures both the base page and all variations with query parameters are invalidated.

  4. If invalidation fails, it logs a warning but doesn't break the publish/delete process

CMS User Cache Bypass (Optional)

This module includes an optional feature to help CMS users bypass CloudFront cache by setting a cookie when they log in. This allows logged-in editors and admins to see the latest content immediately without waiting for cache invalidation.

Note: Setting the cookie alone does not bypass the cache. You must also configure a CloudFront Behavior to bypass caching when this cookie is present. See AWS CloudFront documentation for details on cookie-based cache behaviors.

Enabling the Feature

Add to your .env file:

CLOUDFRONT_CACHE_BYPASS_ENABLED=true

Customizing the Configuration

You can customize the cookie name and required permission in your project's YAML configuration:

Somar\CloudFront\Extensions\MemberSessionCookieExtension:
  cookie_name: 'my-custom-cookie'
  required_permission: 'ADMIN'

Default values:

  • cookie_name: ss-cms-user
  • required_permission: CMS_ACCESS_CMSMain

How It Works

When CLOUDFRONT_CACHE_BYPASS_ENABLED=true:

  1. On Login: If the member has the required permission (default: CMS_ACCESS_CMSMain), the configured cookie is set in their browser
  2. On Logout: The cookie is cleared

This cookie can then be used in CloudFront to create a cache behavior that bypasses the cache for users with this cookie present.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2026-05-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固