270 likes | 442 Views
Cache?. What is caching? A way to increase the average rate of a process by preferentially using a copy of data in a faster, closer, probably more expensive location instead of the data’s original, more distant, slower, less expensive location.
E N D
Cache? • What is caching? • A way to increase the average rate of a process by preferentially using a copy of data in a faster, closer, probably more expensive location instead of the data’s original, more distant, slower, less expensive location. • A way to determine whether the cached copy is valid is important. PACS – 06/21/14
Cache Examples • “The Zoo Photographer” example • CPU Caching • Disk Caching • DNS Caching • Web Caching PACS – 06/21/14
CPU Cache PACS – 06/21/14
CPU Cache PACS – 06/21/14
CPU Cache PACS – 06/21/14
CPU Cache PACS – 06/21/14
Disk Caching • Traditional Disk Cache is actually a disk buffer. It marries the faster bus timing with the slower rotating disk timing. • Seagate Laptop and Ultra Mobile SSHDs integrate NAND flash with traditional hard drive storage to create a hybrid drive with the speed of solid state and the capacity of a hard drive. • Boots Windows 7 up to 35% faster. Programs launch faster. • Installs and works like a typical hard drive – no special device drivers needed • Utilizes solid-state NAND flash and hard drive capacity for unmatched data integrity and reliability PACS – 06/21/14
Disk Cache PACS – 06/21/14
DNS Caching First request for a domain name: Each DNS server saves the results of each request along with an expiration time (TTL). PACS – 06/21/14
DNS Cache • Second request for the same domain name: If the cached value is valid, IP can be returned without further lookups. • Clients also normally cache DNS results. In that case, no request to a DNS server is needed. PACS – 06/21/14
Web Caching • Why worry about speed? Users hate waiting! • The browser save copies of images, stylesheets, javascript or the HTML. The next time the user needs that same resource for the same or a different web page the browser doesn’t have to download the file again. Fewer downloads means a faster, happier site. • All browsers have cache areas available with sizes that can be controlled. • There has to be a mechanism to allow for changed files on the server. • Proxy servers will usually cache files to improve performance for their respective users. PACS – 06/21/14
Web Caching • Browsers use multiple methods of accessing a cached file depending on what the server supplied when the file was initially downloaded: • Last-Modified – asks the server to decide based on the date of the browser’s copy. • If-None-Match – asks the server to decide based on a unique characteristic of the file, like a hash. • Expires – the browser will not ask the server if time is prior to the expiration time of the cached file. • Max-age – like Expires but the server gives the browser a relative time rather than an absolute time when the file was first served. • Different browsers may use any or all of the above approaches. PACS – 06/21/14
Web Caching Other cache related headers: • Cache-control: public means the cached version can be saved by proxies and other intermediate servers, where everyone can see it. • Cache-control: private means the file is different for different users (such as their personal homepage). The user’s private browser can cache it, but not public proxies. • Cache-control: no-cache means the file should not be cached. This is useful for things like search results where the URL appears the same but the content may change. PACS – 06/21/14
Web Caching PACS – 06/21/14
Web Caching PACS – 06/21/14
Web Caching PACS – 06/21/14
Web Caching PACS – 06/21/14
Web Caching • Apache Server Caching Guide: http://httpd.apache.org/docs/2.2/caching.html • Apache Caching Header Guide: http://httpd.apache.org/docs/2.2/mod/mod_expires.html • The general format for setting headers is File types to match Header / Expiration to set • A general tip: the less a resource changes (images, pdfs, etc.) the longer you should cache it. If it never changes (every version has a different URL) then cache it for as long as you can (i.e. a year)! PACS – 06/21/14
Web Caching • If the response’s headers tell the cache not to keep it, it won’t. • If the request is authenticated or secure (i.e., HTTPS), it won’t be cached. • A cached representation is considered fresh (that is, able to be sent to a client without checking with the origin server) if: • It has an expiry time or other age-controlling header set, and is still within the fresh period, or • If the cache has seen the representation recently, and it was modified relatively long ago. • Fresh representations are served directly from the cache, without checking with the origin server. PACS – 06/21/14
Web Caching • If a representation is stale, the origin server will be asked to validate it, or tell the cache whether the copy that it has is still good. • Under certain circumstances — for example, when it’s disconnected from a network — a cache can serve stale responses without checking with the origin server. • If no validator (an ETag or Last-Modified header) is present on a response, and it doesn't have any explicit freshness information, it will usually - but not always - be considered uncacheable. PACS – 06/21/14
Web Caching Using Expires Headers ExpiresActive On ExpiresDefault A0 # 1 YEAR - doesn't change often <FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$"> ExpiresDefault A31536000 </FilesMatch> # 1 WEEK - possible to be changed, unlikely <FilesMatch "\.(jpg|jpeg|png|gif|swf)$"> ExpiresDefault A604800 </FilesMatch> # 3 HOUR - core content, changes quickly <FilesMatch "\.(txt|xml|js|css)$"> ExpiresDefault A10800 </FilesMatch> “A” means “Access plus…”; “M” means “Modification plus…” PACS – 06/21/14
Web Caching Using max-age headers: # 1 YEAR <FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$"> Header set Cache-Control "max-age=31536000, public" </FilesMatch> # 1 WEEK <FilesMatch "\.(jpg|jpeg|png|gif|swf)$"> Header set Cache-Control "max-age=604800, public" </FilesMatch> # 3 HOUR <FilesMatch "\.(txt|xml|js|css)$"> Header set Cache-Control "max-age=10800" </FilesMatch> # NEVER CACHE - notice the extra directives <FilesMatch "\.(html|htm|php|cgi|pl)$"> Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate" </FilesMatch> PACS – 06/21/14
Web Caching • Where do the cache related headers get set? • Meta tags in HTML documents (Pragma: no-cache) are not effective. They’re only honored by few browsers and never by proxy servers. • They should be set in HTTP headers. • A typical complete set of headers might be: HTTP/1.1 200 OK Date: 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 PACS – 06/21/14
Web Caching In .htaccess: <IfModulemod_expires.c> <FilesMatch "\.(css|css\.php|js|gif|jpeg|jpg|png|mp3|pdf|xml\.php)$"> ExpiresActive On ExpiresDefault "access plus 1 year" Header append Cache-Control "must-revalidate, public" </FilesMatch> </IfModule> PACS – 06/21/14
Web Caching In your PHP script: header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past to force a new request OR if ($page_cache) { header("Expires: ".gmdate("D, d M Y H:i:s", $pltime + $page_cache)." GMT"); header("Cache-Control: must-revalidate, public"); header("Last-Modified: ".gmdate("D, d M Y H:i:s", filemtime(__FILE__))." GMT"); } // $page_cache PACS – 06/21/14
Web Caching • Examine your site in Redbot.org to see what cache headers are being sent. • Use Firebug (or Live HTTP Headers addon) with Firefox to see requests and responses to page accesses. • Ctrl-F5 will tell the IE & FF browsers to ignore any cached files and request a fresh copy of the page. PACS – 06/21/14
Web Caching • Caching Tutorial for Web Authors and Webmasters: https://www.mnot.net/cache_docs/ • How To Optimize Your Site With HTTP Caching: http://betterexplained.com/articles/how-to-optimize-your-site-with-http-caching/ PACS – 06/21/14