Automate flushing of CDN cache

You can manually flush the CDN cache directly from your Cockpit. However, if you would like to automate the process where you automatically flush publication actions, you need to do a few things.

Set up a REST Client

  1. Request a token from our Helpdesk.

  2. Create a REST client in Magnolia to access the Fastly API. You can see more at REST Client app.

    Example REST Client configuration
    baseUrl: https://api.fastly.com
    restCalls:
      purge_all:
        method: POST
        entityClass: java.lang.String
        path: '/service/{{service_id}}/purge_all'
        headers:
          'Accept': 'application/json'
          'Fastly-Key': '{{fastly token}}' (1)
    1 The token needed to access the Fastly API.

Configure a Webhook

If you want the purge to be triggered by certain events, you can configure a webhook. See the example configuration below.

See Webhooks module for more details on configuring webhooks.

Example webhook configuration
name: webhook_purge_fastly_cache
restClientName: fastly_rest (1)
restCallName: purge_all (2)
enabled: true

events:
  - name: contentPublished
    eventType: Published
    filter: "@path LIKE '%foo2%' or @nodeType = 'mgnl:page'"
  - name: contentUnpublished
    eventType: Unpublished
    filter: "@path LIKE '%foo2%' or @nodeType = 'mgnl:page'"
1 Should match your REST Client’s name.
2 Should match your REST Client’s specific call - in this case '…​/purge_all' as referenced in path: under Set up a REST Client.

Write a command

If you’d prefer using a command, you can create a command definition to flush the CDN cache.

Your custom command will look at least slightly different than the example provided here. However, this can be used as a guide to completing your own command.

See Commands for more more comprehensive details on writing, configuring, and executing commands.

Command example
import info.magnolia.commands.MgnlCommand;
import info.magnolia.context.Context;
import info.magnolia.init.MagnoliaConfigurationProperties;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;

public class AutomateFlushCDNCache extends MgnlCommand {

    private static final String FASTLY_API_URL_TEMPLATE = "https://api.fastly.com/service/%s/purge_all"; (1)

    public AutomateFlushCDNCache() {
    }

    @Deprecated
    public AutomateFlushCDNCache(MagnoliaConfigurationProperties configurationProperties) {
        this();
    }

    @Override
    public boolean execute(Context context) {
        try {
            // Purge all content in the Fastly service
            // TODO: get those values from configuration; this is just an example with hardcoded values
            String serviceID = "<YOUR_SERVICE_ID>"; (2)
            String fastlyToken = "<YOUR_FASTLY_TOKEN>"; (3)
            purgeFastlyService(serviceID, fastlyToken);
            return true;
        } catch (IOException e) {
            // Log or handle the exception appropriately
            e.printStackTrace();
            return false;
        }
    }

    private void purgeFastlyService(String serviceID, String fastlyToken) throws IOException {
        String fastlyApiUrl = String.format(FASTLY_API_URL_TEMPLATE, serviceID);

        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(fastlyApiUrl);

        // Set headers
        httpPost.setHeader(HttpHeaders.ACCEPT, "application/json");
        httpPost.setHeader("Fastly-Key", fastlyToken);

        // Execute the request
        try {
            HttpResponse response = httpClient.execute(httpPost);
            handleResponse(response);
        } finally {
            httpClient.getConnectionManager().shutdown();
        }
    }

    private void handleResponse(HttpResponse response) throws IOException {
        int statusCode = response.getStatusLine().getStatusCode();

        if (statusCode == 200) {
            // Successful purge
            System.out.println("Fastly service purged successfully.");
        } else {
            // Handle error
            System.err.println("Failed to purge Fastly service. Status code: " + statusCode);
        }

        // Ensure the response entity is fully consumed to release resources
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            entity.getContent().close();
        }
    }
}
1 Define the FASTLY_API_URL_TEMPLATE with the purge_all Fastly API endpoint.

For more, see purge-all.

2 Use your YOUR_SERVICE_ID.
3 Use your YOUR_FASTLY_TOKEN.
Feedback

PaaS

×

Location

This widget lets you know where you are on the docs site.

You are currently perusing through the DX Cloud PaaS docs.

Main doc sections

DX Core Headless PaaS Legacy Cloud Incubator modules