内容预取功能会在用户请求之前,主动将内容加载到边缘缓存中。这确保了首次请求即可命中缓存,消除了冷启动延迟,从而提升用户体验。
传统流程:
用户请求 → 缓存未命中 → 回源获取 → 缓存存储 → 响应
预取流程:
预取任务 → 回源获取 → 缓存存储(就绪)
用户请求 → 缓存命中 → 即时响应
| 优势 | 描述 |
|---|---|
| 消除冷启动 | 首次访问者也能获取缓存内容 |
| 更快的页面加载 | 无需回源往返延迟 |
| 可预测的性能 | 一致的响应时间 |
| 源站保护 | 将负载分散到不同时间 |
预取指定的 URL:
{
"urls": [
"https://example.com/",
"https://example.com/products",
"https://example.com/about",
"https://example.com/css/styles.css",
"https://example.com/js/app.js"
]
}
最适合:已知的关键页面、落地页、营销活动页面
自动从您的站点地图预取 URL:
站点地图 URL:https://example.com/sitemap.xml
Sudun 会解析您的站点地图并预取所有列出的 URL:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com/page1</loc>
<lastmod>2024-01-15</lastmod>
</url>
<url>
<loc>https://example.com/page2</loc>
</url>
</urlset>
最适合:全站预热、对 SEO 至关重要的页面
自动发现并预取链接内容:
{
"start_url": "https://example.com/",
"depth": 2,
"max_urls": 100
}
爬虫会跟随链接来发现内容:
深度 0:首页
深度 1:首页链接的页面
深度 2:深度 1 页面链接的页面
最适合:动态网站、发现新内容
- 来源:URL 列表、站点地图或爬虫
- 计划:一次性或周期性
- 选项:请求头、区域、并发数
| 选项 | 描述 | 默认值 |
|---|---|---|
| 区域 | 预取内容将存入的接入点 | 所有区域 |
| 并发数 | 向源站发起的并行请求数 | 5 |
| 请求头 | 预取请求的自定义请求头 | 无 |
| 忽略 Robots | 跳过 robots.txt 限制 | 否 |
预取到指定的地理区域:
{
"urls": ["https://example.com/"],
"regions": ["us-east", "eu-west", "asia-pacific"]
}
可用区域:
| 区域 ID | 位置 |
|---|---|
| us-east | 北美东部 |
| us-west | 北美西部 |
| eu-west | 欧洲西部 |
| eu-central | 欧洲中部 |
| asia-pacific | 亚太地区 |
| asia-south | 南亚地区 |
立即或在预定时间运行预取:
{
"schedule": {
"type": "once",
"run_at": "2024-01-15T06:00:00Z"
}
}
设置自动周期性预取:
{
"schedule": {
"type": "recurring",
"interval": "daily",
"time": "04:00",
"timezone": "UTC"
}
}
计划选项:
| 间隔 | 描述 | 使用场景 |
|---|---|---|
| hourly | 每小时一次 | 频繁更新的内容 |
| daily | 每天一次 | 标准网站 |
| weekly | 每周一次 | 静态内容 |
| custom | Cron 表达式 | 复杂计划 |
用于高级调度:
{
"schedule": {
"type": "cron",
"expression": "0 */6 * * *"
}
}
常见模式:
0 4 * * * - 每天凌晨 4 点0 */2 * * * - 每 2 小时0 0 * * 0 - 每周日在预取时发送特定标头:
{
"headers": {
"Accept-Language": "en-US",
"Accept-Encoding": "gzip, br",
"X-Prefetch": "true"
}
}
配置预取用户代理:
{
"user_agent": "Sudun-Prefetch/1.0"
}
提示:您的源站可通过用户代理或自定义标头检测预取请求并单独记录。
部署后自动预取:
# CI/CD 集成
- name: 部署
run: deploy-script.sh
- name: 预热缓存
run: |
curl -X POST https://api.Sudun.com/v1/domains/example.com/prefetch \
-H "Authorization: Bearer $API_KEY" \
-d '{"sitemap": "https://example.com/sitemap.xml"}'
通过 Webhook 触发预取:
curl -X POST https://api.Sudun.com/v1/domains/example.com/prefetch/trigger \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"job_id": "prefetch-job-123"}'
内容变更时预取:
{
"trigger": {
"type": "purge",
"action": "prefetch_after_purge"
}
}
跟踪预取任务进度:
| 状态 | 描述 |
|---|---|
| 等待中 | 任务已排队 |
| 运行中 | 预取进行中 |
| 已完成 | 所有 URL 已预取 |
| 失败 | 任务失败(查看错误) |
| 部分完成 | 部分 URL 失败 |
在仪表板中查看指标:
{
"job_id": "pf-abc123",
"status": "completed",
"started_at": "2024-01-15T04:00:00Z",
"completed_at": "2024-01-15T04:05:32Z",
"stats": {
"total_urls": 150,
"successful": 148,
"failed": 2,
"skipped": 0
}
}
首先预取最重要的页面:
{
"urls": [
{"url": "https://example.com/", "priority": "high"},
{"url": "https://example.com/products", "priority": "high"},
{"url": "https://example.com/blog", "priority": "medium"},
{"url": "https://example.com/about", "priority": "low"}
]
}
配置并发以避免源站过载:
{
"concurrency": 3,
"rate_limit": "10/秒"
}
在流量最低时运行预取:
高峰时段:上午 9 点 - 下午 6 点
预取窗口:凌晨 2 点 - 凌晨 5 点
跳过不应预取的 URL:
{
"exclude_patterns": [
"/api/*",
"/admin/*",
"*.json",
"*?session=*"
]
}
如果预取使源站过载:
如果预取后内容未缓存:
curl -X POST https://api.Sudun.com/v1/domains/example.com/prefetch \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"urls": [
"https://example.com/",
"https://example.com/products"
],
"regions": ["us-east", "eu-west"],
"concurrency": 5
}'
curl -X POST https://api.Sudun.com/v1/domains/example.com/prefetch \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"sitemap": "https://example.com/sitemap.xml",
"schedule": {
"type": "daily",
"time": "04:00"
}
}'
curl -X GET https://api.Sudun.com/v1/domains/example.com/prefetch/job-123 \
-H "Authorization: Bearer YOUR_API_KEY"
需要内容预取方面的帮助?请联系 support@Sudun.com