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检测到重复,则会抛出 FOS\HttpCache\Exception\ProxyResponseException 异常。

资源