Configure cache policies for an application
Set how long an object stays cached, bypass the cache for a path, and forward the origin's Set-Cookie header to users.
From Azion Console or the API, you set how long an object stays cached, bypass the cache for a path, and forward the origin’s Set-Cookie header. A cache setting carries the cache policy, and a Rules Engine rule applies it to the requests you choose.
To vary a cached object by a query string argument or by a cookie, refer to Configure Advanced Cache Key.
Prerequisites
Console
- An application. To create one, refer to Applications quickstart.
- Access to Azion Console. Refer to Access Azion Console.
- Application Accelerator can generate usage-related costs. For the rates, refer to Pricing.
Turn on Application Accelerator
A cache setting needs no module, and any application can carry one. Application Accelerator is required for a Max Age below 60 seconds, for cache variation, and for the Bypass Cache and Forward Cookies behaviors. The module is off by default. To turn it on:
Access Azion Console > Applications > your application.
On the Main Settings tab, in the Modules section, turn on Application Accelerator.
The application runs with Application Accelerator. Its cache settings accept a Max Age below 60 seconds, and Bypass Cache and Forward Cookies become available in Rules Engine.
Set the cache TTL for a path
A cache setting holds the time-to-live (TTL) the browser applies and the TTL Azion applies. To create one:
Access Azion Console > Applications > your application, then go to the Cache Settings tab.
In Name, enter a name that identifies the setting. For example: /target-uri - Cache TTL.
Under Browser Cache, select Override cache settings and set the maximum age in seconds.
Under Cache, select Override cache behavior and set Max Age to the number of seconds Azion keeps the object.
The cache setting appears in the Cache Settings tab. The same setting can also vary the cached object by query string or by cookie, which is the Advanced Cache Key feature.
A cache setting takes effect only when a rule applies it. To apply this one to the /target-uri path:
Enter a name for the rule. For example: /target-uri - Cache TTL.
In the Criteria section, select the ${uri} variable.
Enter /target-uri as the argument.
Requests whose URI starts with /target-uri carry the cache setting, at both the browser TTL and the Azion TTL you set. A new rule takes a few minutes to propagate.
Bypass the cache for a path
Bypass Cache stops Azion from caching the origin response for the requests a rule matches. To create the rule:
Access Azion Console > Applications > your application, then go to the Rules Engine tab.
Enter a name for the rule. For example: /target-uri - Bypass Cache.
In the Criteria section, select the ${uri} variable.
Enter /target-uri as the argument.
Requests whose URI starts with /target-uri reach the origin, and Azion stores no response for them. A new rule takes a few minutes to propagate.
For more information, refer to Bypass Cache. For the difference between Bypass Cache and a Max Age of 0 seconds, refer to Cache variation.
Bypass the cache for a path with the API
Bypass Cache can also be set with the API, by sending a rule with a bypass_cache behavior. The example bypasses the cache on /api/, with a rule named cdn - api pass-through. Replace each value with your own. To create the bypass rule:
The API answers 202 with a state of pending and the rule as it was stored.
Requests whose URI starts with /api/ reach the origin, and Azion stores no response for them. A new rule takes a few minutes to propagate.
To confirm the rule, request a path it matches with the Pragma: azion-debug-cache header:
The response carries x-cache: BYPASS. For how to read the header, refer to Check the cache status of a response.
The Accelerate websites and APIs with a CDN use case uses the values of this example.
These checks confirm the Accelerate websites and APIs with a CDN use case.
Forward cookies from the origin to the user
Forward Cookies sends the origin’s Set-Cookie header to users, including on a cache hit. To create the rule:
Access Azion Console > Applications > your application, then go to the Rules Engine tab.
Enter a name for the rule. For example: /target-uri - Forward Cookies.
In the Criteria section, select the ${uri} variable.
Enter /target-uri as the argument.
Requests whose URI starts with /target-uri return the origin’s Set-Cookie header to the user.
The forwarded header can carry session cookies. To stop a user from receiving the session cookies of another user:
In Name, enter a name that identifies the setting. For example: /target-uri - Private cookies.
Under Application Accelerator, open Cache vary by Cookies and set Behavior to Denylist.
Add each session cookie to the list of cookie names.
The cache setting appears in the Cache Settings tab, and it keeps the listed cookies out of the cache key.
To apply that cache setting to the rule that forwards the cookies:
Select the /target-uri - Forward Cookies rule.
Select the /target-uri - Private cookies cache setting.
The rule forwards the origin’s Set-Cookie header and applies the cache setting. A rule change takes a few minutes to propagate. The same rule can also carry the Bypass Cache behavior.
Cache a set of paths only for visitors without a session
A rule can hold several criteria. In a group, each criterion after the first joins the one before it with And or Or, and groups join with And, so a request matches only when it meets every group. The rule in this section joins its two paths with Or in one group and checks the cookie in a second group. It caches two catalog paths only for requests that carry no session cookie, so a signed-in visitor never receives a shared copy. The example uses /products/ and /category/ for the paths and session_id for the cookie the origin sets when a session starts. Replace each one with your own.
The cache setting keeps a page for 600 seconds on Azion and overrides the browser cache to 0 seconds, because a copy in the browser cannot be purged.
Console
Create a cache setting named storefront-catalog, as Set the cache TTL for a path shows, with a browser maximum age of 0 and a Max Age of 600. To create the rule that applies it:
Access Azion Console > Applications > your application, then go to the Rules Engine tab.
Enter storefront - catalog cache.
In the Criteria section, set the first criterion to ${uri} starts with /products/. Add a second criterion joined by Or: ${uri} starts with /category/.
Add a second criteria group with one criterion: ${cookie_session_id} does not exist.
Catalog and product pages requested without a session cookie are cached for 600 seconds, and browsers revalidate them on every visit. A new rule takes a few minutes to propagate.
The Build e-commerce storefronts use case uses the values of this example.
Bypass the cache for several paths and a session cookie
One rule can send every per-visitor request to the origin. The rule in this section matches the cart, checkout, and account paths, and any request that carries the session cookie, so a visitor with a session bypasses the cache on every path. It carries Forward Cookies as well, so the session cookie the origin sets reaches the visitor. Nothing this rule matches is cached, so the denylist cache setting of Forward cookies from the origin to the user is not needed here. The example uses /cart, /checkout, /account, and session_id. Replace each one with your own.
Console
To create the bypass rule:
Access Azion Console > Applications > your application, then go to the Rules Engine tab.
Enter storefront - bypass per-visitor pages.
In the Criteria section, set the first criterion to ${uri} starts with /cart. Add two criteria joined by Or: ${uri} starts with /checkout, and ${uri} starts with /account.
Add a fourth criterion joined by Or: ${cookie_session_id} exists.
Cart, checkout, and account requests, and every request with a session, reach the origin, and Azion stores none of their responses. A new rule takes a few minutes to propagate.
To confirm both rules, request a catalog page twice without a cookie, then the cart, then a catalog page with the session cookie, each with the Pragma: azion-debug-cache header:
The second catalog request carries x-cache: HIT. The cart and the request with the cookie carry x-cache: BYPASS. For how to read the header, refer to Check the cache status of a response.
The Build e-commerce storefronts use case uses the values of this example.
These checks confirm the Build e-commerce storefronts use case.