Kinds of Web Caches
- browser caches
- proxy caches
- gateway caches
how to control caches
- html meta tags -> easy to use , but aren’t very effective . only honored by a few browser caches , not proxy caches
- http headers -> give you a lot of control over how both browser caches and proxies handle your representations .
HTTP/1.1 200 OKDate: Fri, 30 Oct 1998 13:19:41 GMT Server: Apache/1.3.3 (Unix) Cache-Control: max-age=3600, must-revalidate Expires: Fri, 30 Oct 1998 14:19:41 GMT Last-Modified: Mon, 29 Jun 1998 02:28:12 GMT ETag: "3e86-410-3596fbbc" Content-Length: 1040 Content-Type: text/html
Controlling Freshness with the Expires HTTP Header
- Expires -> it has some limitations , because there’s a date involved , clocks must be synchronized
- easy to forget that you’ve set some content to expire at a particular time
Cache-Control HTTP Headers
Useful Cache-Control response headers include:
- max-age = [seconds]
- s-maxage = [seconds]
Tips for Building a Cache-Aware Site
- Use URLS consistently
- Use common library of images and other elements
- Make caches store images and pages that don’t change often by using Cache-Control:max-age header with a large value
- Make caches recognize regularly
- If a resource changes , change its name
- don’t change files unnecessarily
- Use cookies only where necessary
- Minimize use of SSL
Writing Cache-Aware Scripts
- only write file that have changed
- set an age-related header for as far in the future as practical -> max-age
- parsing the HTTP headers : If-Modified-Since , 304 Not Modified
- don’t use POST unless it’s appropriate .
- don’t embed user-specific information in the URL unless the content is unique to that user
- Don’t count on all request from a user coming from the same host
- Generate Content-Length response headers
沒有留言:
張貼留言