Aspnet Caching


Published on

An Introduction to caching in

  • This is nice slide, very helpful for me. This link...
    also helpful.
    Are you sure you want to  Yes  No
    Your message goes here
  • well explained presentation, there is no doubt that cache is the ultimate solution for the issues like performance and scalability. But before going ahead with it, one has to keep into mind its drawbacks/limitations as well. due to its standalone and in-process nature, it only performance well in smaller web farms. But in a larger web garden, where you have multiple servers, the use of a distributed cache can be a better option. here is a good read about the limitations of cache,
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Aspnet Caching

  1. 1. <ul><li> </li></ul>
  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. Post cache substitution <ul><li>Allows to substitute (Replace/modify) a (Small) portion of a cached page output </li></ul><ul><li>Very useful to use where little content in the rendered page varies depending on parameter(s) </li></ul><ul><li>Use an asp:substitution to implement the Post cache substitute </li></ul><ul><li>Provide a method for the call back, that renders the markup for the substitution. </li></ul>
  15. 15. SQL dependency <ul><li>Output Caching/ Fragment caching could be configured to use SQL dependency so that, cached Page/User control will be updated when corresponding table(s) data is/are updated </li></ul><ul><li>SQL dependency is usable in Both SQL server 2000 and SQL server 2005 </li></ul>
  16. 16. SQL dependency <ul><li>The “Polling model” has to be used in SQL server 2000 to use Cache dependency </li></ul><ul><li>--Application “Polls” data after a given period of time </li></ul><ul><li>--Requires more effort in configuring </li></ul><ul><li>--More resource intensive </li></ul><ul><li>The “Notification model” can be used in SQL server 2005 to use Cache dependency </li></ul><ul><li>--Requires less effort in configuring </li></ul><ul><li>--DB server “Notifies” application with Updates </li></ul><ul><li>--Less resource intensive </li></ul>
  17. 17. SQL dependency:Example <ul><li>In ASPX, or, ASCX page, use </li></ul><ul><li><asp:SqlDataSource EnableCaching=&quot;True&quot; SqlCacheDependency=&quot;Northwind:Products&quot; ... /> </li></ul><ul><li>In web.config, use the following configuration: </li></ul><ul><li><caching> </li></ul><ul><li><sqlCacheDependency enabled=&quot;true“ pollTime=&quot;1000&quot;> </li></ul><ul><li><databases> </li></ul><ul><li> <add name=&quot;Northwind“ connectionStringName=&quot;NorthwindConnectionString1&quot;/> </li></ul><ul><li></databases> </li></ul><ul><li></sqlCacheDependency> </li></ul><ul><li></caching> </li></ul><ul><li>Enable SQL server for SQL dependency: Execute following command in the command prompt </li></ul><ul><li>aspnet_regsql.exe -S %Server% -U %uid% -P %pwd% -d %Database% -t %TableName% -et </li></ul>
  18. 18. Page/Fragment output cache : Best practices <ul><li>Don’ts </li></ul><ul><li>Generally use of output cache in pages that contain input forms is not a good idea. </li></ul><ul><li>Generally, it is good practice to not to use output caching in pages that requires authentication. </li></ul><ul><li>Don’t use output cache for static page. </li></ul><ul><li>Don’t use output cache for page that contains lighter content </li></ul><ul><li>Don’t use output cache for page that generally executes faster </li></ul>
  19. 19. Page/Fragment output cache : Best practices <ul><li>Do’s </li></ul><ul><li>Use output cache in public pages </li></ul><ul><li>Use output cache for pages that executes slower </li></ul><ul><li>Use output cache for pages that contains heavy contents </li></ul><ul><li>Use output cache for pages that are frequently being accessed. </li></ul><ul><li>Use Fragment cache (Output cache at User controls) to cache varying common dynamic contents across the pages </li></ul>
  20. 20. Page/Fragment output cache : Best practices <ul><li>Do’s </li></ul><ul><li>Use Post cache substitution for fewer contents in the pages that vary across different parameters </li></ul><ul><li>Use SQL dependency (With longer durations) with output cache for pages that uses data that are very frequently accessed, expensive to retrieve and that are not changed often. </li></ul><ul><li>Set smaller durations for output cache in pages that varies too much depending on input parameters, and/or, that are not frequently accessed </li></ul>
  21. 21. Page/Fragment output cache : Best practices <ul><li>Do’s </li></ul><ul><li>Use moderate duration for pages that are less </li></ul><ul><li>frequently accessed and/or that doesn’t vary too much with input parameters </li></ul><ul><li>Use long duration for pages that are very frequently accessed and/or that vary little with input parameters </li></ul><ul><li>Use Output cache profiles for better manageability (Configuring profiles in web.config) </li></ul>
  22. 22. 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>
  23. 23. 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
  24. 24. 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 DataSet GetProductData() { if (Cache[&quot;ProductData“] = null) { Cache[&quot;ProductData“] = LoadDataSet(); }   Return Cache[&quot;ProductData“]; }
  25. 25. 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><ul><li>SQL dependencies </li></ul><ul><ul><li>SQL based invalidations </li></ul></ul>
  26. 26. 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>
  27. 27. 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>
  28. 28. Caching in Web Farm <ul><li>ASP.NET cache is not usable in a web farm scenario (Load-balanced distributed deployment). </li></ul><ul><li>The .NET remoting could be used to develop a cache server, or, third party caching provider can be used (Ncache, Distributed Cache etc) </li></ul>
  29. 29. 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>
  30. 30. More information <ul><li>aspnet asp x </li></ul><ul><li> </li></ul>