gpt4 book ai didi

java - 通过java代码清除akamai中缓存的url?

转载 作者:行者123 更新时间:2023-11-30 06:01:33 27 4
gpt4 key购买 nike

在我的项目中,每当调用 API 时,它都会缓存在 Akamai 中。但是当客户端通过 UI 更改数据库中的某些内容时。我们需要使 AKAMAI 中缓存的 API 响应失效,并用新的新鲜 json 数据填充它。我在互联网上找到了一些链接:akamai-purging但我无法理解他们正在谈论的这个链接中的 cp-code 是什么?

这是我的示例代码,给出:405 不允许

代码:

public static void main(String[] args) throws IOException, RequestSigningException {

URL url = new URL("https://xxx-host-name-/scripts.4535eaf743502b25ba3a.js");

HttpTransport HTTP_TRANSPORT = new ApacheHttpTransport();
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
AkamaiPostData postData = new AkamaiPostData();
postData.setHostname(AkamaiConstants.SITE_HOST_NAME);

Gson gson = new Gson();
String postDataJSON = gson.toJson(postData);
byte[] contentBytes = postDataJSON.getBytes();
HttpContent content = new ByteArrayContent("application/json", contentBytes);
HttpRequest request = requestFactory.buildDeleteRequest(new GenericUrl(url));
HttpHeaders headers = request.getHeaders();
headers.set("Host", "xxx-host-name-");

ClientCredential credential = new DefaultCredential(AkamaiConstants.CLIENT_TOKEN, AkamaiConstants.ACCESS_TOKEN, AkamaiConstants.CLIENT_SECRET);
RequestSigner signer = new EdgeGridV1Signer(Collections.EMPTY_LIST, 1024 * 2);
HttpRequest signedRequest = signer.sign(request, credential);
HttpResponse response = signedRequest.execute();
String result = response.parseAsString();
System.out.println("result::" + result);
}

最佳答案

所以我终于能够做到了。我使用的这种方法是Akamai提供的快速清除方法。

代码示例:

public class AkamaiFastPurge {

private static final String HTTPS = "https";

public static void main(String... s) throws URISyntaxException, IOException, RequestSigningException {


ClientCredential credential = ClientCredential.builder()
.accessToken("Your-access-token")
.clientToken("Your-client-token")
.clientSecret("Your-client-secret")
.host("Your-host")
.build();


ArrayList arrayList = new ArrayList<String>();

// You can add multiple urls.
arrayList.add("*****-Actual-url-you-want-to-purge*****");

HttpResponse response = invalidateUrls(credential, arrayList, "production");

System.out.println(response.getStatusCode());
System.out.println(response.getStatusMessage());
}

public static HttpResponse invalidateUrls(ClientCredential clientCredential, List<String> urls, String network) {

HttpTransport httpTransport = new ApacheHttpTransport();
HttpRequestFactory requestFactory = httpTransport.createRequestFactory();

HttpRequest request = null;

try {

// This is fast purge approach
URI uri = new URI(HTTPS, "api.ccu.akamai.com", "/ccu/v3/invalidate/url/" + network, null, null);

String requestBody = getStringRequestBody(urls);

request = requestFactory.buildPostRequest(new GenericUrl(uri), ByteArrayContent.fromString("application/json", requestBody));
GoogleHttpClientEdgeGridRequestSigner requestSigner = new GoogleHttpClientEdgeGridRequestSigner(clientCredential);
requestSigner.sign(request);

return request.execute();

} catch (IOException e) {
// log.error("IOException in Akamai Utility", e);
} catch (RequestSigningException e) {
// log.error("RequestSigningException in Akamai Utility", e);
} catch (URISyntaxException e) {
// log.error("UriSyntaxException in Akamai Utility", e);
}

return null;
}

public static String getStringRequestBody(List<String> urls) {

Map<String, List<String>> akamaiRequestMap = Maps.newHashMap();
akamaiRequestMap.put("objects", urls);

return new Gson().toJson(akamaiRequestMap);
}
}

我使用的依赖项:

 <dependency>
<groupId>com.akamai.edgegrid</groupId>
<artifactId>edgegrid-signer-google-http-client</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
</dependencies>

关于java - 通过java代码清除akamai中缓存的url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52192577/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com