Published on

lkjshdfkjshfsdkljhsdkljghsd skdjfhskdfj sd

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


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