Limited Time Free!  Sign up for 1TB of data transfer and get free trials of WAF and Bot Management!
Get Started Now 
Tencent EdgeOne Blog
Tencent EdgeOne Blog
Tutorial

Different Ways to Configure Caching in EdgeOne CDN

Tencent EdgeOne - Product Manager

Tencent EdgeOne is Tencent Cloud’s security and site acceleration solution for the global market built on Tencent’s edge node network. In this post, we'll discuss the various ways to configure an EdgeOne caching configuration and the differences between them. We'll conclude by walking through an Edge Function code sample.

Similar but Different

There are three ways to define your EdgeOne CDN caching configuration: Global Settings, Rule Engine, and Edge Functions.

1. Global Settings - The definitions in Global Settings are applied at a site-wide level. In general, they apply no matter the hostname, URL, file extension, etc.. This is by far the easiest, but most limited, way to set up basic caching rules as the configuration is GUI-based and does not require any coding experience.

global settings in EdgeOne console

 

2. Rule Engine - The definitions in Rule Engine are applied at a more granular level than Global Settings. Within Rule Engine, you can implement IF-THEN programming logic without having to write actual code. For example, "IF the resource's file extension ends in .mp4, THEN set cache TTL to 24 hours".

rule engine

 

3. Edge Functions - The definitions in Edge Function are the most granular. Here, you can write custom rules and logic using a JavaScript service worker, giving you the flexibility to implement your own business logic and test it within the Tencent Cloud console. Writing an Edge Function is considered fairly advanced so only use it if you are comfortable with JavaScript.

edge functions

What's their processing priority?

  • Anything you define in an Edge Function will take priority over Rule Engine and Global Settings;
  • Anything you define in Rule Engine will take priority over Global Settings;
  • Anything not defined in an Edge Function nor Rule Engine will be processed via Global Settings.

Priority Level

Name

P1

Edge Function

P2

Rule Engine

P3

Global Settings

We recommend starting with Global Settings when you configure your EdgeOne cache. If you need to define more granular logic, move to Rule Engine. And if you find yourself hitting constraints within Rule Engine, use Edge Functions.

Edge Function Walk-Through

Let's dive deeper into Edge Functions. An Edge Function is a JavaScript service worker that sits between the client and EdgeOne CDN. Its objective is to intercept HTTP requests and responses objects and modify them, based on the code you write.

Let's say a web browser makes a request to EdgeOne at the URL “https://example.com/image-1.jpeg”. You can write an Edge Function service worker that intercepts all requests made to this URL and rewrites them tohttps://example.com/image-2.jpeg”. The rewritten request is then passed on to EdgeOne CDN, which then follows its normal process to retrieve the resource from cache.

Conversely, Edge Functions can also modify responses. In the same example, the HTTP response for “https://example.com/image-2.jpeg” is returned to the Edge Function by EdgeOne CDN. The Edge Function can then modify the response by, for example, adding a custom header “my-response-header: hello world ” before the response is finally returned to the client.
Remember:

  • The input of an Edge Function is an HTTP Request. The Request is then forwarded to EdgeOne CDN.
  • The output of an Edge Function is an HTTP Response. The Response is then returned to the client.

For more detailed information, check out Mozilla's official description of the Service Worker API .

Managing Your Edge Functions

After logging in to the Tencent Cloud Console, navigate to Edge Functions > Function Management and Edge Functions > Function Triggers.

Within the Function Management page, you can create new Edge Functions using either pre-made templates or writing your own code from scratch. You can also use this page to view, edit, and delete existing Edge Functions.

Within the Function Triggers page, specify rules on when the Edge Function should execute. For example, you can specify that a given Edge Function should execute on every single HTTP request, or only when there is a match on a specific Host.

Code Sample

Now, let’s illustrate how an Edge Function is implemented via a sample code snippet.

async function handleRequest(request) {
  // Add custom request header
  request.headers.set('X-Custom-Request-Header', 'my-request-value');

  try {
    // Fetch the modified request
    const response = await fetch(request);

    // Add custom response header
    response.headers.set('X-Custom-Response-Header', 'my-response-value');

    return response;
  } catch (error) {
    return new Response('Network error occurred', { status: 408 });
  }
}

// Listen for a fetch event and pass the request as input to handleRequest()
addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request));
});

A few notes on the sample above:

  1. The code modifies the incoming HTTP request to the CDN with a custom header, using the request.headers.set() method.
  2. The code modifies the HTTP response with a custom header, using the response.headers.set() method, before the response is sent to the client.
  3. The fetch() method will forward the request to EdgeOne CDN. If the resource is found in cache, it will return it immediately. If not, EdgeOne CDN will forward the request to the upstream origin.
  4. Every Edge Function requires an addEventListener() method as it causes the handleRequest() method to execute.

Expanding on this sample, you can imagine how powerful Edge Functions can be since they allow you to intercept and modify network requests and responses on the fly. Using Edge Functions, you can implement things such as rewriting video manifests, header and cookie based authentication, serving geo-specific content, and more. For further reading, take a look at the official EdgeOne documents.

About Author

Ray Fung is a Solutions Architect at Tencent. He is a member of the Tencent Cloud International team and focuses on the EdgeOne service for customers based in North America. His area of expertise encompasses content delivery for video (VOD and live streaming) and game/software downloads, and has worked with some of the most recognized media companies including Disney+, HBO, Prime Video, NFL, Microsoft, and Valve.


Tencent EdgeOne is an integrated security and acceleration platform based on 3,200 Tencent Cloud edge nodes worldwide. It supports domain name resolution, dynamic/static content acceleration, L4 TCP/UDP acceleration, DDoS mitigation/CC protection/Web application firewall/Bot protection, edge function compute, and more. It helps customers respond to user requests more quickly, securely, and flexibly.

Don't miss out on exclusive content - follow us!

follow us on xfollow us on youtubecontact us now
Develop
EdgeOne
Edge Function