缓存管理API

通过API接口管理CDN缓存,实现缓存规则的配置和缓存内容的清除

概述

缓存管理API提供缓存规则的配置、缓存内容的清除、缓存状态的查询等功能,帮助您优化CDN缓存策略。

缓存管理:支持URL级别、目录级别、域名级别的缓存清除操作

API端点

获取缓存规则

GET /domains/{domain_id}/cache-rules

获取指定域名的缓存规则列表

添加缓存规则

POST /domains/{domain_id}/cache-rules

为指定域名添加新的缓存规则

请求参数:

{
"rule_type": "file_extension",
"match_condition": "*.css,*.js",
"cache_time": 604800,
"ignore_params": true
}

更新缓存规则

PUT /domains/{domain_id}/cache-rules/{rule_id}

更新指定的缓存规则

删除缓存规则

DELETE /domains/{domain_id}/cache-rules/{rule_id}

删除指定的缓存规则

清除缓存

POST /domains/{domain_id}/purge

清除指定域名的缓存内容

请求参数:

{
"purge_type": "url",
"purge_targets": [
"https://example.com/style.css",
"https://example.com/script.js"
]
}

缓存规则类型

文件扩展名规则

rule_type: "file_extension"
match_condition: "*.css,*.js,*.jpg"

根据文件扩展名匹配缓存规则

路径匹配规则

rule_type: "path"
match_condition: "/api/*"

根据URL路径匹配缓存规则

正则表达式规则

rule_type: "regex"
match_condition: "^/images/.*\\.(jpg|png)$"

使用正则表达式匹配缓存规则

全站规则

rule_type: "all"
match_condition: "*"

匹配所有内容的缓存规则

清除缓存类型

URL清除

清除指定的URL缓存

purge_type: "url"
purge_targets: ["https://example.com/page.html"]

目录清除

清除指定目录下的所有缓存

purge_type: "directory"
purge_targets: ["https://example.com/images/"]

全站清除

清除整个域名的所有缓存

purge_type: "all"
purge_targets: []

请求示例

添加缓存规则

curl -X POST \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"rule_type": "file_extension",
"match_condition": "*.css,*.js",
"cache_time": 604800,
"ignore_params": true
}' \
https://api.yakcdn.com/v1/domains/domain_123/cache-rules

清除缓存

curl -X POST \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"purge_type": "url",
"purge_targets": [
"https://example.com/style.css"
]
}' \
https://api.yakcdn.com/v1/domains/domain_123/purge

响应格式

缓存规则响应

{
"code": 200,
"data": {
"id": "rule_123",
"rule_type": "file_extension",
"match_condition": "*.css,*.js",
"cache_time": 604800,
"ignore_params": true
}
}

清除缓存响应

{
"code": 200,
"message": "Purge request submitted",
"data": {
"task_id": "purge_123",
"estimated_time": "2-5 minutes"
}
}

常见问题

Q: 缓存清除需要多长时间?

A: 通常需要2-5分钟,具体时间取决于清除的范围和CDN节点数量。

Q: 如何查看缓存清除状态?

A: 可以通过任务ID查询清除状态,或查看域名管理页面的清除历史。

Q: 缓存规则优先级如何确定?

A: 规则按添加顺序执行,越早添加的规则优先级越高。

Q: 可以批量清除缓存吗?

A: 支持批量清除,可以在purge_targets数组中添加多个URL。

下一步

缓存管理API学习完成后,建议您继续学习: