i

ASP.Net A Complete Guide

How to use Object cache

//Assign the value to the cache

Cache["Greetings"]="Good Morning!";

//use the cache

LabelGreetings.Text=Cache["Greetings"] != null?(string)Cache["Greeting"]:"";

It's always good practice to check the cache item is null or not else. It has the chance to break the application with the null reference exception.

How to insert the item to the cache:

Below are the different parameters available for this caching technique:

Key: Key used to access the item. It must be unique in the cache.

Value:  Data (object) the needs to be cache.

Dependencies - It specifies if the cache key depends on some other item in the cache. If it is dependent and that is changed, then the cache of that object gets automatically cleared.

Absolute expiration - its value is in date-time format. It specifies at what time the object should be removed from the cache.

Sliding expiration - It specifies that the object should be removed after this timespan only if it has not been accessed by the user.

Priority - This determines which objects need to removed first when memory starts to run low.

  • Low

  • below normal

  • Normal - This is the default value

  • abnormal

  • High

  • not removable