本示例介绍如何使用Alibaba Cloud SDK for Java调用PushObjectCache接口将源站的内容主动预热到L2 Cache节点上,用户首次访问可直接命中缓存,缓解源站压力。

前提条件

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-cdn</artifactId>
    <version>3.0.8</version>
</dependency>

示例代码

package com.ygx.testprogram;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.cdn.model.v20180510.PushObjectCacheRequest;
import com.aliyuncs.cdn.model.v20180510.PushObjectCacheResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

public class TestPushObjectCaches {

    public static void main(String[] args) {
        // 初始化参数
        DefaultProfile profile = DefaultProfile.getProfile( 
            "<your-region-id>",             // 您的可用区ID
            "<your-access-key-id>",         // 您的AccessKey ID
            "<your-access-key-secret>");    // 您的AccessKey Secret
        IAcsClient client = new DefaultAcsClient(profile);

        PushObjectCacheRequest request = new PushObjectCacheRequest();
        // 预热区域。取值: domestic、overseas。
        request.setArea("overseas");
        // 需要预热的URL,多个URL之间需要用换行符\n或\r\n分隔。
        request.setObjectPath("abc.com/image/1.png");

        try {
            PushObjectCacheResponse response = client.getAcsResponse(request);
            // 在这里处理自己的业务逻辑和返回参数
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}
说明 更多接口内容,请参见新版API参考