class CachePolicy (construct)
Language | Type name |
---|---|
.NET | Amazon.CDK.AWS.CloudFront.CachePolicy |
Go | github.com/aws/aws-cdk-go/awscdk/v2/awscloudfront#CachePolicy |
Java | software.amazon.awscdk.services.cloudfront.CachePolicy |
Python | aws_cdk.aws_cloudfront.CachePolicy |
TypeScript (source) | aws-cdk-lib » aws_cloudfront » CachePolicy |
Implements
IConstruct
, IDependable
, IResource
, ICache
A Cache Policy configuration.
Example
// Using an existing cache policy for a Distribution
declare const bucketOrigin: origins.S3Origin;
new cloudfront.Distribution(this, 'myDistManagedPolicy', {
defaultBehavior: {
origin: bucketOrigin,
cachePolicy: cloudfront.CachePolicy.CACHING_OPTIMIZED,
},
});
Initializer
new CachePolicy(scope: Construct, id: string, props?: CachePolicyProps)
Parameters
- scope
Construct
- id
string
- props
Cache
Policy Props
Construct Props
Name | Type | Description |
---|---|---|
cache | string | A unique name to identify the cache policy. |
comment? | string | A comment to describe the cache policy. |
cookie | Cache | Determines whether any cookies in viewer requests are included in the cache key and automatically included in requests that CloudFront sends to the origin. |
default | Duration | The default amount of time for objects to stay in the CloudFront cache. |
enable | boolean | Whether to normalize and include the Accept-Encoding header in the cache key when the Accept-Encoding header is 'br'. |
enable | boolean | Whether to normalize and include the Accept-Encoding header in the cache key when the Accept-Encoding header is 'gzip'. |
header | Cache | Determines whether any HTTP headers are included in the cache key and automatically included in requests that CloudFront sends to the origin. |
max | Duration | The maximum amount of time for objects to stay in the CloudFront cache. |
min | Duration | The minimum amount of time for objects to stay in the CloudFront cache. |
query | Cache | Determines whether any query strings are included in the cache key and automatically included in requests that CloudFront sends to the origin. |
cachePolicyName?
Type:
string
(optional, default: generated from the id
)
A unique name to identify the cache policy.
The name must only include '-', '_', or alphanumeric characters.
comment?
Type:
string
(optional, default: no comment)
A comment to describe the cache policy.
The comment cannot be longer than 128 characters.
cookieBehavior?
Type:
Cache
(optional, default: CacheCookieBehavior.none())
Determines whether any cookies in viewer requests are included in the cache key and automatically included in requests that CloudFront sends to the origin.
defaultTtl?
Type:
Duration
(optional, default: The greater of 1 day and minTtl
)
The default amount of time for objects to stay in the CloudFront cache.
Only used when the origin does not send Cache-Control or Expires headers with the object.
enableAcceptEncodingBrotli?
Type:
boolean
(optional, default: false)
Whether to normalize and include the Accept-Encoding
header in the cache key when the Accept-Encoding
header is 'br'.
enableAcceptEncodingGzip?
Type:
boolean
(optional, default: false)
Whether to normalize and include the Accept-Encoding
header in the cache key when the Accept-Encoding
header is 'gzip'.
headerBehavior?
Type:
Cache
(optional, default: CacheHeaderBehavior.none())
Determines whether any HTTP headers are included in the cache key and automatically included in requests that CloudFront sends to the origin.
maxTtl?
Type:
Duration
(optional, default: The greater of 1 year and defaultTtl
)
The maximum amount of time for objects to stay in the CloudFront cache.
CloudFront uses this value only when the origin sends Cache-Control or Expires headers with the object.
minTtl?
Type:
Duration
(optional, default: Duration.seconds(0))
The minimum amount of time for objects to stay in the CloudFront cache.
queryStringBehavior?
Type:
Cache
(optional, default: CacheQueryStringBehavior.none())
Determines whether any query strings are included in the cache key and automatically included in requests that CloudFront sends to the origin.
Properties
Name | Type | Description |
---|---|---|
cache | string | The ID of the cache policy. |
env | Resource | The environment this resource belongs to. |
node | Node | The tree node. |
stack | Stack | The stack in which this resource is defined. |
static AMPLIFY | ICache | This policy is designed for use with an origin that is an AWS Amplify web app. |
static CACHING_DISABLED | ICache | Disables caching. |
static CACHING_OPTIMIZED | ICache | Optimize cache efficiency by minimizing the values that CloudFront includes in the cache key. |
static CACHING_OPTIMIZED_FOR_UNCOMPRESSED_OBJECTS | ICache | Optimize cache efficiency by minimizing the values that CloudFront includes in the cache key. |
static ELEMENTAL_MEDIA_PACKAGE | ICache | Designed for use with an origin that is an AWS Elemental MediaPackage endpoint. |
static USE_ORIGIN_CACHE_CONTROL_HEADERS | ICache | Designed for use with an origin that returns Cache-Control HTTP response headers and does not serve different content based on values present in the query string. |
static USE_ORIGIN_CACHE_CONTROL_HEADERS_QUERY_STRINGS | ICache | Designed for use with an origin that returns Cache-Control HTTP response headers and serves different content based on values present in the query string. |
cachePolicyId
Type:
string
The ID of the cache policy.
env
Type:
Resource
The environment this resource belongs to.
For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.
node
Type:
Node
The tree node.
stack
Type:
Stack
The stack in which this resource is defined.
static AMPLIFY
Type:
ICache
This policy is designed for use with an origin that is an AWS Amplify web app.
static CACHING_DISABLED
Type:
ICache
Disables caching.
This policy is useful for dynamic content and for requests that are not cacheable.
static CACHING_OPTIMIZED
Type:
ICache
Optimize cache efficiency by minimizing the values that CloudFront includes in the cache key.
Query strings and cookies are not included in the cache key, and only the normalized 'Accept-Encoding' header is included.
static CACHING_OPTIMIZED_FOR_UNCOMPRESSED_OBJECTS
Type:
ICache
Optimize cache efficiency by minimizing the values that CloudFront includes in the cache key.
Query strings and cookies are not included in the cache key, and only the normalized 'Accept-Encoding' header is included. Disables cache compression.
static ELEMENTAL_MEDIA_PACKAGE
Type:
ICache
Designed for use with an origin that is an AWS Elemental MediaPackage endpoint.
static USE_ORIGIN_CACHE_CONTROL_HEADERS
Type:
ICache
Designed for use with an origin that returns Cache-Control HTTP response headers and does not serve different content based on values present in the query string.
static USE_ORIGIN_CACHE_CONTROL_HEADERS_QUERY_STRINGS
Type:
ICache
Designed for use with an origin that returns Cache-Control HTTP response headers and serves different content based on values present in the query string.
Methods
Name | Description |
---|---|
apply | Apply the given removal policy to this resource. |
to | Returns a string representation of this construct. |
static from | Imports a Cache Policy from its id. |
RemovalPolicy(policy)
applypublic applyRemovalPolicy(policy: RemovalPolicy): void
Parameters
- policy
Removal
Policy
Apply the given removal policy to this resource.
The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you've removed it from the CDK application or because you've made a change that requires the resource to be replaced.
The resource can be deleted (RemovalPolicy.DESTROY
), or left in your AWS
account for data recovery and cleanup later (RemovalPolicy.RETAIN
).
String()
topublic toString(): string
Returns
string
Returns a string representation of this construct.
CachePolicyId(scope, id, cachePolicyId)
static frompublic static fromCachePolicyId(scope: Construct, id: string, cachePolicyId: string): ICachePolicy
Parameters
- scope
Construct
- id
string
- cachePolicyId
string
Returns
Imports a Cache Policy from its id.