Which content gets cached; how do I define cache rules?
Short answer
GET and HEAD requests (Ignore HTTP Method Standards) that meet the Caching Rules condition in the Cache profile are cached if the Backend Service allows the response to be cached (Ignore Response Headers) and the response does not exceed the Max. Object Size (MB) limit. The default rule covers requests whose path ends with common static file extensions (such as .css, .png, .jpg, .woff); .js, .html and .json are not in this list. The rule is changed by selecting conditions (Conditions) in the Caching rules field of the profile and combining them with and/or; a rule without a condition does not cache any content.
Problem: The Content to Be Cached Varies by Application
Which content can be safely cached differs for each application: on one site, JavaScript files are also wanted in the cache, while user-specific pages must stay out. The extension list of the default rule:
.txt .bmp .jpeg .pdf .ps .ttf .jpg .svg .webp .css .svgz .woff .csv .gif .mid .png .swf .woff2 .doc .midi .ppt .tif .xls .docx .pptx .tiff .xlsx .mp4 .flv .mov .wmv .avi .ogg .mkv .mpg
Solution: Two Methods
| Your situation | Suitable method |
|---|---|
| Changing the rule from the interface | Method 1: Caching rules field |
| From the command line | Method 2: WEB-CLI |
Method 1: Caching Rules Field Recommended
- How it works When the rule conditions are met, the request is served from the cache or the response is written to the cache.
- Where it is openedLoad Balancer → Profiles → Cache → Caching Rules
- Requirement The condition to be used in the rule; if it is not in the list, it is created first
- vService type HTTP
Step-by-Step Configuration
If the condition to be used in the rule does not exist, it is created with Load Balancer → Conditions → Add; e.g. Criteria = Path, End in the match selection, value .js → Add (adding a condition).
On the Load Balancer → Profiles → Cache screen, Add is used for a new profile (how to add), and right-click on the row → Edit for an existing profile.
In the Caching Rules field, the .js condition is added next to the default extension condition with or. At most 3 groups are added with +; the groups and the conditions within a group are combined with and/or; the negated forms of the conditions are also in the list (automatic NOT condition). The list contains only request-stage conditions.
Add is clicked for a new profile, Save when editing. If the profile is also used in other vServices, the change is reflected in all of them; the Used in tab in the preview is checked beforehand.
On the Load Balancer → Load Balancer vService List screen, the HTTP vService is selected and Edit is clicked; in the Details panel, the Cache box is checked and the profile is selected (how to add to a vService).
Save is clicked. The change is applied without dropping open connections; the copies in the cache are deleted (recommended values). In a cluster, it is also sent to the peer device with the default auto-sync; if Manual Sync Mode is on, it is sent with Synchronize.
Verification
The same address is requested twice with curl -s -o /dev/null -D - https://www.example.com/app.js.
TRCACHE: MISS, then TRCACHE: HITOn the HTTP Frontend tab of the vService dashboard, the Frontend Cache graph is checked. If Cache Lookups increases but Cache Hits does not, the rule matches but the response is not stored (method, response headers or size).
Cache Hits increasesMethod 2: WEB-CLI
- How it works The condition and the rule are written from the command line; the rule takes an expression that combines the conditions by their IDs.
- Where it is opened TR7 WEB-CLI →
lb condition·lb cache·lb service - Requirement WEB-CLI access;
--cacheRules.N.idis required for each rule - vService type HTTP
Step-by-Step Configuration
lb condition add criteria path query .js --content_placement end --caseInsensitive true --name js-dosyalari--cacheRules.N.cond takes the ID of the condition, not its name. The ID is read from the list:
lb condition listThe ID of the default extension condition is isCommonFileExtension; the .js condition is added to it with or:
lb cache add name statik-js --cacheRules.0.id statik --cacheRules.0.cond "isCommonFileExtension or <js-condition-id>"lb service update id <vService> --cache statik-jsVerification
The same address is requested twice with curl -s -o /dev/null -D - https://www.example.com/app.js.
TRCACHE: MISS, then TRCACHE: HITCommon Mistakes
- Removing the condition of the rule. With a rule without a condition, no content is cached; the interface does not give a warning in this case.
- Assuming that
.jsand.htmlfiles are cached by default. These extensions are not in the default list; if needed, they are added with a condition. - Trying to build a rule with response conditions. Response-stage conditions such as the status code or the response size cannot be selected in Caching rules.
- Turning on the Ignore options for the hit ratio. When Ignore Response Headers is turned on, the headers of the Backend Service that prevent caching are not taken into account.
- Writing a broad rule that covers user-specific pages. For keeping personal content out: How do I prevent user-specific content from being cached by mistake?
How it is done in the documentation
