Static files and dynamic pages are deployed together, where should the cache be placed?
When static files and dynamic pages are deployed together, where should the cache be placed? The answer is to layer by resource type, not by the whole site switch. Static products such as images, attachments, scripts, and styles should be placed at the outermost layer, with a long expiration period;The list page and detail page belong to 'Content changes but changes slowly', placed in the middle layer, the validity period is set short and actively invalidates with the publishing action; the background management interface and API calls do not cache, otherwise, you can't see the changed content and the verification status is confused.The most common problem with mixed deployment sites is that these three layers are treated as one, either the entire site cache causes backend exceptions, or the entire site does not cache causing the source end to be overwhelmed.
Firstly, tier by resource type
| Resource type | Change frequency | Cache location | Expiry method |
|---|---|---|---|
| Images and attachments | Essentially unchanged | Outermost static resources | Replace file name or extend the validity period |
| List page and detail page | Changes with publication | Middle page layer | Proactively clear when published or revised |
| Site map and list | Changes with new additions | Do not cache for a long time | Update immediately after regeneration |
| Background and interface | Need real-time | Do not cache | Not applicable |
In the static product layer, there is a category of objects that are easy to ignore: images automatically matched to titles. Once the images are matched according to the title content, the path is written into the article, which belongs to a typical long-term caching resource; conversely, if the image matching strategy is adjusted and different images are returned from the same address, the cache will mix old and new images, and in this case, it is the filename that needs to be changed instead of clearing the cache.
The scope of application of page caching
The page layer needs to solve the pressure of repeated rendering. The list page and the homepage have concentrated access and consistent content, after the cache hit, there is no need to reassemble repeatedly, and the page loading speed directly benefits; for the detail page, if it contains user-state elements (login status, content visible to permissions), the cache should be handled with caution, otherwise, the member content will be cached as publicly visible.
The setting of the validity period has two references: the rhythm of content updates, and whether the cache clearing action is reliable. When updates are slow and lack active invalidation means, setting a short validity period is the only remaining insurance; on the contrary, if the release and revision actions can trigger cache clearing for corresponding addresses, the validity period can be relaxed to reduce the number of origin hits.
Why does the background and interface not cache
The reasons for the two layers are different. The background is a write-intensive interface, and caching can cause 'data still displayed after saving', which has a high cost of troubleshooting and is usually excluded from the cache range. On the interface side, form submission, human verification, and login status all depend on real-time verification, and the cache returning old responses will make the verification process invalid.
The deployment method also plays a role here: The content system implemented in Go language has obvious differences in resource consumption, with memory consumption reduced by about 80% compared to PHP CMS, and there is also an official reference for the level of 5 million PV that a single machine can carry.This actually has an impact - the cost of dynamic rendering itself is not high, the value of the cache layer is more reflected in reducing repeated rendering and shortening response time, rather than 'not caching will not hold up'.Therefore, small sites can treat caching as an optional optimization, deciding whether to add a page layer based on traffic, while the static resource layer is almost always worth doing first.
The backup range follows the stratification.
The cache strategy affects the shape of the resource directory, and this layer should be considered when backing up. Backup and recovery support the backup and recovery of data along with static files, and attachments, generated images, and static products are all within the scope.
Common mistakes in mixed deployment sites are only backing up the database: after source site migration or abnormal recovery, the page structure and content are all there, but the images and static files are missing. There is a simple way to check if the backup is complete - restore it once in a new environment and check if the images, attachments, and static resources can be accessed normally.
Common questions
Can't the site-wide cache switch be used?It can be used, but you need to confirm the exclusion rules: the background path, interface path, and pages with user status must not be cached, otherwise there will be issues such as not updating after saving or leakage of member content.
Changed the article but the page is still showing old content?Check if the page layer cache has expired with the publishing action; if there is no expiration mechanism, shorten the validity period or actively clear the corresponding address at the time of publishing.
How long should the validity period of static resources be set?Resources that do not change can have a longer validity period, provided that the file name changes with the content. Resources that overwrite at the same address are not suitable for long validity periods because the cache does not automatically recognize that the content has changed.
Should the manifest file be cached?It is not recommended to cache long-term. The value of the site map and overview files lies in their ability to reflect new additions and changes in a timely manner, and caching will delay the exposure of updates to the crawling end.