Caching Caching MacDonald Ch. 26 MacDonald Ch. 26 MIS 424 Professor Sandvig
Feb 22, 2016
CachingCachingMacDonald Ch. 26MacDonald Ch. 26
MIS 424Professor Sandvig
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
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
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
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
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
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 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 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 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 Caching - parametersPage Caching - parameters
Also may specify cache for header Also may specify cache for header attributes:attributes:
– LanguageLanguage– Browser versionBrowser version– Etc.Etc.
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 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 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
ASP.NET CachingASP.NET Caching
1.1. Page Page 2.2. Page fragmentPage fragment3.3. Cache classCache class4.4. Create your ownCreate your own
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
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
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
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
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