简体   繁体   中英

Does enabling Cache affect dynamic content?

Does enabling cache affect dynamic content?

For example, on one of my PHP sites, the cache header is set to:

Cache-Control: public, max-age=21600

Does that affect dynamic content as well?

If so, then what is the standard way to cache a dynamic PHP site? Presumably, you would cache static content (eg images), but not dynamic content (eg html, text, etc). How and where would you accomplish that?

Yes. The answer is yes. This controls browser and some ISP caching. It will cache dynamic content for the time you specify in many locations. Not all. Not all browsers will follow the rules, not all ISPs will follow the rules. Famously, AOL used to disregard people's cache rules and cache everything for strange times leading to broken pages on the early web.

On a dynamic page that isn't cached, you can use this value to set pages that change relatively in-frequently to cache for 10 minutes to an hour. For images, css, js files and things for longer. Caching for 8 hours is probably too much, as it is much longer than 1 hour, but only reduces the total number of hits for the content by 7.

Other Caching

There are other, probably more reliable, ways to cache content. You can look into query caching, file caching, memcached. All of these options can also be used to cache things other than content as well. They will all help you speed up repetitive actions.

Query Caching

Many databases, MySQL being the defacto standard, offer query caching. This will cache the results of queries on tables that haven't been updated since the last time the query was run. Perfect for normalized look-up tables. Ideal for tables that are updated only once in a blue moon. Works well for tables that are updated about once and hour to even every 10 minutes. For tables that change more often than that, they will produce limited time-saving results.

File Caching and Memcached

These can be used to cache key/value pairs of information. They can be page_url/page_content or page_list/array_of_pages_in_site or any other key/value pairs you need. This is how most people go about caching pages for 10-60 minutes these days. They are reliable, controlled on the server, and can be flushed instantly if needed. They don't need to be time based, if your logic is written correctly you can treat these like the query cache. Only when information is updated do you flush that key from the cache, and then the page updates instantly and otherwise sits in cache.

Header Cache

Which brings us back to header cache. It is still smart to cache here for about 10 minutes even with the other caches in place. The other caches still require requests of the server, which can slow it down. While this won't reduce that pressure by much, it will reduce it. And it doesn't take much effort to install.

The idea is your website is made up of resources [pages, images, scripts even], and you provide an expire limit for everyone of them, or invalidate cache for elements you modified [eg you added a new post on the homepage, or you edited an entry].

A common solution is to use a reverse proxy like Varnish who will provide cached stuff very fast to the client, and will look for newer versions if cache header changed in your content.

The caching header generator process is up to you - you can find some ideas here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM