Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microsoft ASP.NET: An Overview of Caching Holly Mazerolle Developer Support Engineer Microsoft Developer Support Microsoft Corporation.

Similar presentations


Presentation on theme: "Microsoft ASP.NET: An Overview of Caching Holly Mazerolle Developer Support Engineer Microsoft Developer Support Microsoft Corporation."— Presentation transcript:

1 Microsoft ASP.NET: An Overview of Caching Holly Mazerolle Developer Support Engineer Microsoft Developer Support Microsoft Corporation

2 2 Overview  Introduction to ASP.NET caching  Output caching  Fragment caching  Data caching

3 3 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 Web server, proxy server, and client browsers  Types of caching Output caching Output caching Fragment caching Fragment caching Data caching Data caching

4 4 Output Caching  What is output caching?  @ OutputCache directive and the cache object  Output caching attributes: Duration Duration Location Location VaryByParam VaryByParam VaryByHeader VaryByHeader VaryByCustom VaryByCustom

5 5 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 Server response times reduced CPU load reduced CPU load reduced  Appropriate caching of pages affects site performance dramatically

6 6 @ OutputCache Directive and the Cache Object  @ OutputCache declaratively controls caching behavior For.aspx,.asmx, or.ascx For.aspx,.asmx, or.ascx  The cache object programmatically controls caching behavior <%@ OutputCache Duration="600“ Location="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  Duration sets the time to cache the output In seconds In seconds Required 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 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 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 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 Any: Output cache can be located on any of these locations None: No output caching is turned on for the item None: No output caching is turned on for the item <%@ OutputCache Duration="600" Location="Any“ VaryByParam=“none” %>

8 8 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 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 The cache stores multiple copies of a page based on HTTP headers <%@ OutputCache Duration="10“ VaryByParam="location;count" %> <%@ OutputCache Duration="60“ VaryByHeader="Accept-Language" %>

9 9 OutputCache Members: VaryByCustom  VaryByCustom If the value is “Browser,” cache varies by browser type and major version 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 If the value is a custom string, you must override HttpApplication.GetVaryByCustomString in the Global.asax and implement your own caching logic

10 10 Fragment Caching  What is fragment caching?  VaryByControl  Nested cached user controls  Cached controls are not programmable

11 11 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 VaryByParam VaryByControl VaryByControl  Location not supported because fragments must reside on server to be assembled

12 12 Fragment Caching a User Control [*.ascx] <%@ OutputCache Duration="10“ VaryByControl="State;Country" VaryByParam="*"%> VaryByParam="*"%> public String State { public String State { get { return state.Value; } get { return state.Value; } set { state.Value = State; } } set { state.Value = State; } } public String Country { public String Country { get { return country.Value; } get { return country.Value; } set { country.Value = Country; } } set { country.Value = Country; } }</script>

13 13 VaryByControl  VaryByControl The sixth attribute supported by OutputCache The sixth attribute supported by OutputCache Only supported in user control caching Only supported in user control caching Caching is based on user control properties Caching is based on user control properties <%@ OutputCache Duration="10“ VaryByControl="State;Country“ VaryByParam="*"%>

14 14 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 Is very efficient and powerful Requires almost nothing from a developer Requires almost nothing from a developer  Varying by many properties could create a large tree, and therefore, a large memory footprint

15 15 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

16 16 Data Caching  What is data caching?  Working with the cache object  Cache dependencies  Scavenging memory  Using callbacks with caching

17 17 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

18 18 Working with the Cache Object  Cache object features Dependencies allow logic to invalidate cached items Dependencies allow logic to invalidate cached items Scavenging (automatic expiration) Scavenging (automatic expiration) Callbacks when an item is removed from cache 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("ProductData")) Then Cache("ProductData") = LoadDataSet() Return Cache("ProductData") End Function

19 19 Cache Dependencies  File-based dependencies Cached item invalidated when files change Cached item invalidated when files change  Key-based dependencies Cached item invalided when another cached item changes Cached item invalided when another cached item changes  Time-based dependencies Absolute time-based invalidations Absolute time-based invalidations Sliding time-based invalidations Sliding time-based invalidations

20 20 Scavenging Memory  Automatic system that initiates when memory becomes scarce  Tag cached items with relative importance CacheItemPriority CacheItemPriority CacheItemPriorityDecay CacheItemPriorityDecay  Items must be added to cache using.Add or.Insert to make use of these enumerations

21 21 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 Cleanup Update Update Recreate Recreate Logging Logging  Items must be added to caching using.Add or.Insert to use callback functionality

22 22 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.

23 23 Additional Information about Caching  INFO: ASP.NET Caching Overview INFO: ASP.NET Caching Overview INFO: ASP.NET Caching Overview  HOW TO: Control Page Output Caching in ASP.NET by Using Visual Basic.NET HOW TO: Control Page Output Caching in ASP.NET by Using Visual Basic.NET 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 HOW TO: Perform Fragment Caching in ASP.NET by Using Visual Basic.NET HOW TO: Perform Fragment Caching in ASP.NET by Using Visual Basic.NET

24 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://support.microsoft.com/webcasts/ Your feedback is sincerely appreciated. Please send any comments or suggestions about the Support WebCasts to supweb@microsoft.com. supweb@microsoft.com


Download ppt "Microsoft ASP.NET: An Overview of Caching Holly Mazerolle Developer Support Engineer Microsoft Developer Support Microsoft Corporation."

Similar presentations


Ads by Google