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

Caching in asp.net - AbsoluteExpiration, SlidingExpiration, and CacheItemPriority - Part 128

Suggested Videos
Part 125 - Caching multiple versions of user control using VaryByParam
Part 126 - Caching application data
Part 127 - Different ways to cache application data

In this video we will discuss about AbsoluteExpiration, SlidingExpiration, and CacheItemPriority parameters of the Insert() and Add() methods of cache object. 



When you cache an item using the Insert() or Add() method, you can also speicfy, how long you want the item to be cached. There are 2 ways to do this.
AbsoluteExpiration: A DateTime object that specifies when the data should be removed from the cache. When you specify "AbsoluteExpiration", the cache item will expire at that time, irrespective of whether the cached item is accessed or not. 

For example, the following line will cache dataset, ds, for 10 seconds, irrespective of whether the dataset, is accessed or not within those 10 seconds, after it is cached. Since we are using absolute expiration, we specified Cache.NoSlidingExpiration for "SlidingExpiration" parameter of the Insert() method.
Cache.Insert("ProductsData", ds, null, DateTime.Now.AddSeconds(10), System.Web.Caching.Cache.NoSlidingExpiration);



SlidingExpiration: A TimeSpan object that identifies how long the data should remain in the cache after the data was last accessed.

For example, the following line will cache dataset, ds, for 
10 seconds. If the dataset is accessed from the cache with in the specified 10 seconds, then, from that point, the dataset will remain in cache for the next 10 seconds. Since we are using sliding expiration, we specified Cache.NoAbsoluteExpiration for "AbsoluteExpiration" parameter of the Insert() method.
Cache.Insert("ProductsData", ds, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(10));

What happens if you specify both, AbsoluteExpiration and SlidingExpiration when caching application data?
You get a run time exception stating 'absoluteExpiration must be DateTime.MaxValue or slidingExpiration must be timeSpan.Zero'

CacheItemPriority: Sliding expiration and absolute expiration can be used to control, how long the item is cached, but please note, if the web server is running low on memory, and if it 
requires memory, it may remove cached items that may not have expired. However, the order in which the items are removed is determined by the cached item's priority. Cache item's priority can be specified using CacheItemPriority enum. 

CacheItemPriority enum values:
CacheItemPriority.Low
CacheItemPriority.BelowNormal
CacheItemPriority.Normal
CacheItemPriority.Default
CacheItemPriority.AboveNormal
CacheItemPriority.High
CacheItemPriority.NotRemovable

For example, let us say we have 3 items with the following priorities and they are cached.
Item 1 with CacheItemPriority.NotRemovable
Item 2 with CacheItemPriority.High
Item 3 with CacheItemPriority.AboveNormal

Now, if the server is running low on memory, it may remove the items from cache, even if they have not expired. Since, Item 3 has the least priority, it will be removed first, followed by Item 2 and then finally Item 1.

5 comments:

  1. Hi Venkat,
    First of thank you very much for providing .net stuff in the form of videos.
    I think here link was missing.for part 128.
    once check and correct it
    thanks once again

    ReplyDelete
    Replies
    1. I have included the missing link now. Thank you very much for bringing it to my attention.

      Delete
  2. Hi Venkat,

    This is a great help indeed for everyone who wants to learn .Net. I am really impressed by the way you have presented the concepts, making it simple and easy to understand. Thank you so much!

    Sheena

    ReplyDelete
  3. Very nice video.

    Is there any time limit that can be set to the number of hours/days to cache? or will it work for any amount of time in both sliding expiration and absolute expiration.

    ReplyDelete
  4. Thank you for a very helpful tutorial. I'am new to C# and wondering if your explicitly instantiating a new Cache object?

    ReplyDelete

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