# Cache rules

Use cache rules to decide what is cached and for how long; cache groups and per-site changes, and how to raise the hit ratio.

Cache rules decide what a node keeps locally and for how long. The more complete the rules, the fewer requests go to the origin.


## Cache groups
Every site is attached to a cache group, and its rules follow the group:
- Switch group in the site's "Cache groups" drop-down; the site's rules are replaced with that group's.
- Change individual rules directly in the rule table below. The change applies only to this site and shows as "N changed for this site"; click "Restore all" to go back to the group's rules.
- Platform groups are maintained by the administrator in the "Cache groups" card under Global Settings › Default Settings. Tenants can only view them or "Copy as my own"; their own groups can be edited and deleted.
- Tenants pick their default group under Default Settings › Cache, and new sites use it by default. "Reset all to default" moves all sites back to the default group and clears each site's own changes.

## Writing the rule table
Rules match by file extension, directory or full path, take effect from top to bottom, and the whole table is saved together. Each rule can set:
- **Match type and value**: extension, directory or full path, for example extension `jpg`, directory `/static/`.
- **Cache time**: how long nodes keep it, in seconds, minutes, hours or days; there is also a browser cache time that tells the visitor's browser how long to keep it.
- **Ignore parameters**: no, all parameters, or specific ones. When ignored, the same file with different `?` parameters is treated as one cache entry.

:::tip
Write one long-cache rule by extension for static resources (images, scripts, styles). Do not cache dynamic pages or paths with login state or personalized content.
:::

## Raise the hit ratio
Check the request and byte hit ratios in Statistics. A low ratio usually has two causes:
1. The origin sends "do not cache" headers (Cache-Control: no-cache and so on): choose to ignore the origin's cache headers in the rule, or have the origin change its headers.
2. The same file is treated as different files because of different `?` parameters: turn on ignore parameters for static resources.

:::warn
Ignoring the origin's cache headers makes nodes disregard headers such as Set-Cookie. Use it only on paths that surely have no personalized content.
:::

After changing rules, old cached content only updates immediately after [purge and prefetch](https://www.gocdn888.com/en/guide/purge).