且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在缓存中存储的数据表重用呢?

更新时间:2023-11-17 16:00:22

东西沿着这些路线?

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;
}

您会需要一些机制来从缓存刷新的数据表,如果它的变化。例如,你可以使用SqlCacheDependency.

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);