Overview of caching
Caching is an important concept in computing. When applied to ASP.NET, it can greatly enhance the performance of your Web applications. Caching is used to speed up the performance of the application. We can store these into cache memory. The retrieval of these pages will be fast.
ASP.NET 2.0 frameworks support the following types of caching.
- Page output caching
- Partial page caching
- Data source caching
- Data caching
Page output Caching
Output caching caches the output of a page (or portions of it) so that a page's content need not be generated every time it is loaded.
Consider the following page where a user logs in to a site and reads the latest news. The page will display the user's name and retrieve the latest news since he last logged in.
Figure 1: Displaying the latest news.
In a typical ASP.NET page, every time the user views the page, the Web server will have to dynamically generate the content of the page and perform the relevant database queries (which are a very expensive task to do).
Considering the fact that the page does not change for a certain period of time, It is always a good idea to cache whatever is non-static so that the page can be loaded quickly.
Specifying the cache location
You can use the location attribute of the <%@ output cache %> directive to specify where a page is cached. This attribute accepts the following values:
1. Any
2. Client
3. Downstream
4. None
5. Server
6. Server and client
Any: A page is cached on the browser, proxy server, and web server(the default value).
Client: The page is cached only on the browser.
Downstream: The page is cached on the browser and any proxy server, but not the web server.
None: The page will not cache on browser/proxy server/web server.
Server: The page is cached on the web server but not on the browser or any proxy server.
Server and client: The page is cached on the browser and web server, but not any proxy server.
By default, when you use page output caching, a page is cache in three locations:
- The web server
- Proxy server
- The browser
These are situations in which you might need to modify this default behavior.
For example: If you caching private information then you don't want to cache the information on the web server or any proxy server.
Note: When windows authentication is enabled in the web configuration file (the default). The cache control header is automatically set to the value private, and setting of the location attribute is ignored.
Default caching is for five minute.
To judge caching date time class when the page goes in cache then there is date and time and when we retrieve the page then no change in date and time because no processing on the server.
Best option for caching is on client.
State management is used to access the data at one page to another.
Manipulating the page output cache programmatically: There are three attributes use to manipulate the page output caching in asp.net2.0
- Vary by param
- Vary by header
- vary by custom
Vary by Param: In vary by param the caching perform on the basis of the parameter list which is supplied at run time. The Vary by param attribute specifies how the caching should be performed, based on the query string supplied to the page.
Vary by header: In very by header the caching perform on the basis of language.(the default language of system is US English).this can be done by satali0te assemblies and culture assemblies.
Vary by custom: In vary by custom the caching is performing on the basis of browser.
Data caching: For data caching we create cache object to store into the cache.Cache variable are like application variable are declare on fly and can be access into the page of the root directory. We can direct use the cache object with the key or we can use the insert or add method of the cache object.
New Cache Features:
Cache Profiles
Cache profiles allow you to create cache settings in an application's Web.config file and then reference those settings on individual pages. This allows you to apply cache settings to many pages at once.
Custom Cache Dependencies
In ASP.NET 2.0 you can create your own custom cache dependency based on application-specific conditions. To create a custom cache dependency, you create a class that inherits from Cache Dependency and in your custom class implement your own dependency methods.
SqlCacheDependency
ASP.NET 2.0 introduces the SqlCacheDependency class, which allows you to configure an item in the cache to have a dependency on a table or row in a Microsoft SQL Server database. ASP.NET 2.0 allows you to set a dependency on a table in SQL Server 7.0, SQL Server 2000, and SQL Server 2005. When using SQL Server 2005, you can also set a dependency on a specific record.
Post-Cache Substitution
ASP.NET 2.0 now supports post-cache substitution, which allows you to configure a section in a page as non-cacheable.
Caching Enhancements:
- Control Caching
- Cache configuration Enhancement.
In ASP.NET 2.0, you can configure user control cache settings at run time with the CachePolicy object. The CachePolicy object allows you to work with user control caching the same way that you can work programmatically with page output caching.
ASP.NET 2.0 introduces new cache configuration settings that you can specify in an application's Web.config file. These settings allow more control over the cache, such as how much memory is used and cache scavenging behavior.
Time-Based Caching:
Another technique for caching is based on time. For example, the cache can expire on a certain date, or it will only be available for a certain period of time. There are two ways in which you can use time-based caching:
- Absolute Expiration. The cache is set to expire on a particular date and time.
- Sliding Expiration. The cache is set to expire after a certain period of inactivity.
Conclusion:
Caching of Web pages is an effective way of increasing performance while minimizing the use of precious server resources. Choosing the appropriate level for caching data is important for balancing caching versus memory usage. One of the most effective strategies to good Web application performance is to cache data only when necessary.