更新缓存规则¶
1. 接口描述¶
接口请求路径:POST /prod-api/cdn/layer7/advance/modifyCacheRules
modifyCacheRules 用于新增或编辑指定域名下的缓存规则。新增时不传 locationId;编辑时需传入 查询缓存规则 返回的 locationId。
Token 获取方式见 密钥鉴权。
2. 输入参数¶
以下请求参数列表仅列出了接口请求参数和部分公共参数,完整公共参数列表见 公共请求参数。
| 参数名称 | 必选 | 类型 | 描述 |
|---|---|---|---|
| domainId | 是 | Integer | 域名 ID 示例值: 205 |
| type | 是 | Integer | 规则类型。取值:1(全部)、2(文件类型)、3(文件夹)、4(全路径)、5(首页)示例值: 2 |
| name | 是 | String | 规则内容。类型为全部时通常为 *;文件类型时多个后缀以分号分隔示例值: png;jpg;jpeg;gif;ico;bmp |
| cacheTime | 是 | Integer | 缓存时间,须 >= 0示例值: 0 |
| cacheTimeUnit | 是 | String | 缓存时间单位。取值:s(秒)、m(分钟)、h(小时)、d(天)示例值: s |
| ignoreHeaders | 否 | Array of String | 忽略的响应头。可选值:Expires、Cache-Control、Set-Cookie、X-Accel-Expires |
| locationId | 否 | Integer | 规则 ID。新增时不传;编辑时必填,取查询接口返回的 data.row[].locationId示例值: 888 |
3. 示例¶
示例1 新增缓存规则¶
输入示例¶
{
"domainId": 205,
"type": 2,
"name": "png;jpg;jpeg;gif;ico;bmp",
"cacheTime": 0,
"cacheTimeUnit": "s",
"ignoreHeaders": ["Expires", "Cache-Control"]
}
代码调用¶
请将 {登录域名}、token 替换为实际值。
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class ModifyCacheRulesCreateTest {
public static void main(String[] args) throws Exception {
String url = "https://{登录域名}/prod-api/cdn/layer7/advance/modifyCacheRules";
String token = "eyJhbGciOiJIUzUxMiJ9.xxxxxx";
String requestBody = "{"
+ "\"domainId\":205,"
+ "\"type\":2,"
+ "\"name\":\"png;jpg;jpeg;gif;ico;bmp\","
+ "\"cacheTime\":0,"
+ "\"cacheTimeUnit\":\"s\","
+ "\"ignoreHeaders\":[\"Expires\",\"Cache-Control\"]"
+ "}";
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Authorization", "Bearer " + token);
httpPost.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
int statusCode = response.getStatusLine().getStatusCode();
String body = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("Status Code: " + statusCode);
System.out.println("Body: " + body);
}
}
}
}
package main
import (
"bytes"
"fmt"
"io"
"net/http"
)
func main() {
url := "https://{登录域名}/prod-api/cdn/layer7/advance/modifyCacheRules"
token := "eyJhbGciOiJIUzUxMiJ9.xxxxxx"
requestBody := []byte(`{
"domainId": 205,
"type": 2,
"name": "png;jpg;jpeg;gif;ico;bmp",
"cacheTime": 0,
"cacheTimeUnit": "s",
"ignoreHeaders": ["Expires", "Cache-Control"]
}`)
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(requestBody))
if err != nil {
panic(err)
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println("Body:", string(body))
}
输出示例¶
示例2 编辑缓存规则¶
输入示例¶
{
"domainId": 205,
"locationId": 888,
"type": 2,
"name": "png;jpg;jpeg;gif;ico;bmp",
"cacheTime": 86400,
"cacheTimeUnit": "s"
}
代码调用¶
请将 {登录域名}、token 替换为实际值。
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class ModifyCacheRulesUpdateTest {
public static void main(String[] args) throws Exception {
String url = "https://{登录域名}/prod-api/cdn/layer7/advance/modifyCacheRules";
String token = "eyJhbGciOiJIUzUxMiJ9.xxxxxx";
String requestBody = "{"
+ "\"domainId\":205,"
+ "\"locationId\":888,"
+ "\"type\":2,"
+ "\"name\":\"png;jpg;jpeg;gif;ico;bmp\","
+ "\"cacheTime\":86400,"
+ "\"cacheTimeUnit\":\"s\""
+ "}";
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Authorization", "Bearer " + token);
httpPost.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
int statusCode = response.getStatusLine().getStatusCode();
String body = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("Status Code: " + statusCode);
System.out.println("Body: " + body);
}
}
}
}
package main
import (
"bytes"
"fmt"
"io"
"net/http"
)
func main() {
url := "https://{登录域名}/prod-api/cdn/layer7/advance/modifyCacheRules"
token := "eyJhbGciOiJIUzUxMiJ9.xxxxxx"
requestBody := []byte(`{
"domainId": 205,
"locationId": 888,
"type": 2,
"name": "png;jpg;jpeg;gif;ico;bmp",
"cacheTime": 86400,
"cacheTimeUnit": "s"
}`)
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(requestBody))
if err != nil {
panic(err)
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println("Body:", string(body))
}
输出示例¶
4. 输出参数¶
| 参数名称 | 类型 | 描述 |
|---|---|---|
| code | Integer | 状态码。示例值:200 |
| msg | String | 提示信息。示例值:更新成功 |
5. 错误码¶
| HTTP 状态码 / 业务提示 | 描述 |
|---|---|
| 200 | 成功 |
| 401 | 未鉴权或 Token 无效 |
| 403 | 无权限 |
| CDN10002 | 更新失败:忽略请求头信息参数不合法! |
| CDN10003 | 更新失败:缓存时间单位只能为:s / m / h / d! |
| CDN10004 | 更新失败:缓存时间只能为大于等于 0 的整数! |
| CDN10005 | 更新失败:规则目录类别错误! |
| CDN10031 | 更新失败:缓存时间不能超过 3650 天,请重新输入 |
| CDN10032 | 更新失败:缓存时间不能超过 604800000 秒,请重新输入 |
| CDN10033 | 更新失败:缓存时间不能超过 10000 分钟,请重新输入 |
| CDN10034 | 更新失败:缓存时间不能超过 10000 小时,请重新输入 |