我有一个从数据库返回的结果集,需要几秒钟才能运行。为了提高性能,我将缓存结果长达30分钟。对数据库的调用需要一个日期参数,这是用户从日历中选择的未来60天内的日期。ASP.NET数据缓存
下面是一个使用字符串列表的代码示例保持简单的例子:
public List<String> GetConcertList(DateTime selectedDate)
{
String cacheKey;
cacheKey = "ConcertList" + selectedDate.Date.Ticks.ToString();
List<String> concertList = HttpContext.Current.Cache[cacheKey] as List<String>;
if (concertList == null)
{
// Normally this is the call to the database that passes the selected date
concertList.Add("Just a test for " + selectedDate.ToString());
HttpContext.Current.Cache.Insert(cacheKey, concertList, null, DateTime.Now.AddMinutes(30),
System.Web.Caching.Cache.NoSlidingExpiration);
}
return concertList;
}
是否有更好的方法,然后使用日期的关键缓存每一天的名单?
作为一般的经验法则,如果您直接缓存存储过程的结果,那么缓存键应该将参数与存储过程匹配。所以,你的缓存键看起来不错。但我同意@ o6tech的答案 - 根据您的数据集,也许您可以执行多级缓存(缓存整个数据集,然后再缓存部分数据集)。全部取决于你的数据集以及它会改变的频率。 – RPM1984 2010-09-01 01:29:52