説明
繰り返されるリクエストに同じ応答を安全に返せる場合は、SAM REST API の GET 応答をキャッシュします。
想定される影響
キャッシュは遅延とバックエンドの負荷を減らせます。キャッシュを共有すると、古いデータや別のユーザーの応答を返すおそれもあります。
対処方法
CacheClusterEnabled: true を指定し、MethodSettings で対象メソッドの CachingEnabled を有効にします。異なる応答を区別できるキャッシュキーと、必要な鮮度に合う有効期間を選びます。
例
例の ResourcePath: /* はすべての GET ルートに適用されます。キャッシュを共有できるルートに範囲を絞り、API 定義とキャッシュキーを設定します。
変更前
yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
TracingEnabled: true
変更後
yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
TracingEnabled: true
CacheClusterEnabled: true
MethodSettings:
- ResourcePath: /*
HttpMethod: GET
CachingEnabled: true