缓存策略定义了可在整个域名范围内应用的高级缓存方案。策略将多个缓存设置组合成可复用的配置,便于统一管理缓存行为。
缓存策略是一种命名配置,用于指定:
缓存策略 "静态资源"
├── TTL:30天
├── 浏览器TTL:1天
├── 缓存键:URL + Accept-Encoding
├── 压缩:Brotli、Gzip
└── 提供陈旧内容:启用
Sudun 为常见用例提供预配置策略:
适用于典型网站的均衡缓存方案:
| 设置项 | 取值 |
|---|---|
| 边缘TTL | 遵循源站头部 |
| 浏览器TTL | 遵循源站头部 |
| 缓存键 | URL + 查询字符串 |
| 压缩 | 自动 |
| 提供陈旧内容 | 仅出错时 |
适用于静态内容的最大化缓存方案:
| 设置项 | 取值 |
|---|---|
| 边缘TTL | 30天 |
| 浏览器TTL | 7天 |
| 缓存键 | 仅URL |
| 压缩 | 始终启用 |
| 提供陈旧内容 | 始终启用 |
适用于动态内容的最小化缓存方案:
| 设置项 | 取值 |
|---|---|
| 边缘TTL | 无 |
| 浏览器TTL | 无 |
| 缓存键 | 不适用 |
| 压缩 | 实时压缩 |
| 提供陈旧内容 | 从不 |
{
"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
}
}
}
控制缓存时长:
| 设置项 | 说明 | 取值范围 |
|---|---|---|
| edge_ttl | 边缘节点缓存时间 | 0 - 31536000(1年) |
| browser_ttl | 浏览器缓存时间 | 0 - 31536000 |
| respect_origin | 遵循源站头部 | true/false |
| min_ttl | 最短缓存时间 | 0 - 86400 |
| max_ttl | 最长缓存时间 | 0 - 31536000 |
TTL优先级规则:
若 respect_origin = true:
TTL = 源站Cache-Control值(在min/max范围内)
若 respect_origin = false:
TTL = edge_ttl设置值
定义缓存内容的唯一性标识:
{
"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
}
}
缓存键选项:
| 选项 | 效果 |
|---|---|
| include_query_string | 按查询参数区分缓存 |
| query_string_whitelist | 仅这些参数影响缓存 |
| query_string_blacklist | 忽略这些参数 |
| include_headers | 按请求头部区分缓存 |
| include_cookies | 按Cookie区分缓存 |
| include_device_type | 区分移动端/桌面端缓存 |
| include_geo | 按国家区分缓存 |
配置内容压缩:
{
"compression": {
"mode": "auto",
"algorithms": ["br", "gzip", "deflate"],
"min_size": 1024,
"types": ["text/*", "application/json", "application/javascript"]
}
}
压缩模式:
| 模式 | 行为 |
|---|---|
| auto | 根据内容类型和大小自动压缩 |
| always | 始终压缩符合条件的内容 |
| never | 禁用压缩 |
| origin | 仅使用源站压缩 |
配置陈旧内容行为:
{
"stale": {
"serve_while_revalidate": true,
"serve_on_error": true,
"max_stale_age": 86400,
"error_codes": [500, 502, 503, 504]
}
}
为特定路径分配策略:
{
"policy_assignments": [
{
"policy": "aggressive-static",
"match": {
"path": "/static/*"
}
},
{
"policy": "dynamic",
"match": {
"path": "/api/*"
}
}
]
}
按文件扩展名分配策略:
{
"policy": "aggressive-static",
"match": {
"file_extensions": ["css", "js", "jpg", "png", "woff2"]
}
}
按响应内容类型分配策略:
{
"policy": "standard",
"match": {
"content_type": ["text/html", "application/xhtml+xml"]
}
}
策略可以从其他策略继承:
{
"name": "my-images-policy",
"inherits": "aggressive-static",
"overrides": {
"cache": {
"browser_ttl": 604800
},
"optimization": {
"image_optimization": true
}
}
}
继承链:
基础策略:aggressive-static
└── my-images-policy(继承并覆盖 browser_ttl)
└── my-hero-images(继承并覆盖图像质量)
跟踪策略随时间的变化:
{
"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 压缩"
}
]
}
恢复到之前的策略版本:
在部署前测试策略更改:
{
"preview": true,
"test_urls": [
"https://example.com/test-page",
"https://example.com/static/test.css"
]
}
测试策略效果:
{
"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
}
}
}
查看策略性能指标:
| 指标 | 说明 |
|---|---|
| 缓存命中率 | 从缓存提供服务的请求百分比 |
| 源站卸载率 | 源站请求减少百分比 |
| 节省带宽 | 从缓存提供的数据量 |
| 平均TTL | 内容在缓存中的平均停留时间 |
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"
需要缓存策略帮助?请联系 support@Sudun.com