Customize cache keys

更新时间:
复制 MD 格式

A cache key is the unique identifier for an object cached on a DCDN node. By default, the cache key is the request URL, including any query parameters. By customizing the cache key, you can consolidate multiple request URLs for the same object into a single cache key. This improves your cache hit ratio, reduces the back-to-origin request rate, and conserves back-to-origin traffic. This topic describes how to configure a custom cache key.

Use cases

Use case 1:

Your application generates URLs with dynamic or complex query parameters for the same object. By default, DCDN treats each unique URL as a separate object, leading to multiple cached copies, a lower cache hit ratio, and increased back-to-origin requests.image03

You can configure a custom cache key to ignore irrelevant parameters, ensuring all requests for the same object use one cache key. This optimizes caching and reduces back-to-origin traffic.image04

Use case 2:

You serve different versions of an object based on a value in the request's HTTP header, such as a client field that specifies the operating system. Although the request URLs are identical, the content served is different.image05

You can customize the cache key to include the value of the client HTTP header. This lets DCDN create and serve distinct cached versions of the object based on the client's operating system.image06

Procedure

  1. Log on to the DCDN console.

  2. On the Domain Names page, find the domain name that you want to manage and click Configure in the Actions column.

  3. In the left-side navigation pane for the domain name, click Cache.

  4. On the Custom Cache Key tab, configure the cache key.

    Note
    • The Custom Cache Key feature allows you to configure rules for URIs, Parameter Actions, and HTTP headers. It also supports custom variables to extract specific fields from a request. The final cache key is a combination of these four components.

    • Customizing the cache key does not modify the back-to-origin URL. It only changes the cache identifier for the request. The content of the back-to-origin request remains identical to the original client request.

    自定义Cachekey

  5. Click OK.

Examples

URIs

Client requests for http://example.com/a/b/image_01.jpg and http://example.com/a/b/c/image_01.jpg are treated as a request for the same file. The Cachekey for this file is http://example.com/c/image_01.jpg.URI

Parameter Actions

The client request http://example.com/a/b/image_01.jpg?delete_par=1&modify_par=1 is modified by the rule, which adds add_par=1, deletes delete_par, and changes the value of modify_par to 2, resulting in the final URL http://example.com/a/b/image_01.jpg?modify_par=2&add_par=1.

Important

When multiple actions are configured for the same parameter, the order of precedence is: Add > Delete > Retain Only > Modify.

参数操作

HTTP Headers

The values of the User-Agent and Accept-Language HTTP headers are appended to the cache key. For example, for a request to http://example.com/a/b/image_01.jpg with User-Agent=Mozilla/5.0 (Linux; X11) and Accept-Language=en, the cache key is http://example.com/a/b/image_01.jpgMozilla/5.0(Linux;X11)en.HTTP Header

Custom Variables

Example 1:

The variable name is language, the source is Request Header, the source field name is Accept-Language, the matching rule is ([%w]+),([%w]+), and the variable expression is $1aa.Custom variable

When a client sends a request for http://example.com/a/b/image_01.jpg with the HTTP request header Accept-Language=en,ch, the matching rule matches en and assigns it to $1 in the variable expression. The variable expression then appends aa to create the variable enaa, assigns it the alias language, and appends the alias to the URL to form the final cache key: http://example.com/a/b/image_01.jpgenaa.

Note

In a variable expression, $n represents the content matched by the n-th parenthesis in the matching rule. For example, if the string is Accept-Language=en,ch and the matching rule is ([%w]+),([%w]+), then $1=en and $2=ch.

Example 2:

The variable name is expired, the source is Request Cookie, the source field name is a, the matching rule is [%w]+:(.*), and the variable expression is $1.custom variable

For a client request to http://example.com/a/b/image_01.jpg with the cookie Cookie a=expired_time:12635187, the matching rule matches 12635187, assigns it to $1 in the variable expression, aliases it as expired, and appends it to the URL to form the final cache key: http://example.com/a/b/image_01.jpg12635187.

Example 3:

Combine URI rules and custom variables.

URIs:

Aggregates all requests with URIs that match /abc/.*/abc into /abc.Example 3

Custom variables:

The variable name is testname, the source is Path, the matching rule is /abc/xyz/(.*), and the variable expression is $1.Example 3

For the client request URL http://example.com/abc/xyz/abc/image_01.jpg, the cache key is modified to http://example.com/abc/image_01.jpg based on the URI configuration. Then, based on the custom variable configuration, the URL matches /abc/xyz/(.*). In this case, $1 is assigned the value abc and is appended to the cache key, forming the final cache key: http://example.com/abc/image_01.jpgabc. This combination of two rules allows you to implement more complex caching logic.