Upcoming SlideShare
Loading in...5








Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

sergaerwga sergaerwga Presentation Transcript

  • Microsoft ASP.NET: An Overview of Caching Holly Mazerolle Developer Support Engineer Microsoft Developer Support Microsoft Corporation
  • Overview
    • Introduction to ASP.NET caching
    • Output caching
    • Fragment caching
    • Data caching
  • Introduction to Caching in ASP.NET
    • Caching is the most critical factor in creating scalable, high performance Web applications
    • Caching locations
      • Web server, proxy server, and client browsers
    • Types of caching
      • Output caching
      • Fragment caching
      • Data caching
    View slide
  • Output Caching
    • What is output caching?
    • @ OutputCache directive and the cache object
    • Output caching attributes:
      • Duration
      • Location
      • VaryByParam
      • VaryByHeader
      • VaryByCustom
    View slide
  • What Is Output Caching?
    • Pages that use the output cache are executed one time, and the page results are cached
    • The pre-executed page is then served to later requests
    • Performance and scalability both benefit
      • Server response times reduced
      • CPU load reduced
    • Appropriate caching of pages affects site performance dramatically
  • @ OutputCache Directive and the Cache Object
    • @ OutputCache declaratively controls caching behavior
      • For .aspx, .asmx, or .ascx
    • The cache object programmatically controls caching behavior
    <%@ OutputCache Duration=&quot;600“ Location=&quot;Any“ VaryByParm=“none” %> Is equivalent to: [C#] Response.Cache.SetExpires(DateTime.Now.AddSeconds(600)); Response.Cache.SetCacheability(HttpCacheability.Public);
  • OutputCache Members: Duration and Location
    • Duration sets the time to cache the output
      • In seconds
      • Required
    • Location sets the location to cache the output
      • Server : The output is held in memory on the Web server and is used to satisfy requests
      • Downstream : A header is added to the response to indicate to proxy servers to cache the page
      • Client : A header is added to the response indicating to browsers to cache the page
      • Any : Output cache can be located on any of these locations
      • None : No output caching is turned on for the item
    <%@ OutputCache Duration=&quot;600&quot; Location=&quot;Any“ VaryByParam=“none” %>
  • OutputCache Members: VaryByParam and VaryByHeader
    • VaryByParam
      • The cache stores multiple copies of a page based on specific Querystring or Form parameters and any combinations thereof
    • VaryByHeader
      • The cache stores multiple copies of a page based on HTTP headers
    <%@ OutputCache Duration=&quot;10“ VaryByParam=&quot;location;count&quot; %> <%@ OutputCache Duration=&quot;60“ VaryByHeader=&quot;Accept-Language&quot; %>
  • OutputCache Members: VaryByCustom
    • VaryByCustom
      • If the value is “Browser,” cache varies by browser type and major version
      • If the value is a custom string, you must override HttpApplication.GetVaryByCustomString in the Global.asax and implement your own caching logic
  • Fragment Caching
    • What is fragment caching?
    • VaryByControl
    • Nested cached user controls
    • Cached controls are not programmable
  • What Is Fragment Caching?
    • Just as you can vary the versions of a page that are output cached, you can output cache regions of a page
    • Regions are defined based on user controls
    • User controls contain their own @OutputCache directive
    • Fragment caching supports
      • VaryByParam
      • VaryByControl
    • Location not supported because fragments must reside on server to be assembled
  • Fragment Caching a User Control
    • [*.ascx]
    • <%@ Language=&quot;C#&quot; %>
    • <%@ OutputCache Duration=&quot;10“ VaryByControl=&quot;State;Country&quot;
    • VaryByParam=&quot;*&quot;%>
    • <script runat=server>
    • public String State {
    • get { return state.Value; }
    • set { state.Value = State; } }
    • public String Country {
    • get { return country.Value; }
    • set { country.Value = Country; } }
    • </script>
  • VaryByControl
    • VaryByControl
      • The sixth attribute supported by OutputCache
      • Only supported in user control caching
      • Caching is based on user control properties
    <%@ OutputCache Duration=&quot;10“ VaryByControl=&quot;State;Country“ VaryByParam=&quot;*&quot;%>
  • Nested Cached User Controls
    • User controls can be nested
    • Cached user controls can also be nested
    • Since multiple versions of a user control are cached, this creates a hierarchy of cached output
    • This caching tree
      • Is very efficient and powerful
      • Requires almost nothing from a developer
    • Varying by many properties could create a large tree, and therefore, a large memory footprint
  • Cached Controls Are Not Programmable
    • Do not try to programmatically access a user control that is in the cache
    • Cached controls are not added to the control tree
    • Trying to access a cached control will throw an exception
  • Data Caching
    • What is data caching?
    • Working with the cache object
    • Cache dependencies
    • Scavenging memory
    • Using callbacks with caching
  • What Is Data Caching?
    • The data cache holds application data such as strings, datasets, and other objects
    • Adding items to the data cache is easy
    • Although similar to the familiar application variables model, it is much more powerful
    Cache (“counter”) = mycount.text Application(“counter”) = mycount.text
  • Working with the Cache Object
    • Cache object features
      • Dependencies allow logic to invalidate cached items
      • Scavenging (automatic expiration)
      • Callbacks when an item is removed from cache
    • To use dependencies or callbacks, use Cache.Insert or Cache.Add
    • Code using cached items must be able to both create or insert, and retrieve cached items
    Public Function GetProductData() As DataSet If (IsNothing(Cache(&quot;ProductData&quot;)) Then Cache(&quot;ProductData&quot;) = LoadDataSet()   Return Cache(&quot;ProductData&quot;) End Function
  • Cache Dependencies
    • File-based dependencies
      • Cached item invalidated when files change
    • Key-based dependencies
      • Cached item invalided when another cached item changes
    • Time-based dependencies
      • Absolute time-based invalidations
      • Sliding time-based invalidations
  • Scavenging Memory
    • Automatic system that initiates when memory becomes scarce
    • Tag cached items with relative importance
      • CacheItemPriority
      • CacheItemPriorityDecay
    • Items must be added to cache using .Add or .Insert to make use of these enumerations
  • Using Callbacks with Caching
    • Create Callbacks with this delegate:
    • CacheItemRemovedCallback
    • Callbacks are used to receive notification when an item is evicted from the cache
      • Cleanup
      • Update
      • Recreate
      • Logging
    • Items must be added to caching using .Add or .Insert to use callback functionality
  • Review
    • Code that uses items from the data cache must be able to create the object, load the cache, or retrieve the object before using it.
    • Caching can hide problems. Test without caching.
    • Use tracing, performance counters, and stress software to identify caching wins.
  • Additional Information about Caching
    • INFO: ASP.NET Caching Overview
    • HOW TO: Control Page Output Caching in ASP.NET by Using Visual Basic .NET
    • HOW TO: Perform Fragment Caching in ASP.NET by Using Visual Basic .NET
    • Thank you for joining today’s Microsoft Support
    • WebCast.
    • For information about all upcoming Support WebCasts,
    • and access to the archived content (streaming media
    • files, PowerPoint ® slides, and transcripts), please visit:
    • http:// /
    • Your feedback is sincerely appreciated. Please send any
    • comments or suggestions about the Support
    • WebCasts to [email_address] .