Caching policies define advanced caching schemes that can be applied across the domain name. Policies combine multiple cache settings into a reusable configuration to facilitate unified management of cache behavior.
A cache policy is a named configuration that specifies:
缓存策略 "静态资源"
├── TTL:30天
├── 浏览器TTL:1天
├── 缓存键:URL + Accept-Encoding
├── 压缩:Brotli、Gzip
└── 提供陈旧内容:启用
Sudun provides pre-configured policies for common use cases:
Balanced caching scheme for a typical website:
| Set item | Take the value |
|---|---|
| Edge TTL | Follow the origin header |
| Browser TTL | Follow the origin header |
| cache key | URL + query string |
| Compression | Automatic |
| Provide stale content | Only when there is an error |
Maximizing caching schemes for static content:
| Set item | Take the value |
|---|---|
| Edge TTL | 30 days |
| Browser TTL | 7 days |
| cache key | URL only |
| Compression | Always enabled |
| Provide stale content | Always enabled |
Minimized caching schemes for dynamic content:
| Set item | Take the value |
|---|---|
| Edge TTL | None |
| Browser TTL | None |
| cache key | Not applicable |
| Compression | Real-time compression |
| Provide stale content | Never |
{
"name": "我的静态策略",
"description": "静态资源策略",
"settings": {
"cache": {
"edge_ttl": 2592000,
"browser_ttl": 86400,
"respect_origin": false
},
"cache_key": {
"include_query_string": false,
"include_headers": ["Accept-Encoding"]
},
"optimization": {
"compression": "auto",
"minify": false
},
"stale": {
"serve_while_revalidate": true,
"serve_on_error": true,
"max_stale_age": 86400
}
}
}
Control cache duration:
| Set item | Description | Value range |
|---|---|---|
| edge_ttl | Edge node cache time | 0 - 31536000 (1 year) |
| browser_ttl | Browser cache time | 0 - 31536000 |
| respect_origin | Follow the origin header | true/false |
| min_ttl | Minimum cache time | 0 - 86400 |
| max_ttl | Maximum cache time | 0 - 31536000 |
TTL Priority Rules:
若 respect_origin = true:
TTL = 源站Cache-Control值(在min/max范围内)
若 respect_origin = false:
TTL = edge_ttl设置值
Define the unique identity of the cached content:
{
"cache_key": {
"include_query_string": true,
"query_string_whitelist": ["page", "sort"],
"query_string_blacklist": ["utm_*", "fbclid"],
"include_headers": ["Accept-Encoding", "Accept-Language"],
"include_cookies": [],
"include_device_type": false,
"include_geo": false
}
}
Cache key options:
| options | Effect |
|---|---|
| include_query_string | Differentiate caches by query parameters |
| query_string_whitelist | Only these parameters affect the cache |
| query_string_blacklist | Ignore these parameters |
| include_headers | Differentiate caches by request header |
| include_cookies | Differentiate cache by cookie |
| include_device_type | Differentiate between mobile/desktop caches |
| include_geo | Differentiate caches by country |
Configure content compression:
{
"compression": {
"mode": "auto",
"algorithms": ["br", "gzip", "deflate"],
"min_size": 1024,
"types": ["text/*", "application/json", "application/javascript"]
}
}
Compression mode:
| mode | behavior |
|---|---|
| auto | Automatically compress according to content type and size |
| always | Always compress eligible content |
| never | Disable compression |
| origin | Use origin compression only |
Configure stale content behavior:
{
"stale": {
"serve_while_revalidate": true,
"serve_on_error": true,
"max_stale_age": 86400,
"error_codes": [500, 502, 503, 504]
}
}
Assign a policy to a specific path:
{
"policy_assignments": [
{
"policy": "aggressive-static",
"match": {
"path": "/static/*"
}
},
{
"policy": "dynamic",
"match": {
"path": "/api/*"
}
}
]
}
Assignment Policies by File Extension:
{
"policy": "aggressive-static",
"match": {
"file_extensions": ["css", "js", "jpg", "png", "woff2"]
}
}
Assign policies by response content type:
{
"policy": "standard",
"match": {
"content_type": ["text/html", "application/xhtml+xml"]
}
}
Policies can be inherited from other policies:
{
"name": "my-images-policy",
"inherits": "aggressive-static",
"overrides": {
"cache": {
"browser_ttl": 604800
},
"optimization": {
"image_optimization": true
}
}
}
Inheritance Chain:
基础策略:aggressive-static
└── my-images-policy(继承并覆盖 browser_ttl)
└── my-hero-images(继承并覆盖图像质量)
Track changes in strategy over time:
{
"name": "static-assets",
"version": 3,
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-01-15T10:30:00Z",
"changelog": [
{
"version": 3,
"date": "2024-01-15",
"changes": "将浏览器 TTL 增加到 7 天"
},
{
"version": 2,
"date": "2024-01-10",
"changes": "添加了 Brotli 压缩"
}
]
}
Revert to a previous policy version:
Test policy changes before deployment:
{
"preview": true,
"test_urls": [
"https://example.com/test-page",
"https://example.com/static/test.css"
]
}
Test strategy effectiveness:
{
"ab_test": {
"enabled": true,
"variants": [
{
"name": "control",
"policy": "current-policy",
"weight": 50
},
{
"name": "experiment",
"policy": "new-policy",
"weight": 50
}
],
"metrics": ["cache_hit_ratio", "origin_requests", "ttfb"]
}
}
{
"name": "product-pages",
"settings": {
"cache": {
"edge_ttl": 3600,
"browser_ttl": 300
},
"cache_key": {
"include_query_string": true,
"query_string_whitelist": ["variant", "color", "size"]
},
"stale": {
"serve_while_revalidate": true,
"max_stale_age": 3600
}
}
}
{
"name": "blog-content",
"settings": {
"cache": {
"edge_ttl": 600,
"browser_ttl": 60
},
"stale": {
"serve_while_revalidate": true,
"serve_on_error": true
}
}
}
{
"name": "api-cacheable",
"settings": {
"cache": {
"edge_ttl": 60,
"browser_ttl": 0,
"respect_origin": true
},
"cache_key": {
"include_query_string": true,
"include_headers": ["Authorization"]
}
}
}
{
"name": "immutable-assets",
"settings": {
"cache": {
"edge_ttl": 31536000,
"browser_ttl": 31536000,
"immutable": true
},
"cache_key": {
"include_query_string": false
}
}
}
View policy performance metrics:
| Indicators | Description |
|---|---|
| Cache hit rate | The percentage of requests that serve from the cache |
| Origin uninstall rate | Origin request reduction percentage |
| Save bandwidth | The amount of data provided from the cache |
| Average TTL | The average time content spends in the cache |
curl -X POST https://api.Sudun.com/v1/domains/example.com/policies \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "my-policy",
"settings": {
"cache": {"edge_ttl": 86400},
"compression": {"mode": "auto"}
}
}'
curl -X PUT https://api.Sudun.com/v1/domains/example.com/policies/my-policy \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"settings": {
"cache": {"edge_ttl": 172800}
}
}'
curl -X GET https://api.Sudun.com/v1/domains/example.com/policies \
-H "Authorization: Bearer YOUR_API_KEY"
Need help with caching policies? Please contact support@Sudun.com