Codice per uso cache in Asp.Net

Tecnica di utilizzo della cache di Asp.Net, spunto di Html.It.

public ArrayList GetUsers(bool AllowCache)
{
// this directive will force the variable AllowCache to
// false while in ‘debug mode’ in Visual Studios .NET
#if DEBUG
AllowCache = false;
#endif
string strCacheKey = “Users-GetUsers”;
// cached?
//
if( AllowCache && ( null != HttpContext.Current.Cache[strCacheKey]) )
return (ArrayList)HttpContext.Current.Cache[strCacheKey];
// code to fetch users from database
//
ArrayList alUsers = null;
alUsers = Users.GetUsers();
// cache it if ‘allowed’
if ((AllowCache) && (HttpContext.Current.Cache[strCacheKey] == null))
HttpContext.Current.Cache.Insert(strCacheKey, alUsers, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero);
return alUsers;
}

~ di pythonyan su 29 Novembre 2007.

Lascia un commento