本文目录导读:

我来介绍几种批量清除API缓存的方法和脚本示例:
使用curl脚本批量清除
Linux/Mac Bash脚本
#!/bin/bash
# API配置
API_BASE_URL="https://api.example.com"
API_KEY="your-api-key"
ENDPOINTS=("users" "products" "orders" "categories")
# 清除单个端点缓存
clear_cache() {
local endpoint=$1
response=$(curl -s -o /dev/null -w "%{http_code}" \
-X DELETE \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
"${API_BASE_URL}/cache/${endpoint}")
if [ "$response" -eq 200 ]; then
echo "✓ 清除 $endpoint 缓存成功"
else
echo "✗ 清除 $endpoint 缓存失败 (HTTP $response)"
fi
}
# 批量清除
for endpoint in "${ENDPOINTS[@]}"; do
clear_cache "$endpoint"
done
Python脚本批量清除
#!/usr/bin/env python3
import requests
import json
from concurrent.futures import ThreadPoolExecutor, as_completed
class CacheClearer:
def __init__(self, base_url, api_key):
self.base_url = base_url
self.headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
def clear_endpoint_cache(self, endpoint):
"""清除单个端点缓存"""
url = f"{self.base_url}/cache/{endpoint}"
try:
response = requests.delete(url, headers=self.headers)
if response.status_code == 200:
return f"✓ {endpoint}: 缓存清除成功"
else:
return f"✗ {endpoint}: 失败 (HTTP {response.status_code})"
except Exception as e:
return f"✗ {endpoint}: 错误 - {str(e)}"
def batch_clear(self, endpoints=None, parallel=True):
"""批量清除缓存"""
if endpoints is None:
# 从配置文件或API获取端点列表
endpoints = self.get_endpoints()
if parallel:
# 并行清除
with ThreadPoolExecutor(max_workers=5) as executor:
futures = {executor.submit(self.clear_endpoint_cache, ep): ep
for ep in endpoints}
for future in as_completed(futures):
print(future.result())
else:
# 顺序清除
for endpoint in endpoints:
result = self.clear_endpoint_cache(endpoint)
print(result)
def get_endpoints(self):
"""获取需要清除缓存的端点列表"""
try:
response = requests.get(
f"{self.base_url}/cache/endpoints",
headers=self.headers
)
if response.status_code == 200:
return response.json()['endpoints']
except:
pass
return ['users', 'products', 'orders', 'categories']
# 使用示例
if __name__ == "__main__":
clearer = CacheClearer(
base_url="https://api.example.com",
api_key="your-api-key"
)
# 指定端点清除
endpoints = ['users', 'products', 'orders']
clearer.batch_clear(endpoints, parallel=True)
# 或者自动获取所有需要清除的端点
clearer.batch_clear()
使用Redis清空模式(如果缓存使用Redis)
#!/bin/bash
# Redis配置
REDIS_HOST="localhost"
REDIS_PORT=6379
REDIS_PASSWORD="your-password"
# 清空特定模式的缓存键
clear_redis_cache() {
local pattern=$1
# 获取匹配的键
keys=$(redis-cli -h $REDIS_HOST -p $REDIS_PORT -a $REDIS_PASSWORD \
KEYS "api:cache:${pattern}*")
if [ -n "$keys" ]; then
echo "$keys" | while read -r key; do
redis-cli -h $REDIS_HOST -p $REDIS_PORT -a $REDIS_PASSWORD \
DEL "$key"
echo "删除: $key"
done
else
echo "没有匹配的键: ${pattern}"
fi
}
# 批量清除不同类型的缓存
patterns=("users:" "products:" "orders:")
for pattern in "${patterns[@]}"; do
echo "清除缓存: api:cache:${pattern}*"
clear_redis_cache "$pattern"
done
使用配置文件批量清除
# cache-config.yaml
api:
base_url: "https://api.example.com"
key: "your-api-key"
endpoints:
- name: "users"
methods: ["GET", "POST"]
- name: "products"
methods: ["GET"]
- name: "orders"
methods: ["GET", "PUT"]
- name: "inventory"
methods: ["GET", "POST", "DELETE"]
cache_strategy:
batch_size: 5
timeout: 30
retry_count: 3
parallel: true
通用清除函数库
// Node.js脚本
const axios = require('axios');
const fs = require('fs');
class ApiCacheManager {
constructor(config) {
this.apiKey = config.apiKey;
this.baseUrl = config.baseUrl;
this.client = axios.create({
baseURL: config.baseUrl,
headers: {
'Authorization': `Bearer ${config.apiKey}`,
'Content-Type': 'application/json'
}
});
}
async clearEndpoint(endpoint) {
try {
const response = await this.client.delete(`/cache/${endpoint}`);
return { endpoint, status: 'success', code: response.status };
} catch (error) {
return { endpoint, status: 'failed', error: error.message };
}
}
async batchClear(endpoints, options = {}) {
const { parallel = true, batchSize = 5, delay = 1000 } = options;
const results = [];
if (parallel) {
// 并行处理
const batches = [];
for (let i = 0; i < endpoints.length; i += batchSize) {
batches.push(endpoints.slice(i, i + batchSize));
}
for (const batch of batches) {
const batchResults = await Promise.all(
batch.map(ep => this.clearEndpoint(ep))
);
results.push(...batchResults);
await new Promise(resolve => setTimeout(resolve, delay));
}
} else {
// 顺序处理
for (const endpoint of endpoints) {
const result = await this.clearEndpoint(endpoint);
results.push(result);
if (result.status === 'failed') {
console.warn(`清除 ${endpoint} 失败: ${result.error}`);
}
// 添加延迟避免请求过快
await new Promise(resolve => setTimeout(resolve, 500));
}
}
return results;
}
async clearByPattern(pattern) {
// 通过模式匹配清除
const endpoints = await this.getEndpointsByPattern(pattern);
return this.batchClear(endpoints);
}
}
// 使用示例
async function main() {
const config = {
apiKey: 'your-api-key',
baseUrl: 'https://api.example.com'
};
const manager = new ApiCacheManager(config);
// 清除指定端点
const endpoints = ['users', 'products', 'orders'];
const results = await manager.batchClear(endpoints, {
parallel: true,
batchSize: 3,
delay: 2000
});
console.log('清除结果:', results);
}
main().catch(console.error);
使用Docker容器批量清除
# docker-compose.yml
version: '3.8'
services:
cache-clearer:
image: alpine/curl
entrypoint: |
/bin/sh -c "
apk add --no-cache jq;
while IFS= read -r endpoint; do
echo 'Clearing cache for: ' $endpoint;
curl -X DELETE -H 'Authorization: Bearer \${API_KEY}' \
https://api.example.com/cache/\$endpoint;
sleep 1;
done < /endpoints.txt
"
volumes:
- ./endpoints.txt:/endpoints.txt
environment:
- API_KEY=${API_KEY}
注意事项
-
安全考虑:
- 使用环境变量存储API密钥
- 实现认证和授权检查
- 添加操作日志记录
-
性能优化:
- 使用并发控制限制请求速率
- 实现重试机制处理临时错误
- 添加超时控制
-
错误处理:
- 记录失败的清除操作
- 实现部分成功的回滚机制
- 发送通知报告清除结果
-
监控告警:
- 记录清除操作的性能指标
- 设置失败率告警阈值
- 定期验证缓存是否被成功清除
根据你的具体需求选择合适的方法,记得在生产环境使用前进行充分测试。