Saturday 2 April 2011

Asp.net Data Caching

Often an application can increase performance by storing in memory data that is accessed frequently and that requires significant processing time to create. For example, if your application processes large amounts of data using complex logic and then returns the data as a report accessed frequently by users, it is efficient to avoid recreating the report every time a user requests it. Similarly, if your application includes a page that processes complex data but that is updated only infrequently, it is inefficient for the server to recreate that page on every request.

To help you increase application performance in these situations, ASP.NET provides caching using two basic caching mechanisms. The first is application caching, which allows you to cache data you generate, such as a DataSet or a custom report business object. The second is page output caching, which saves the output of page processing and reuses the output instead of re-processing the page when a user requests the page again.

The application cache

The application cache provides a programmatic way for you to store arbitrary data in memory using key/value pairs. Using the application cache is similar to using application state. However, unlike application state, the data in the application cache is volatile, meaning it is not stored in memory for the life of the application. The advantage of using the application cache is that ASP.NET manages the cache and removes items when they expire or become invalidated, or when memory runs low. You can also configure application caching to notify your application when an item is removed. For more information see Caching Application Data.

The pattern when using the application cache is to determine whether an item exists in the cache any time you access an item, and if it does, to use it. If the item does not exist, you can recreate the item and then place it back in the cache. This pattern ensures that you always have the latest data in the cache.

The page output cache

The page output cache stores the contents of a processed ASP.NET page in memory. This allows ASP.NET to send a page response to a client without going through the page processing lifecycle again. Page output caching is especially useful for pages that do not change often but require significant processing to create. For example, if you are creating a high-traffic Web page to display data that is not frequently updated, page output caching can dramatically increase the performance of that page. Page caching can be configured individually for each page, or you can create cache profiles in the Web.config file, which allow you to define caching settings once and then use those settings with multiple pages.

Page output caching provides two models for page caching: full page caching and partial page caching. Full page caching allows the entire contents of a page to be persisted to memory and used to fulfill client requests. Partial page caching allows parts of a page to be cached and other parts to be dynamic. For more information see Caching ASP.NET Pages.

Partial page caching can work in two ways: control caching and post-cache substitution. Control caching, also sometimes referred to as fragment caching, allows you to cache parts of the page output by including the information in a user control and then marking the user control as cacheable. This allows specific content within a page to be cached, while the overall page is not cached and therefore recreated each time. For example, if you create a page that displays largely dynamic content, such as stock information, but has sections that are static, such as weekly summaries, you can place the static sections in user controls and allow them to be cached.

Post-cache substitution is the opposite. The page as a whole is cached, but fragments within the page are dynamic. For example, if you create a page that is static for set periods of time, you can set the entire page to be cached. If you added a Label control to the page that displayed the user's name, the Label would stay the same for each page refresh and each user, showing the name of the user who requested that page before it was cached. However, with post-cache substitution, you can configure the page to be cached, but mark individual sections of the page as not cacheable. In this case, you could add your Label controls to a non-cacheable section and they would be dynamically created for each user and page request.

Inserting an Item to Cache

The Add method enables you to add an item to the cache. The Add method ignores any call to add an item if another item with the same key already exists in the cache. The insert method functionally is the same asAdd method but it overwrites the item if another item with the same key already exists in the cache.

Let's look at the insert method:

 public void Insert(string key, object value,CacheDependency dependencies,   DateTime absoluteExpiration,   TimeSpan slidingExpiration,   CacheItemPriority priority,   CacheItemRemovedCallback onRemoveCallback );
  • value: The object to be inserted in the cache.
  • key: The cache key used to reference the object.
  • dependencies: The file or cache key dependencies for the item. When any dependency changes, the object becomes invalid and is removed from the cache. If there are no dependencies, this parameter contains a null reference (Nothing in Visual Basic).
  • absoluteExpiration: The time at which the inserted object expires and is removed from the cache.
  • slidingExpiration: The interval between the time the inserted object was last accessed and when that object expires. If this value is the equivalent of 20 minutes, the object will expire and be removed from the cache 20 minutes after it was last accessed.
  • priority: The cost of the object relative to other items stored in the cache, as expressed by theCacheItemPriority enumeration. This value is used by the cache when it evicts objects; objects with a lower cost are removed from the cache before objects with a higher cost.
  • onRemoveCallback: A delegate that, if provided, will be called when an object is removed from the cache. You can use this to notify applications when their objects are deleted from the cache.

 Cache.Insert ("DSN", connectionString,new CacheDependency(�filepath�)    DateTime.Now.AddMinutes(2),    TimeSpan.Zero,    CacheItemPriority.High,   new CacheItemRemovedCallback(this.RemovedCallback) );

When we are inserting an object to cache, we can set the dependency and a call back function that will be executed when the cache expires. CacheItemRemovedCallback is a delegate, which defines a callback method for notifying applications when a cached item is removed from the Cache. Now, we can do some cool things with these two features. We will first look at setting the dependencies.

Posted By: Mr. Palash Paul


No comments:

Post a Comment