Cache duration refers to the amount of time that a resource is cached on Dynamic Route for CDN (DCDN) nodes. By default, the minimum cache duration is 10 seconds, and the maximum is 3,600 seconds. Expired resources are automatically removed from DCDN nodes. Requests that attempt to access expired resources are considered invalid and redirected to the origin server. Then, DCDN retrieves the requested resources from the origin server and caches them on the DCDN nodes. You can set a cache duration for static resources based on file directories or filename extensions.

Background information

The following figure shows how cache rules are applied to resources on DCDN nodes. Cache rule
Cache rule on the origin server? Cache rule in the DCDN console? Cache behavior on DCDN nodes
Yes, the cache rule is set to cache-control:no-cache or cache-control:no-store. N/A Follows the cache rule that is set on the origin server. Resources retrieved from the origin server are not cached on DCDN nodes. Each request is redirected to the origin server. No content acceleration is achieved.
Yes, but the cache rule is not set to cache-control:no-cache or cache-control:no-store. Yes
Follows the cache rule that is set in the DCDN console. The cache rules set in the DCDN console are applied based on the following priorities: weight > filename extension > directory.
Note
  • A resource retrieved from the origin server by DCDN may match two cache rules that have the same weight. One cache rule is based on filename extensions and the other is based on directories. In this case, the cache rule that is based on filename extensions applies because it has a higher priority.
  • If a resource matches two cache rules that have the same settings, for example, both are based on filename extensions or directories, and have the same weight, one of the matched rules is applied at random.
No Follows other cache rules that are set on the origin server.

If the response header carries the cache-control and expires cache rules, the cache-control rule has a higher priority than the expires rule. In this case, only the cache-control rule takes effect.

No Yes
Follows the cache rule that is set in the DCDN console. The cache rules set in the DCDN console are applied based on the following priorities: weight > filename extension > directory.
Note
  • A resource retrieved from the origin server by DCDN may match two cache rules that have the same weight. One cache rule is based on filename extensions and the other is based on directories. In this case, the cache rule that is based on filename extensions applies because it has a higher priority.
  • If a resource matches two cache rules that have the same settings, for example, both are based on filename extensions or directories, and have the same weight, one of the matched rules is applied at random.
No Follows the default cache rules of Alibaba Cloud DCDN. By default, the minimum cache duration is 10 seconds, and the maximum is 3,600 seconds. The default cache rules are as follows:
  • If the response from the origin server contains the last_modified header, the default cache duration is calculated as: (Current time - last_modified) × 0.1. The last_modified header indicates the time when the resources on the origin server were last modified.
  • If the response from the origin server does not contain the last-modified header but contains the ETag header, the default cache duration is 10 seconds.
  • If the response from the origin server does not contain the last-modified or ETag header, the retrieved resources are not cached on DCDN nodes.

Scenarios

Alibaba Cloud DCDN nodes respond to requests based on the cache status. If the requested resource is not cached on DCDN nodes, the request is redirected to the origin server to retrieve the resource. After the resource is retrieved from the origin server, an HTTP 2xx status code is returned to the DCDN node. The retrieved resource is cached on the DCDN node and returned to the client.

Cache duration is ideal for the following scenarios:
  • The resources on the origin server are updated, but the cache on DCDN nodes is not updated.
  • Requests are frequently redirected to the origin server because the resources cached on DCDN nodes frequently expire. This results in a low cache hit ratio.
  • Resources take a long time to load.

Usage notes

  • To differentiate resources before and after they are updated on the origin server, we recommend that you use different names for resources before and after they are updated when you configure the cache duration for static resources. You can use version numbers to differentiate resources. For example, you can name a resource img-v1.0.jpg and name the updated resource img-v2.1.jpg.
  • The cache duration affects the amount of network traffic redirected to the origin server. The data transfer fee varies based on the amount of back-to-origin network traffic. We recommend that you set a cache duration based on your business requirements. A short cache duration may cause DCDN nodes to frequently redirect requests to the origin server and increase the consumption of network traffic on the origin server.
  • Cached resources that are requested less frequently may be removed from the DCDN nodes before they expire.

Procedure

  1. Log on to the DCDN console.
  2. In the left-side navigation pane, click Domain Names.
  3. On the Domain Names page, find the domain name that you want to manage, and click Configure in the Actions column.
  4. In the left-side navigation pane on the details page of the specified domain name, click Caching.
  5. On the Cache Duration tab, click Add.
  6. In the Cache Duration dialog box, set the parameters.
    Configure the cache duration
    Parameter Description
    Type You can select Directory or Filename Extension. Select a type based on your business requirements.
    • Directory: specifies the resources that are in a specified directory.
    • Filename Extension: specifies the file types of resources.
    Content
    • If you select Directory, take note of the following rules:
      • You can enter only one directory at a time. You can use a forward slash (/) to specify all directories.
      • You can enter a full path. It must start with a forward slash (/), for example, /directory/aaa.
    • If you select Filename Extension, take note of the following rules:
      • You can enter one or more filename extensions and separate them with commas (,), for example, jpg,txt. Filename extensions are case-sensitive.
        The following static file types are supported:
        • Images: GIF, PNG, BMP, JPEG, and JPG.
        • Web pages: HTML, HTM, and SHTML.
        • Audio and video files: MP3, WMA, FLV, MP4, WMV, OGG, and AVI.
        • Text files: DOC, DOCX, XLS, XLSX, PPT, PPTX, TXT, and PDF.
        • Others: ZIP, EXE, TAT, ICO, CSS, JS, SWF, APK, M3U8, and TS.
      • You cannot use an asterisk (*) to specify all file types.
    Expire In Specifies the cache duration of cached resources. The maximum cache duration is three years. Take note of the following rules:
    • Specifies a cache duration of one month or longer for static files that are updated less frequently, such as images and application packages.
    • Specifies a cache duration based on your business requirements for static files that are frequently updated, such as JS and CSS files.
    • Specifies a cache duration of 0 seconds to disable caching for dynamic files, such as PHP, JSP, and ASP files.
    Weight Specifies the weight of the cache rule. The weight indicates the priority of the cache rule. Valid values: 1 to 99. A greater value indicates a higher priority.
    Note
    • We recommend that you set a unique weight for each cache rule. If two cache rules are of different types but have the same weight, the cache rule that is based on filename extensions has a higher priority than the other. If two cache rules are of the same type and have the same weight, one of the rules is applied at random.
    • If you have configured multiple cache rules for a cached resource, only the first matched rule is applied.
  7. Click OK.

    After you configure a cache rule, you can Modify or Delete the rule on the Cache Duration tab.

Examples

In the following examples, three cache rules are configured for the accelerated domain name example.aliyun.com. Rule 1 has the highest priority. If Rule 1 is applied, other rules are skipped.
  • Rule 1: Type is set to Filename Extension, Content is set to jpg and png, Expire In is set to 1 month, and Weight is set to 90.
  • Rule 2: Type is set to Directory, Content is set to /www/dir/aaa, Expire In is set to 1 hour, and Weight is set to 70.
  • Rule 3: Type is set to Directory, Content is set to a full path /www/dir/aaa/example.php, Expire In is set to 0 seconds, and Weight is set to 80.

Related operations

BatchSetDcdnDomainConfigs