Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Different ways to cache application data in asp.net - Part 127

Suggested Videos
Part 124 - Caching multiple versions of user control using VaryByControl
Part 125 - Caching multiple versions of user control using VaryByParam
Part 126 - Caching application data in asp.net

In Part 126, of the asp.net video tutorial we discussed about, "Caching application data" using direct assignment as shown below. Please watch Part 126, from the asp.net video tutorial by clicking here, before proceeding.
Cache["Cache_Key"] = Data_To_Cache



In this video we will discuss about
1. Different ways to cache application data 
2. Removing an item from cache
3. Similarities and difference between cache and application state



Apart from caching data, using assignment, there are 2 other ways to cache application data
1. Using "Cache" object's Insert() method
2. Using "Cache" object's Add() method

Caching application data, using "Cache" object's Insert() method:
Cache object's, Insert() method has got 5 overloaded versions. Depending on application requirement, we can choose the overloaded version that best suits our needs. The simplest overloaded version, takes 2 parameters.
1. The Cache Key and
2. The data that we want to cache

In Part 126, we cached products dataset, using assignment as shown below
Cache["ProductsData"] = ds;

The same thing can be achieved, using cache object's "Insert()" method, as shown below.
Cache.Insert("ProductsData", ds);

We will discuss about other overloaded versions of "Insert()" method in a later video session.

Caching application data, using "Cache" object's Add() method:
Adding application data, to the cache object, using "Add()" method is similar to "Insert()" method. As "Insert()" method has got several overloaded versions, some of the parameters are optional. Where as, "Add()" method does not have any overloads, and hence, all parameters must be specified.
Cache.Add("ProductsData", ds, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);

We will discuss about "absolute expiration", "sliding expiration", and CacheItemPriority in a later video session. 

Be cautious when dealing with cache keys: Assigning a value to a cache key, that is already being used will silently overwrite the existing value, without any warnings. For example
The value stored in "MyKey" is "Value1"
Cache["MyKey"] = "Value 1";

The following line will silently overwrite "MyKey" value to "Value 2" 
Cache["MyKey"] = "Value 2";

To Remove an item from cache explicitly, use Remove() method. The following line would remove the cache item with key "MyKey"
Cache.Remove("MyKey");

Please Note: An item may be removed automatically, from cache, when any of the following conditions are true
The cached item has expired.
The cache is full.
There is a cache dependency, and the item, that the cache object is dependent on has changed.

In a way, Cache object is similar to Application state. The objects that we store in application state variables are available anywhere within a Web application. Objects stored in cache are also available anywhere within a Web application. But the difference is that, items stored in cache can expire, where as items in application state will never expire.

3 comments:

  1. Hi Venkat,
    This is chandu,You have not provided a video link for asp.net part 127 video in your blog but video was avialble in your youtube channel.
    Once see and correct it.
    Once again thank you very much for providing this technical stuff for community
    Thanks
    chandu

    ReplyDelete
    Replies
    1. Hi Chandu, Thank you very much. I have included it now. Good Luck.

      Delete
  2. what is the default time out for Cache

    ReplyDelete

It would be great if you can help share these free resources