Quote for the Week

"Learn to enjoy every moment of your life"

Wednesday, November 19, 2014

Data Caching in Asp.net

Data Caching:

Data caching stores the required data in cache. So the web server did not send request to DB server every time for each and every request which increase the web site performance. Hence, caching of data can dramatically improve the performance by reducing database hits and round-trips.

We can do the caching like this,


HttpContext.Current.Cache.Insert("strCacheName","data you want to cache", null, DateTime.Now.AddMinutes(expTime), TimeSpan.Zero);

Parameters used:

key:
The cache key used to reference the object.

value:
The object to be inserted in the cache.

dependencies:
The file or cache key dependencies for the inserted object. When any dependency changes, the object becomes invalid and is removed from the cache. If there are no dependencies, this parameter contains null.

absoluteExpiration:
The time at which the inserted object expires and is removed from the cache. To avoid possible issues with local time such as changes from standard time to daylight saving time, use System.DateTime.Utc. Now rather than System.DateTime.Now for this parameter value. If you are using absolute expiration,the slidingExpirationparameter must be System.Web.Caching.Cache.NoSlidingExpiration.

slidingExpiration:
The interval between the time the inserted object is last accessed and the
time at which 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. If you are using sliding expiration, the absoluteExpiration
parameter must be System.Web.Caching.Cache.NoAbsoluteExpiration.

No comments: