To improve the cache hit ratio on CDN points of presence (POPs), you can configure cache rules to manage the validity periods and specify the priorities of cache rules. After a resource expires, it is automatically deleted from the POP and re-cached on the POP from the origin server. This topic describes how to configure cache rules for resources on POPs and how to specify validity periods for cached resources.

Overview

  • If you do not configure cache rules on the origin server or POPs, the default validity period is used for cached resources. By default, cached resources are valid for 3,600 seconds. After you add a domain name to ApsaraVideo VOD, you can change the default validity period. The amount of origin traffic and the fees incurred vary based on the validity period that you specify. We recommend that you specify a validity period based on your business requirements. A short validity period may cause POPs to frequently redirect requests to the origin server and increase the amount of network traffic that is consumed by the origin server.
  • The default cache rule specifies the validity period of cached resources. The cache rule that you configure on POPs is assigned a higher priority than the cache configurations on the origin server. If no cache configuration is specified on the origin server, you can configure a cache rule based on the directory or file name extension in the ApsaraVideo VOD console.
  • If the cached files are not frequently accessed, they may be removed from the POP before they expire.
  • If a client request includes the If-Match header and the response from the origin server includes the ETag header, the POP directly returns the requested resource to the client when the value of the If-Match header is the same as the value of the ETag header. If the values of the If-Match header and the ETag header are different, the POP retrieves the latest version of the requested resource from the origin server, and then returns the requested resource to the client. During this process, the resource that is cached on the POP is replaced with the retrieved version. POPs compare the If-Match value with the ETag value before they apply cache rules.

When you update a resource file on your origin server, we recommend that you include the version number in the name of the update file instead of using the same name as the existing resource file. For example, you can specify img-v1.0.jpg and img-v2.1.jpg as the names of two update files. Then, you can configure a cache rule for the resource file. The following figure shows the cache rules for the resources that are cached on POPs.

Cache rules

Procedure

  1. Log on to the ApsaraVideo VOD console.
  2. In the left-side navigation pane, click Configuration Management.
  3. On the Domain Names page, select the domain name that you want to configure, and click Configure in the Actions column.
  4. On the page that appears, click Cache. On the Cache Validity tab, click Add.
  5. Configure a cache rule. Set the Type parameter to Folder or File Extension.
    Cache Validity
    The following table describes the parameters.
    ParameterDescription
    Type
    • Folder: specifies the resources that are cached in the specified directory.
    • File Extension: specifies the resources that are cached in files with the specified file name extensions.
    Address
    • If you set Type to Folder, enter a directory that starts with a slash (/), such as /directory/aaa. You can enter a full path.
    • If you set Type to File Extension, use commas (,) to separate multiple file name extensions. Example: JPG,TXT.
    Validity PeriodThe validity period of the cached resources. The maximum validity period that you can specify is three years. We recommend that you set this parameter based on the following rules:
    • Specify a validity period of one month or longer for static files such as images and application packages that are not frequently updated.
    • Specify a validity period for static files such as JavaScript and CSS files that are frequently updated based on your actual workloads.
    • Specify a validity period of 0 seconds to disable caching for dynamic files such as PHP, JSP, and ASP files.
    WeightThe priority of the cache rule.
    Note
    • The value must be an integer from 1 to 99. A higher value specifies a higher priority. A rule that is assigned a higher priority takes precedence over rules with lower priorities.
    • We recommend that you do not specify the same priority for different rules. If two rules are assigned the same priority, one of the rules is randomly applied.
    • After a cache rule takes effect, other rules are not applied.
    For example, if you set the following rules for example.aliyun.com, rule 1 is preferentially applied over other rules.
    • Rule 1: The Type parameter is set to File Extension, the Address parameter is set to jpg.png, the Validity Period Parameter is set to 1 Month, and the Weight parameter is set to 90.
    • Rule 2: The Type parameter is set to Folder, the Address parameter is set to /www/dir/aa, the Validity Period Parameter is set to 1 Hour, and the Weight parameter is set to 70.
    • Rule 3: The Type parameter is set to Folder, the Address parameter is set to /www/dir/aaa/example.php, the Validity Period Parameter is set to 0 Seconds, and the Weight parameter is set to 80.
  6. Click OK.