简体   繁体   English

如何使用 REST API Java 使 Google CDN 缓存失效?

[英]How to invalidate Google CDN cache using REST API Java?

I have been researching for a working example for this but haven't found any.我一直在为此研究一个工作示例,但没有找到任何示例。

I referred following links Stackoverflow Link and Google Official Docs我参考了以下链接Stackoverflow LinkGoogle Official Docs

From these documentations I did understand that I need to implement this从这些文档中,我确实了解到我需要实现这一点

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.compute.Compute;
import com.google.api.services.compute.model.CacheInvalidationRule;
import com.google.api.services.compute.model.Operation;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;

public class ComputeExample {
  public static void main(String args[]) throws IOException, GeneralSecurityException {
    // Project ID for this request.
    String project = "my-project"; // TODO: Update placeholder value.

    // Name of the UrlMap scoping this request.
    String urlMap = "my-url-map"; // TODO: Update placeholder value.

    // TODO: Assign values to desired fields of `requestBody`:
    CacheInvalidationRule requestBody = new CacheInvalidationRule();

    Compute computeService = createComputeService();
    Compute.UrlMaps.InvalidateCache request =
        computeService.urlMaps().invalidateCache(project, urlMap, requestBody);

    Operation response = request.execute();

    // TODO: Change code below to process the `response` object:
    System.out.println(response);
  }

  public static Compute createComputeService() throws IOException, GeneralSecurityException {
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

    GoogleCredential credential = GoogleCredential.getApplicationDefault();
    if (credential.createScopedRequired()) {
      credential =
          credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
    }

    return new Compute.Builder(httpTransport, jsonFactory, credential)
        .setApplicationName("Google-ComputeSample/0.1")
        .build();
  }
}

BUT if you see this example it only has placeholder values in its place.但是,如果您看到这个例子,它只有占位符值。

IF I WANTED TO FLUSH CACHE OF A PAGE CALLED https://mywebsite.com/homepage.html WHERE WOULD I ENTER THIS INFORMATION IN THE ABOVE CODE?如果我想刷新名为 https://mywebsite.com/homepage.html的页面的缓存,我应该在上面的代码中哪里输入这些信息?

Do I added it here我在这里添加了吗

 credential.createScoped(Arrays.asList("https://mywebsite.com/homepage.html"));

OR Should I add it in UrlMaps?或者我应该在 UrlMaps 中添加它吗? This is very confusing.这非常令人困惑。

It should go in the request body.它应该在请求正文中 go 。 The request body contains data with the following structure :请求正文包含具有以下结构的数据

JSON representation
{
  "path": string,
  "host": string
}

These Fields takes followings:这些字段包含以下内容:

  • path as string路径作为字符串
  • host as string主机作为字符串

If set, this invalidation rule will only apply to requests with a Host header matching host.如果设置,此失效规则将仅适用于主机 header 匹配主机的请求。

You might need to create requestbody object您可能需要创建 requestbody object

CacheInvalidationRule requestBody = new CacheInvalidationRule();

this should creates cacheinvalidationrule object and assigns to requestBody这应该创建缓存无效规则 object 并分配给 requestBody

Additionally, you might also need to something like this此外,您可能还需要这样的东西

requestBody.setHostand requestBody.setPath = ""

This two properties takes string as an argument这两个属性将字符串作为参数

requestBody.setHost=" mywebsite.com"

and

requestBody.setPath = "/homepage.html"

Hope it helps, good luck希望对你有帮助,祝你好运

I would suggest to checking developers.google.com Compute.UrlMaps.InvalidateCache class , use Method summary description I believe it would be helpful for you to understand this class and how to incorporate it in your code.我建议检查developers.google.com Compute.UrlMaps.InvalidateCache class使用方法摘要描述 我相信这将有助于您了解此 class 以及如何将其合并到您的代码中。 It contains method details and parameter description for example它包含方法详细信息和参数描述,例如

Constructor Detail InvalidateCache构造函数详细信息InvalidateCache

protected InvalidateCache(java.lang.String project,
                          java.lang.String urlMap,
                          CacheInvalidationRule content)

Initiates a cache invalidation operation, invalidating the specified path, scoped to the specified UrlMap.启动缓存失效操作,使指定路径失效,范围为指定 UrlMap。 Create a request for the method "urlMaps.invalidateCache".为方法“urlMaps.invalidateCache”创建一个请求。 This request holds the parameters needed by the the compute server.此请求包含计算服务器所需的参数。 After setting any optional parameters, call the AbstractGoogleClientRequest.execute() method to invoke the remote operation.设置任何可选参数后,调用 AbstractGoogleClientRequest.execute() 方法来调用远程操作。

InvalidateCache#initialize(com.google.api.client.googleapis.services.AbstractGoogleC lientRequest)

must be called to initialize this instance immediately after invoking the constructor.必须在调用构造函数后立即调用以初始化此实例。

Parameters:参数:

  • project - Project ID for this request. project - 此请求的项目 ID。
  • urlMap - Name of the UrlMap scoping this request. urlMap - 确定此请求范围的 UrlMap 的名称。
  • content - the CacheInvalidationRule内容 - CacheInvalidationRule

Method detail for example for setAlt set例如 setAlt set 的方法详细信息

public Compute.UrlMaps.InvalidateCache set(java.lang.String parameterName,
                                           java.lang.Object value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM