×
全部 > AI技术 > AI编程 > AI代码生成 > 生成Cloudflare的URL缓存刷新脚本

生成Cloudflare的URL缓存刷新脚本

claude3.5 sonnet 20241022(Anthropic)

### Cache Refresh Overview This script is designed to purge specific URLs from the Cloudflare CDN cache. It uses the Cloudflare API to authenticate and perform the cache refresh operation. ### Purge Script Source Code (Bash) ```bash #!/bin/bash # Cloudflare API credentials API_TOKEN="your-cloudflare-api-token" ZONE_ID="your-cloudflare-zone-id" # URLs to purge TARGET_LIST=( "https://example.com/index.html" "https://example.com/style.css" ) # Purge URLs PURGE_URL="https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" # Create JSON payload JSON_PAYLOAD=$(jq -n --argjson files "$(printf '%s\n' "${TARGET_LIST[@]}" | jq -R . | jq -s .)" '{files: $files}') # Send purge request curl -X POST "$PURGE_URL" \ -H "Authorization: Bearer $API_TOKEN" \ -H "Content-Type: application/json" \ -d "$JSON_PAYLOAD" echo "Purge request sent for URLs: ${TARGET_LIST[@]}" ``` ### Usage Instructions 1. **Install `jq`**: Ensure you have `jq` installed for JSON manipulation. You can install it using your package manager (e.g., `apt-get install jq` for Debian-based systems). 2. **Set API Token and Zone ID**: Replace `your-cloudflare-api-token` and `your-cloudflare-zone-id` in the script with your actual Cloudflare API token and zone ID. 3. **Add URLs**: Modify the `TARGET_LIST` array to include the URLs you want to purge from the cache. 4. **Run the Script**: Execute the script in your terminal using `bash script_name.sh`. ### Security Notes - Keep your API token secure and do not expose it in public repositories. - Limit the permissions of your API token to only what is necessary for cache purging. - Consider using environment variables to store sensitive information instead of hardcoding them in the script.

DeepSeek V3(Deepseek)

# Cloudflare CDN 缓存刷新脚本 (URL刷新) ## Cache Refresh Overview - **CDN平台**: Cloudflare - **刷新类型**: 单URL刷新 (支持批量URL) - **刷新方式**: 通过Cloudflare API v4发送POST请求清除指定URL缓存 - **刷新速度**: 通常3秒内生效(全球节点) ## Purge Script Source Code (bash) ```bash #!/bin/bash # Cloudflare API配置 API_TOKEN="your-cloudflare-api-token" # 替换为您的API令牌 ZONE_ID="your-cloudflare-zone-id" # 替换为您的区域ID API_URL="https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/purge_cache" # 要刷新的URL数组 (JSON格式) URLS_TO_PURGE='[ "https://example.com/index.html", "https://example.com/style.css" ]' # 发送清除请求 response=$(curl -s -X POST "${API_URL}" \ -H "Authorization: Bearer ${API_TOKEN}" \ -H "Content-Type: application/json" \ --data "{\"files\":${URLS_TO_PURGE}}") # 检查结果 if [[ $(echo "$response" | jq -r '.success') == "true" ]]; then echo "✅ 缓存刷新成功!" echo "刷新ID: $(echo "$response" | jq -r '.result.id')" else echo "❌ 刷新失败:" echo "$response" | jq '.errors[] | .message' exit 1 fi ``` ## Usage Instructions 1. **依赖安装**: - 需要安装 `jq` 工具处理JSON (`apt-get install jq` 或 `brew install jq`) - 需要 `curl` 命令 2. **脚本准备**: ```bash chmod +x cloudflare_purge.sh ``` 3. **执行刷新**: ```bash ./cloudflare_purge.sh ``` 4. **批量刷新**: - 修改 `URLS_TO_PURGE` 数组内容 - 单次请求最多支持30个URL 5. **定时任务**: ```bash # 每天凌晨刷新 0 0 * * * /path/to/cloudflare_purge.sh ``` ## Security Notes 1. **API令牌安全**: - 建议使用仅包含 `Zone.Cache Purge` 权限的API令牌 - 不要将令牌提交到版本控制系统 2. **速率限制**: - 免费版: 每分钟1,000次请求 - 专业版: 每分钟3,000次请求 3. **敏感信息处理**: ```bash # 更安全的方式是通过环境变量传递: export CF_API_TOKEN="your-token" API_TOKEN="${CF_API_TOKEN}" ``` 4. **日志记录**: 建议添加日志记录功能跟踪刷新操作: ```bash echo "$(date) - 刷新URL: ${URLS_TO_PURGE}" >> /var/log/cdn_purge.log ```

