1 / 18

ASP .NET Caching

ASP .NET Caching. - Pradeepa Chandramohan. What is Caching?. Storing data in memory for quick access. In Web Application environment, data that is cached is usually, commonly displayed database values. Repeated database calls are avoided.

Download Presentation

ASP .NET Caching

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ASP .NET Caching - Pradeepa Chandramohan

  2. What is Caching? • Storing data in memory for quick access. • In Web Application environment, data that is cached is usually, commonly displayed database values. • Repeated database calls are avoided. • Demand on Web server’s and database server’s system resources are decreased. • Increases performance by keeping frequently accessed data in memory.

  3. Types of Caching • Output Caching • Fragment Caching • Data Caching • Time based Caching

  4. Output Caching • Also known as Page level Caching. • Implementation is through an Output Cache Engine. • Each time an ASP .NET page request comes in, the engine checks for a cached output entry. If found, this cached HTML is sent as response, otherwise, the page is dynamically created and stored in the Output Cache Engine. • Useful in cases of many static pages.

  5. Output Caching Implementation • By using the OutputCache page directive. • Syntax is as shown below: <%@OutputCache Duration=“60” VaryByParam=“none”%> • Duration – number of seconds the HTML output of the Web page is held in Cache. • VaryByParam – Specifies how the caching should be performed based on the query string supplied to the page.

  6. Output Caching - Example • Consider the following URL: http://localhost/Caching/WebForm1.aspx?name=John&newsid=12 • Query String passed to the page is name=John&newsid=12 • Changing the VaryByParam attribute: <%@ OutputCache Duration=“15” VaryByParam=“Name” %> • Page will be cached according to the Name key. • Consider the following two URLs http://localhost/Caching/WebForm1.aspx?name=John&newsid=12 http://localhost/Caching/WebForm1.aspx?name=John&newsid=45 • Second page will be from cache (Cache will be refreshed only if the Name value changes)

  7. Output Caching – Example (Contd..) • Consider the following two URLs: http://localhost/Caching/WebForm1.aspx?name=John&newsid=12 http: //localhost/Caching/WebForm1.aspx?name=John&newsid=45 • For the page to be regenerated, Name and NewsID keys have to be changed. <% Output Cache Duration=“15” VaryByParam=“Name;NewsID” %> • This causes the second page to be refreshed. • Equivalent to using a *, which means change in any key would regenerate the page. <% Output Cache Duration=“15” VaryByParam=“*” %> • If the page has to be cached regardless of query string ‘none’ can be used. <% Output Cache Duration=“15” VaryByParam=“none” %>

  8. Fragment Caching • Caches regions of the page content. • More powerful than Output Caching. • This technique separates portions of a page that takes more time to create (such as database queries) from other parts of the page. • The part of the page that requires less system resources can be generated dynamically for each request.

  9. Fragment Caching - Example • Consider a Web Application that displays the news titles in a Combo Box. • The dataset containing the news is cached in order to minimize the number of times the application wants to connect to the SQL server to retrieve the news.

  10. Fragment Caching – Example (Contd ..) • First time the page loads, user clicks on the Get News button to get the news from the SQL Server. • We assume that the news changes daily. • Page is refreshed each time the date changes. • The cached version is displayed to all the users visiting the page on the same day.

  11. Data Caching • Storing data in memory for quick access. • Items in the data cache will be evicted from memory, if memory becomes scarce. • While adding items to a data cache, the duration of how long it can persist can be specified.

  12. Data Caching - Implementation • .NET data caching API is comprised of two classes in the System.Web.Caching namespace. • The first class (Cache) is used to add and remove items from the data cache. • The second class (Cache dependency) is used to assign a cache dependency to an item in the data cache. • To add an item: Cache[“key”] = value; • This adds the item value to the data cache with the key key. • Key is used to reference the item at some later point.

  13. Data Caching – Implementation (Contd ..) • To extract the value inserted above: value = Cache.Get(“Key”) • To remove item from the cache: Cache.Remove(“Key”)

  14. Time based Caching • Caching based on time i.e. the data is available only for a certain period of time. • Two ways to use: - Absolute Expiration - Sliding Expiration • Absolute Expiration – Cache is set to expire on a particular date and time. • Sliding Expiration – Cache is set to expire after certain period of inactivity.

  15. Time based Caching - Implementation • Absolute Expiration Cache.Insert(“News”,ds,null,DateTime.Now.AddMinutes(2),Cache.NoSlidingExpiration) • The cache is set to expire exactly two minutes after the user has retrieved the data. • Sliding Expiration Cache.Insert(“News”,ds,null,Cache.NoAbsoluteExpiration,TimeSpan.FromMinutes(1)) • This causes the cache to be cleared if the user does not reload the page within one minute.

  16. Use Caching Sparingly • Caching has to be used sparingly. • Takes up valuable system resources and eats up available memory. • When server runs out of memory, contents of cache will be evicted. • Eviction will be based on the priority of the data in the cache which can be set as shown: Cache.Insert(“News”,ds,null,Cache.NoAbsoluteExpiration,TimeSpan.FromMinutes(1),System.Web.Caching.CacheItemPriority.High,null) • There are seven levels of priority: NotRemovable, High, AboveNormal, Default, Normal, BelowNormal, Low.

  17. Conclusion • Effective way of increasing performance. • Minimizes the use of server resources. • Choosing the appropriate level for caching data is important to balance caching versus memory usage. • Most effective strategy in Web Application – Cache data only when necessary.

  18. THANK YOU

More Related