27
PACS – 06/21/14 1 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.

Cache?

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

PACS – 06/21/14 1

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 2

Cache Examples

“The Zoo Photographer” exampleCPU CachingDisk CachingDNS CachingWeb Caching

PACS – 06/21/14 3

CPU Cache

PACS – 06/21/14 4

CPU Cache

LevelAccess Typical

Technology

Managed

Time Size By

Registers 1-3 ns ?1 KB Custom CMOS Compiler

Level 1 Cache (on-chip)

2-8 ns 8 KB-128 KB SRAM Hardware

Level 2 Cache (off-chip)

5-12 ns 0.5 MB - 8 MB SRAM Hardware

Main Memory 10-60 ns 64 MB - 1 GB DRAM Operating

System

Hard Disk3,000,000 -

20 - 100 GB Magnetic Operating System/User10,000,000 ns

PACS – 06/21/14 5

CPU Cache

PACS – 06/21/14 6

CPU Cache

PACS – 06/21/14 7

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 neededUtilizes solid-state NAND flash and hard drive capacity for unmatched data integrity and reliability

PACS – 06/21/14 8

Disk Cache

PACS – 06/21/14 9

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 10

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 11

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 12

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 13

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.

Web Caching

PACS – 06/21/14 14

Web Caching

PACS – 06/21/14 15

Web Caching

PACS – 06/21/14 16

Web Caching

PACS – 06/21/14 17

PACS – 06/21/14 18

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 setA 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 19

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 20

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.

Web Caching

• Using Expires HeadersExpiresActive OnExpiresDefault 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 21

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 22

PACS – 06/21/14 23

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 OKDate: Fri, 30 Oct 1998 13:19:41 GMTServer: Apache/1.3.3 (Unix)Cache-Control: max-age=3600, must-revalidateExpires: Fri, 30 Oct 1998 14:19:41 GMTLast-Modified: Mon, 29 Jun 1998 02:28:12 GMTETag: "3e86-410-3596fbbc"Content-Length: 1040Content-Type: text/html

Web Caching

In .htaccess:

<IfModule mod_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 24

Web Caching

In your PHP script:

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past to force a new request

ORif ($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 25

PACS – 06/21/14 26

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.