缓存规则定义了Sudun如何从边缘服务器缓存和提供内容。正确配置的缓存规则可以提升性能、减轻源站负载并改善用户体验。
请求 → 边缘服务器 → 缓存检查
│
┌───────┴───────┐
│ │
缓存命中 缓存未命中
│ │
返回缓存内容 从源站获取
缓存响应
返回给用户
默认情况下,Sudun根据以下内容类型进行缓存:
| 内容类型 | 默认TTL | 是否可缓存 |
|---|---|---|
| 图片 (jpg, png, gif, webp) | 1天 | 是 |
| CSS、JavaScript | 1天 | 是 |
| 字体 (woff, woff2, ttf) | 1天 | 是 |
| HTML | 不缓存 | 否 |
| API响应 | 不缓存 | 否 |
| 要素 | 描述 | 示例 |
|---|---|---|
| 匹配条件 | 匹配请求的条件 | URL路径、文件扩展名 |
| 缓存操作 | 如何处理匹配的请求 | 缓存、绕过、覆盖 |
| TTL | 缓存存活时间 | 3600 (1小时) |
# 精确匹配
/images/logo.png
# 前缀匹配
/static/*
# 通配符匹配
/api/*/public
# 正则匹配 (高级)
^/blog/[0-9]{4}/.*$
按文件类型缓存:
{
"match": {
"file_extension": ["jpg", "jpeg", "png", "gif", "webp", "css", "js"]
},
"action": "cache",
"ttl": 86400
}
{
"match": {
"path": "/search",
"query_string": {
"contains": "category"
}
}
}
{
"match": {
"headers": {
"Accept": "application/json"
}
},
"action": "bypass"
}
在边缘服务器存储内容:
{
"match": { "path": "/static/*" },
"action": "cache",
"ttl": 604800,
"settings": {
"browser_ttl": 86400,
"serve_stale": true
}
}
始终从源站获取:
{
"match": { "path": "/api/*" },
"action": "bypass"
}
忽略源站缓存头部:
{
"match": { "path": "/assets/*" },
"action": "override",
"ttl": 2592000,
"ignore_origin_headers": true
}
内容在边缘服务器缓存的时间:
| 值 | 时长 | 使用场景 |
|---|---|---|
| 60 | 1分钟 | 频繁更新的内容 |
| 3600 | 1小时 | 半动态内容 |
| 86400 | 1天 | 静态资源 |
| 604800 | 1周 | 极少变动的内容 |
| 2592000 | 30天 | 版本化资源 |
浏览器本地缓存内容的时间:
{
"edge_ttl": 86400,
"browser_ttl": 3600
}
提示:将浏览器TTL设置得比边缘TTL短,以便保持对缓存失效的控制。
缓存键决定了什么因素使缓存对象具有唯一性。
https://example.com/page?sort=date&page=1
│ │ │
协议方案 路径 查询字符串
| 选项 | 行为 |
|---|---|
| 包含全部 | 缓存随所有查询参数变化 |
| 排除全部 | 忽略查询字符串进行缓存 |
| 包含特定 | 仅指定参数影响缓存 |
| 排除特定 | 忽略指定参数 |
示例:包含特定参数
{
"cache_key": {
"query_string": {
"include": ["category", "page"]
}
}
}
根据请求头变化缓存:
{
"cache_key": {
"headers": ["Accept-Language", "Accept-Encoding"]
}
}
根据Cookie变化缓存 (谨慎使用):
{
"cache_key": {
"cookies": ["session_type", "user_tier"]
}
}
在获取新内容的同时提供缓存内容:
{
"serve_stale": {
"while_revalidate": true,
"on_error": true,
"max_stale_age": 86400
}
}
当源站失败时提供陈旧内容:
源站错误 (5xx) → 提供陈旧内容 → 记录错误
默认情况下,Sudun 遵循以下源站头部:
| 头部 | 行为 |
|---|---|
| Cache-Control: max-age=X | 缓存 X 秒 |
| Cache-Control: no-cache | 提供前重新验证 |
| Cache-Control: no-store | 不缓存 |
| Cache-Control: private | 不在边缘缓存 |
| Expires | 缓存至指定日期 |
| ETag | 启用条件请求 |
强制缓存,忽略源站头部:
{
"match": { "path": "/static/*" },
"action": "cache",
"ttl": 86400,
"respect_origin_headers": false
}
规则按顺序评估。第一个匹配的规则生效:
规则 1: /api/* → 绕过 (优先级: 1)
规则 2: /api/public/* → 缓存 (优先级: 2)
规则 3: /* → 缓存 (优先级: 3)
请求: /api/users → 匹配规则 1 → 绕过
请求: /api/public/data → 匹配规则 1 → 绕过 (而非规则 2!)
重要:将更具体的规则放在通用规则之前。
{
"name": "静态资源",
"match": {
"file_extension": ["css", "js", "jpg", "png", "gif", "ico", "woff2"]
},
"action": "cache",
"ttl": 2592000,
"browser_ttl": 86400
}
{
"name": "API 绕过",
"match": { "path": "/api/*" },
"action": "bypass"
}
{
"name": "HTML 页面",
"match": {
"file_extension": ["html"],
"path": "/*"
},
"action": "cache",
"ttl": 300,
"serve_stale": true
}
{
"name": "版本化资源",
"match": {
"path": "/assets/v*/*"
},
"action": "cache",
"ttl": 31536000,
"immutable": true
}
检查 X-Cache-Status 响应头部:
| 状态 | 含义 |
|---|---|
| HIT | 从缓存提供 |
| MISS | 从源站获取 |
| BYPASS | 缓存被绕过 |
| EXPIRED | 缓存过期,正在重新验证 |
| STALE | 提供陈旧内容 |
# 检查缓存状态
curl -I https://example.com/static/style.css
# 响应头部
X-Cache-Status: HIT
Cache-Control: max-age=86400
Age: 3600
通过 API 管理缓存规则:
# 列出缓存规则
curl -X GET https://api.Sudun.com/v1/domains/example.com/cache-rules \
-H "Authorization: Bearer YOUR_API_KEY"
# 创建缓存规则
curl -X POST https://api.yeSudunom/v1/domains/example.com/cache-rules \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "Static Assets",
"match": {"file_extension": ["css", "js", "png"]},
"action": "cache",
"ttl": 86400
}'
需要缓存规则方面的帮助?请联系 support@Sudun.com