Top Banner
Caching Caching MacDonald Ch. 26 MacDonald Ch. 26 MIS 424 Professor Sandvig
20

Caching MacDonald Ch. 26

Feb 22, 2016

Download

Documents

Media

Caching MacDonald Ch. 26. MIS 424 Professor Sandvig. Overview. What is Caching Where to cache When to cache Data caching in ASP.NET Page caching Page Fragment caching Cache Class Create you own cache. What is Caching. Cache = “secret store” Armies: cache weapons - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Caching MacDonald Ch. 26

CachingCachingMacDonald Ch. 26MacDonald Ch. 26

MIS 424Professor Sandvig

Page 2: Caching MacDonald Ch. 26

OverviewOverview

What is CachingWhat is CachingWhere to cacheWhere to cacheWhen to cacheWhen to cacheData caching in ASP.NETData caching in ASP.NET

1.1. Page cachingPage caching2.2. Page Fragment cachingPage Fragment caching3.3. Cache ClassCache Class

Create you own cacheCreate you own cache

Page 3: Caching MacDonald Ch. 26

What is CachingWhat is Caching

Cache = “store”Cache = “store”– Armies: cache weaponsArmies: cache weapons– Squirrels: cache nutsSquirrels: cache nuts– Computers: cache dataComputers: cache data

Benefits of caching data:Benefits of caching data:– ReuseReuse

Process once, reuse many timesProcess once, reuse many timesFaster, cheaperFaster, cheaper

Page 4: Caching MacDonald Ch. 26

Where to CacheWhere to Cache

Client-sideClient-side– BrowsersBrowsers

ImagesImagesPagesPages

– Reduces download timesReduces download times

Server-sideServer-side– Reduces server processing loadReduces server processing load

Page 5: Caching MacDonald Ch. 26

When to Cache on serverWhen to Cache on server

Frequently hit pagesFrequently hit pages– Content changes infrequentlyContent changes infrequently

NYTimes.comNYTimes.comESPN.comESPN.comCMS (see Drupal CMS (see Drupal Boost))

Data is expensive to retrieveData is expensive to retrieve– MIS 424MIS 424

Weather forecastWeather forecastAmazon Best SellersAmazon Best Sellers

Page 6: Caching MacDonald Ch. 26

When NOT to cacheWhen NOT to cache

Pages that change frequentlyPages that change frequently– Shopping cartShopping cart– Amazon.comAmazon.com

Customized for each userCustomized for each user

Infrequently accessed dataInfrequently accessed data– Cache must be stored Cache must be stored Overhead Overhead– cost depends upon caching methodcost depends upon caching method

Page 7: Caching MacDonald Ch. 26

Caching Tools in ASP.NETCaching Tools in ASP.NET

Caching OptionsCaching Options1.1. Page Page 2.2. Page fragmentPage fragment3.3. Data classData class4.4. Create your ownCreate your own

DatabaseDatabase Application or session objectApplication or session object

Page 8: Caching MacDonald Ch. 26

Page CachingPage Caching

Caches entire pageCaches entire pagePage directive:Page directive:

<%@ Page OutputCache Duration=“300” VaryByParm=“none” %><%@ Page OutputCache Duration=“300” VaryByParm=“none” %>

– Duration in secondsDuration in seconds

Stored on diskStored on disk– Low overheadLow overhead

Page 9: Caching MacDonald Ch. 26

Page Caching - parametersPage Caching - parameters

VaryByParm=VaryByParm=– ““None”None”

Single cache for all parameter combinationsSingle cache for all parameter combinations

– ““*”*”Different cache for each parameter combinationDifferent cache for each parameter combination

– ““ParameterList”ParameterList”Different cache for listed parametersDifferent cache for listed parametersOne cache for other parametersOne cache for other parameters

Page 10: Caching MacDonald Ch. 26

Page Caching - parametersPage Caching - parameters

Example: Example: – 14_CachePage.aspx 14_CachePage.aspx source, output, output

Location:Location:– ServerServer– ClientClient– Downstream – proxy serversDownstream – proxy servers– AnyAny– NoneNone

Page 11: Caching MacDonald Ch. 26

Page Caching - parametersPage Caching - parameters

Also may specify cache for header Also may specify cache for header attributes:attributes:

– LanguageLanguage– Browser versionBrowser version– Etc.Etc.

Page 12: Caching MacDonald Ch. 26

ASP.NET CachingASP.NET Caching

1.1. Page Page 2.2. Page fragmentPage fragment3.3. Data classData class4.4. Create your ownCreate your own

Page 13: Caching MacDonald Ch. 26

Page Fragment CachingPage Fragment Caching

Cache part of a pageCache part of a page– Content in user controlContent in user control

Syntax similar to Page cacheSyntax similar to Page cacheExample:Example:– Music Store menuMusic Store menu

Page 14: Caching MacDonald Ch. 26

Page Fragment CachingPage Fragment Caching

Benefits:Benefits:– Useful for menus, headers, footers…Useful for menus, headers, footers…– Easy to implementEasy to implement– Stored on diskStored on disk

WeaknessesWeaknesses– OnClick events frozenOnClick events frozen– Timeout fixed duration onlyTimeout fixed duration only

Page 15: Caching MacDonald Ch. 26

ASP.NET CachingASP.NET Caching

1.1. Page Page 2.2. Page fragmentPage fragment3.3. Cache classCache class4.4. Create your ownCreate your own

Page 16: Caching MacDonald Ch. 26

Cache ClassCache Class

Cached data accessible throughout Cached data accessible throughout applicationapplication– Use data on multiple pageUse data on multiple page

Cache any type of data objectCache any type of data object– strings, datasets, dataviews, …strings, datasets, dataviews, …

Set expiration dateSet expiration date– Several optionsSeveral options

Page 17: Caching MacDonald Ch. 26

Cache ClassCache Class

Cache.Insert methodCache.Insert method

Parameters for:Parameters for:– Expiration date – absolute & slidingExpiration date – absolute & sliding– File dependenciesFile dependencies

Database tablesDatabase tables

– Subroutine to run when expiresSubroutine to run when expires

DocumentationDocumentation

Page 18: Caching MacDonald Ch. 26

Cache Class - SyntaxCache Class - Syntax

Cache.Add(Key, Object, CacheDependency, Cache.Add(Key, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) CacheItemRemovedCallback)

Example: Example: – CacheExample.aspx source, outputCacheExample.aspx source, output

Page 19: Caching MacDonald Ch. 26

Create Your OwnCreate Your Own

Program own rulesProgram own rulesCustomize storage locationCustomize storage location– DatabaseDatabase– FileFile– Combination of Cache locationsCombination of Cache locationsExpiration ruleExpiration rule– Fixed time of day, week, month…Fixed time of day, week, month…– Number of hits…Number of hits…Example: Weather forecastExample: Weather forecast

Page 20: Caching MacDonald Ch. 26

SummarySummary

Several Caching options .NETSeveral Caching options .NET– PagePage– Page fragmentPage fragment– Cache objectCache object– Roll your ownRoll your own

Dramatic performance improvements Dramatic performance improvements when data expensive to retrievewhen data expensive to retrieve