jean-beru / fos-http-cache-cloudfront

使用PHP管理CloudFront HTTP缓存代理的工具

1.2.0 2024-06-04 08:15 UTC

This package is auto-updated.

Last update: 2024-09-04 08:55:16 UTC


README

Latest Version Total Downloads Monthly Downloads Software License Tests

本库为FOSHttpCache提供了CloudFront的实现。

用法

初始化依赖

首先,创建一个AsyncAws\CloudFront\CloudFrontClient实例以允许代理进行请求。有关更多信息,请参阅aws-sdk-php文档

use Aws\CloudFront\CloudFrontClient;

$client = new CloudFrontClient(/* client configuration */);

创建CloudFront代理

要实例化代理,请传递CloudFront客户端和AWS CloudFront分发ID。

use JeanBeru\HttpCacheCloudFront\Proxy\CloudFront;

$proxy = new CloudFront(
    client: $client,
    options: [
      'distribution_id' => 'XYZ1234657',
    ],
);

无效URL

在“XYZ1234657”分发中使/homepage URL及其匹配/assets/*模式的所有URL无效。

$proxy
    ->purge('/homepage')
    ->purge('/assets/*')
    // To send the purge request, flush() method must be called
    ->flush()
; 

避免请求重复

CloudFront API要求一个“调用者引用”以避免重复请求。默认情况下,此库使用UniqIdCallerReferenceGenerator生成唯一的标识符。

您可以使用CallerReference文件夹中存在的其他生成器,或通过实现CallerReferenceGenerator接口来自定义。

例如,如果您想避免在相同分钟内重复调用

use JeanBeru\HttpCacheCloudFront\Proxy\CloudFront;
use JeanBeru\HttpCacheCloudFront\CallerReference\DateCallerReferenceGenerator;

$proxy = new CloudFront(
    client: $client,
    options: [
      'distribution_id' => 'XYZ1234657',
      'caller_reference_generator' => new DateCallerReferenceGenerator('YmdHi'),
    ],
);

如果AWS检测到重复,将抛出异常。

资源