Monday, March 30, 2009

how to Cache an object in asp.net using c#?

Suppose you want to cache an object say like datatable,you can easily do so by

Cache.Insert("DtIndustry", DtIndustry, null, DateTime.Now.AddMinutes(10), System.Web.Caching.Cache.NoSlidingExpiration);

The above command cache the datatable called DtIndustry from 10 min.

Next if u want that object first check the cache first if cache is null then fill that datatable and again add that object into cache.
if (Cache["DtIndustry"] == null)
{
DtIndustry = objDll.retieve_DtIndustry();
Cache.Insert("DtIndustry", DtIndustry, null, DateTime.Now.AddMinutes(10), System.Web.Caching.Cache.NoSlidingExpiration); ;
}
else
{
DtIndustry = (DataTable)Cache["DtIndustry"];
}

No comments:

Post a Comment