Caching in ASP.NET By SRIRAM. B
Objective
Caching Concepts
Cache Profiles
Types of Caching
Cache Dependency
Page Output Caching
File Dependency
Partial Page Caching
SQL Cache Dependency
Post Cache Substitution
Data Source Caching
Data Caching
Cache Life Time
SQL Cache Invalidation
Cache Event
Concepts
SQL Cache Dependency
SQL Cache Dependency Configuration
SQL Cache Dependency Attribute
Caching Concepts
Caching Concepts
Caching is a technique used to improve the performance by keeping the frequently accessed data in memory.
It is used to retain the pages or data access HTTP requests and reuse them without the expense of re-creating them.
It saves time and resources. It is used to create high-performance and scalable web applications.
Types of Caching
Types of Caching
Page Output Caching It is used to catch the entire page.The next time that any user requests the same page the page is retrieved from the cache.
Partial Caching It is used to catch a regions of a page. You can apply different caching policies to different areas of a page.
Data Caching Caching the database objects and any other data and use them in multiple ASP.NET Pages.
DataSouce Caching Caching with different ASP.NET DataSource Controls such as SQLDataSource & ObjectDataSource controls. It caches the data that it represents
Page Output Caching
Page Output Caching
Output Cache Attributes
Output Cache Attributes..
The VaryByParam attribute causes a new instance of a page to be cached when a different parameter is passed to the page(The parameter may be query string parameter or form parameter)
You can assign two special values to the VaryByParam attribute:
None -> Causes any query string or form parameter to be ignored. Only one version of the page is cached.
* -> Causes a new cached version of the page to be created whenever there is a change in query string or form parameter passed to the page.
The VaryByControl attribute enables you to generate different cache versions of a page depending on the value of a particular control in the page. It is useful when you need to create a single-page Master-Detail Form.
Output Cache Attributes..
The VaryByHeader attribute to create different cached versions of a page when the value of a particular browser header changes. The browser headers are transmitted with each page request, including Accept-Language, User-Agent, Cookie
The VaryByCustom attribute to create different cached versions of a page that depend on the type of browser being used requested to the page.The browser attribute is the type of browser and major version.
Partial Page Caching
Partial Page / Fragment Caching
Fragment Caching in Web Page
Post Cache Substitution
Post Cache Substitution
Used when we want to cache the whole page but also need some dynamic region inside the cached page is known as Post Cache Substitution.
There two ways to implement the Post Cache Substitution
Response.Write Substitution
Substitution Control
User Controls with Substitution
Container Page
Data Source Caching
SDS Cache Dependency
Data Caching
Data Caching
The cache provides a simple dictionary interface that allows programmers to easily place objects in and retrieve them from the cache. In the simplest case, placing an item in the cache is just like adding an item to a dictionary: Cache["mykey"] = myValue;
The existence of the object should be checked before retrieving the data from the cache: myValue = Cache["mykey"]; if(myValue != null ) { DisplayData(myValue); }
Data Caching..
Scavenging means that the cache attempts to remove infrequently used or unimportant items if memory becomes scarce. Programmers who want to control how scavenging occurs can provide hints to the scavenger when items are inserted into the cache that indicate the relative cost of creating the item and the relative rate at which the item must be accessed to remain useful.
Expiration allows programmers to give cache items lifetimes, which can be explicit (for example, expire at 6:00) or can be relative to an item's last use. After an item has expired, it is removed from the cache and future attempts to retrieve it return the null value unless the item is reinserted into the cache.
SQL Cache Dependency
SQL Cache Dependency Configuration
SQL Dependency Attribute
Cache Profiles
Cache Profiles
Cache configuration is used to cache the profiles in web.config system and then use those profiles in multiple asp.net pages.
Cache Profiles allows cache setting for hundreds of page to be displayed and managed centrally from configuration.
Cache Dependency
Cache Dependency
Cache dependencies allow the validity of a cache item to be based on an external file or on another cache item. If a dependency changes, the cache item is invalidated and removed from the cache.
File Dependency Cache item depend on the external file
Key Dependency Cache item depend on the another cache item
SQLCache Dependency Dependent on the database item
Page Cache File Dependency
Data Cache Dependency
Page Cache Dependency
Cascaded Dependency
Cache Life Time
Cache Life Time
Absolute Expiration You can specify the actual time period after which the cached item will be removed from the memory.
Sliding Expiration You specify the time period after which the cached time will be removed from the memory after it is last accessed
Absolute and Sliding Expiration
Cache Event
Cache Event Handling
Cache Item Removed Callback
Cache Item Removed Callback..
SQL Cache Invalidation
Enables you to make the cache item dependent on the entry in the database, so the cache entry will be only cleared when the data in the database is changed.
Types : Polling based, SQL Notification based
For Pooling based <%@ OutputCache Duration="999999" SqlDependency="Pubs:Authors" VaryByParam="none" %> For SQL Notificated based <%@ OutputCache Duration="999999" SqlDependency="CommandNotification" VaryByParam="none" %>
Cache Invalidation Reasons
Demo
Session Ends
Exercise
Relax