Hire ASP Developer
in ASP .NET

Cache Management

Cache Management
by | 17 Jun 2013

Review of:
Product by:
Hire ASP Developer

Reviewed by:
Rating:
4
On June 17, 2013
Last modified:October 22, 2013

Summary:

In a web application, sometimes we require to store data in the server side to avoid costly data retrieval operation from data stores and time consuming data formatting logic to improve application performance as well as to re-use the same data in subsequent requests via users,  machines and applications. So, to achieve this we need to store (cache) data on the server side.

In ASP.NET, we come across features like Cache objects, Session  and Application, but it is important to understand the difference between them in order to effectively use them in web applications.

Caching

Caching data is extremely important for boosting performance in ASP.NET, as the pages and controls are dynamically generated here.

Caching places frequently used data in quickly accessed media like the random access memory of the computer. The ASP.NET runtime consists of a key-value map of CLR objects called cache. This resides with the application and is available via the System.Web.UI.Page and HTTpContext.

In some respect, caching is similar to storing the state objects. Yet, the storing information in state objects is deterministic, that is, you can count on the data being stored there, and caching of data in nondeterministic. The data will not be available if its lifetime come to an end, or the application releases its memory, or caching does not take place for any reason.

You can access items in the cache using an indexer and may control the lifetime of objects in the cache and set up links between the cached objects and their physical sources.

Following are the three important aspects of QOS (Quality Of Service) :

  • Performance – Caching improves application performance due to less data retrieval and formatting operations.
  • Scalability – Since caching minimizes data retrieval and formatting operations, it reduces the load on server resources thus increasing the scalability of the application.
  • Availability – As the application uses data from a cache, the data will be accurately available according to the time frame.

Types of caching in ASP.NET:

  • Application caching
  • Page output caching

Application caching

ASP.NET provides built in Cache object from System.Web.Caching namespace. This object contains ‘Insert‘ method to store any collection into the application cache.

  • Key: This is the primary name to access the cached object in the Cache collection. The key must be unique in the cache.
  • Value: This is the data as an Object that you want to cache.
  • Dependencies: It is the associated item for Cache. Framework will trigger the signal during the changes in this dependency object.
  • AbsoluteExpiration: This is the time as a DateTime object at which the object should be removed from the cache.
  • SlidingExpiration: This is the time as a TimeSpan object after which the object should be removed from the cache if it has not been accessed by a user.
  • Priority: This is a CacheItemPriority enumeration value that you can use to determine which objects are removed first during scavenging (i.e., when memory starts to run low). Lower priority objects are removed sooner.
  • onRemoveCallback: This is an event handler that is called when the object is removed from the cache. This can be null if you don’t want to specify a callback method.

Example:

View Code ASP.NET
1
2
3
4
5
6
7
8
9
10
11
Cache["EmpName "] = "EmpName";
 
Cache["DeptName"] = "DeptName";
 
Cache["RoleName"] = "Role";
 
lblEmpName.Text = Cache["EmpName"];
 
lblDeptName.Text = Cache["DeptName"];
 
lblRoleName.Text = Cache["RoleName"];

 Page output caching

To enable Page Output Caching, ASP .NET provides declarative approach at each page in your site. Using this approach, the programmer will get the granular control over which pages get cached and how they get cached. It’s possible by adding the @ OutputCache directive to the top of a page’s markup. Programmers can configure this directive by using the below attributes.

  • Duration: The number of seconds to cache the page. It’s the only mandatory parameter.
  • Location: One of the OutputCacheLocation enumeration values, such as Any (Default), Client, Downstream, Server, None, or ServerAndClient.
  • CacheProfile: The name of the cache settings to associate with the page.
  • NoStore: Flag to determine whether to prevent secondary storage of sensitive information.
  • Shared: Flag to determine whether user control output can be shared with multiple pages. The default is false.
  • VaryByParam: A semicolon-separated list of strings used to vary the output cache.
  • VaryByControl: A semicolon-separated list of strings used to vary a user control’s output cache.
  • SqlDependency: It identifies a set of database and table name pairs on which a page or control’s output cache depends.

Leave a Reply