Download presentation
Presentation is loading. Please wait.
Published byHenry Ball Modified over 9 years ago
1
Microsoft ASP.NET: An Overview of Caching
2
2 Overview Introduction to ASP.NET caching Output caching Data caching Difference between Data Caching and Output Caching
3
3 What is Caching Cache = “secret store” Armies: cache weapons Armies: cache weapons Squirrels: cache nuts Squirrels: cache nuts Computers: cache data Computers: cache data Benefits of caching data: Reuse Reuse Process once, reuse many times Process once, reuse many times Faster, cheaper Faster, cheaper Caching means temporary storage of data in memory that is highly- demanding and frequently used in order to accelerate performance and for quick access to various essential information. Caching means temporary storage of data in memory that is highly- demanding and frequently used in order to accelerate performance and for quick access to various essential information.
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 Output (or) Page Caching This is the most simplest form of caching. caches the output of a page (or portions of it) so that content of page are not generated every time it is loaded.
6
6 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
7
7 @ 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);
8
8 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” %>
9
9 OutputCache Members: VaryByParam and VaryByHeader VaryByParam This is a mandatory parameter. This is used to mention whether variables in the request should result in separate cache entries. “none" can be used to specify no separate cache requirement. "*" indicates create a new cache entires for different set of variables.
10
10 VaryByHeader, VaryByCustom VaryByHeader and VaryByCustom are primarily used to customise look and feel of the on the client accessing site. VaryByHeader and VaryByCustom are primarily used to customise look and feel of the on the client accessing site. <%@ OutputCache Duration="60“ VaryByParam="None" VaryByCustom="browser" %>
11
11 Data Caching What is data caching? How to insert objects into the cache Cache dependencies
12
12 Data Caching The basic principle of data caching is that you add items that are expensive to create to a special built in collection object How to insert items into cache Cache.Insert(key,dependencies,absoluteExpiration,_ Slidingexpiration)
13
13 Cache.Insert parameters ParameterDescription Key A string that assigns a name to the cached item Item The actual object that is to be cached Dependencies A cache dependency object that allows you to create a dependency absoluteExpiration A Datetime object representing the time at which the item will be removed from cache
14
14 ParameterDescription slidingExpiration A Timespan object represents how long the ASP.NET will wait between requests before removing a cached item Cache.Insert ("student", ds, Nothing, DateTime.MaxValue, TimeSpan.FromMinutes(2))
15
15 Cache dependency Suppose a local XML file is cached into the cache object Incase the XML file is updated and it needs to be reloaded once again but due to cache there may be latency Cache dependencies overcomes this dilemma It makes the object to drop from the cache the moment it is amended
16
16 Database Cache Browser Data Caching Data Cache dependency
17
17 Data Caching Output Caching Data caching allows a web application to store commonly used objects so that they are available for use across all pages in the site. Page caching allows the server to store a copy of the output of a dynamic page, and to use this to respond to requests instead of running the dynamic code again. Data caching is the storing of data internal to a web application, such that different pages can access the data. If you cache a page, you store a copy of the output of the page
18
18 Data Caching Output Caching The data cache is available through the Cache property of either the Page or the HttpContext class There are three approaches to setting page caching. @OutputCache directive in the top of the ASP.NET page. @OutputCache directive in the top of the ASP.NET page. cache options via code. cache options via code. The third option is to use meta data attributes in code The third option is to use meta data attributes in code Data remains in the cache for the time specified by the DateTime parameter of the Cache.Insert method Pages remain in the cache for some length of time, determined by the Duration parameter. The value is set in seconds.
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 Cache Dependency File based dependency Key based dependency Time based dependency
21
21 Key-based dependency invalidates a particular Cache item when another Cache item changes. For example, if instead of loading our ProductData from a database, we loaded it from an XML file Dim dom As XmlDocument() dom.Load(Server.MapPath("product.xml") Cache("ProductData") = dom
22
22 Dim dependency as new CacheDependency (Server.MapPath("product.xml")) Cache.Insert("ProductData", dom, dependency)
23
23 Key based dependency
24
24 Time based dependency Time-based dependency simply expires the item at a defined point in time.We have two options for time based dependency Absolute—Sets an absolute time; for example, current time + 10 minutes for the Cache entry to expire. Absolute—Sets an absolute time; for example, current time + 10 minutes for the Cache entry to expire. Sliding—Resets the time for the item in the Cache to expire on each request Sliding—Resets the time for the item in the Cache to expire on each request
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.