Web front end caching best practices for site?

元气小坏坏 提交于 2019-12-03 03:29:59
user37078

I don't know everything about caching, but here are some suggestions:

Anon #1,2: (static,semi-dynamic items) You could set them to never expire. If you need to change them, change their URL. If-modified-since checks are cheap but not free.

Anon #3: (dynamic items) Here's where ETags and/or Last-Modified comes in very handy. Depending on what you're serving, you can generate a good Last-Modified header. If your database stores the modified date of all items you were planning to show, you could something to the effect of SELECT MAX(last_updated) FROM items_to_show. Caveat: This takes into account the age of the data, and not the age of your template, so if you've changed your django template, you'd be at a loss as to how to communicate that in the header.

Or you could do something similar with an ETag. It could be a checksum of the contents that are generated. This will take the changing of the template into account.

Something to note with both of these approaches to caching dynamic content is that they really save more bandwidth than they do web server/database load. You can always make judicious use of the Expires header though to help in cases where the changes to a page are periodic and predictable.

My suggestions for the logged in stuff would be similar, except I would look at the Vary header. This can signal to caching proxies that different logged in users will not be served the same content.

In general, I would use either ETag or Last-modified, but not both.

Bittarman

My best answer to this, is that you have plenty of options for all of the static files, which can produce lots of gains in their own way, each beneficial in a specific scenario, so weigh up the pro's and cons according to your specific need.

However, what most people neglect to think about is their dynamic content, sure caching db results and the like are great, but still involve actually starting up the parsing engine of PHP/ASP or whatever.

If you look at the super-cache plugin for wordpress, you will note that it has the ability to actually prepare your html as static files. Not only that, but it also makes a gzip copy, and uses rewrite rules to check for the existence of these files as an appropriate alternative to starting up the parser. This is obviously going to give you the best result, as it is not only saving your processing time, but also bandwidth.

If you want to see the performance disparity, compare the apachebench results of <?php die('hello world'); with serving a static .html page.

Obviously you would need to be careful with this sort of caching, but it can be very useful to replace fullpage caching from inside an interpreter like PHP.

There are some relevant suggestions on the ySlow pages.

Etags might not be a good idea apparently.

MikeJ

I would suggest reading Scalable Internet Architectures There are several chapters devoted to scaling up via caching, CDN etc. It should point you in the right direction to get going. Helped me scale up the site I support immensely.

--

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!