gpt-4o(OpenAI)

```markdown # Cache Refresh Overview 该脚本用于在Cloudflare平台上进行URL缓存刷新。目标是刷新指定的URL,从而确保用户能快速访问最新的页面内容。 # Purge Script Source Code ```bash #!/bin/bash # Cloudflare API URL for purge cache API_URL="https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/purge_cache" # Authentication credentials API_TOKEN="your-cloudflare-api-token" ZONE_ID="your-cloudflare-zone-id" # List of URLs to purge URLS=("https://example.com/index.html" "https://example.com/style.css") # Function to purge cache for given URLs purge_cache() { local urls="$1" # Create JSON payload payload=$(jq -n --argjson urls "$urls" '{files: $urls}') # Make a POST request to purge cache response=$(curl -s -X POST "$API_URL" \ -H "Authorization: Bearer $API_TOKEN" \ -H "Content-Type: application/json" \ --data "$payload") # Check response for errors if echo "$response" | grep -q '"success":true'; then echo "Cache purged successfully." else echo "Error purging cache." echo "Response: $response" fi } # Prepare URL list as JSON array urls_json=$(jq -nc --arg urls "${URLS[*]}" '[$urls | split(" ")]') # Purge cache purge_cache "$urls_json" ``` # Usage Instructions 1. 确保安装了`jq`工具,如果没有安装可通过`apt`或`brew`进行安装: ```bash # Debian/Ubuntu sudo apt-get install jq # macOS brew install jq ``` 2. 将脚本中的`API_TOKEN`和`ZONE_ID`替换为您的实际Cloudflare API令牌和Zone ID。 3. 将`URLS`数组修改为您需要刷新的URL列表。 4. 为脚本赋予执行权限并运行脚本: ```bash chmod +x purge_cloudflare_cache.sh ./purge_cloudflare_cache.sh ``` # Security Notes - 请妥善保管您的`API_TOKEN`,避免将其暴露在公共环境中。 - 如果重新配置目标URL或认证信息,请确保对脚本进行安全性与配置的双重校验,以免意外刷新错误的内容。 - 考虑在生产环境中使用加密存储或环境变量管理敏感信息。 ```

通义千问2.5-Coder-32B(阿里云百炼)

# Cache Refresh Overview 本次请求为针对Cloudflare CDN平台的缓存刷新操作,具体刷新类型为单URL刷新。目标刷新对象为两个URL:`https://example.com/index.html` 和 `https://example.com/style.css`。认证方式采用Cloudflare的API Token和Zone ID。 # Purge Script Source Code 以下是使用Bash编写的缓存刷新脚本: ```bash #!/bin/bash # Configuration API_TOKEN="your-cloudflare-api-token" ZONE_ID="your-cloudflare-zone-id" URLS=("https://example.com/index.html" "https://example.com/style.css") # Function to purge URLs purge_urls() { local urls=("$@") for url in "${urls[@]}"; do echo "Purging URL: $url" curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \ -H "Authorization: Bearer $API_TOKEN" \ -H "Content-Type: application/json" \ --data "{\"files\":[\"$url\"]}" done } # Main script execution purge_urls "${URLS[@]}" ``` # Usage Instructions 1. 将上述脚本保存为一个文件,例如 `purge_cache.sh`。 2. 替换脚本中的 `API_TOKEN` 和 `ZONE_ID` 为你自己的Cloudflare API Token和Zone ID。 3. 确保脚本具有可执行权限,可以通过以下命令赋予: ```bash chmod +x purge_cache.sh ``` 4. 运行脚本: ```bash ./purge_cache.sh ``` # Security Notes - **API Token安全性**:确保API Token的安全性,不要将其硬编码在脚本中,可以考虑使用环境变量或者配置文件存储敏感信息。 - **权限控制**:API Token应仅具备必要的权限,避免过度权限导致的安全风险。 - **日志记录**:建议在生产环境中添加日志记录功能,以便于排查问题和审计操作。

模型名称 模型提供商 响应总时长(s) 生成 token 数 首 token 延迟(s) 生成速率(tokens/s)
10.12
0
2.12
0
39.01
694
内容最多
7.07
17.79
7.15
响应最快
562
1.22
78.6
速度最快
14.81
438
0.52
延迟最小
29.57
AI文本生成
38家服务商提供服务
AI深度推理
11家服务商提供服务
AI代码生成
11家服务商提供服务
AI数学模型
10家服务商提供服务