Something along these lines?
public DataTable GetDataTableFromCacheOrDatabase()
{
DataTable dataTable = HttpContext.Current.Cache["secret key"] as DataTable;
if(dataTable == null)
{
dataTable = GetDataTableFromDatabase();
HttpContext.Current.Cache["secret key"] = dataTable;
}
return dataTable;
}
You're going to need some mechanism to flush the data table from the cache if it changes. For example you could use an SqlCacheDependency.
As requested, to clear the cache an hour after the table is added you would do:
HttpContext.Current.Cache.Insert("secret key", dataTable, null, DateTime.Now.AddHours(1), System.Web.Caching.Cache.NoSlidingExpiration);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…