[英]ASP.NET Data Caching
我有一个需要几秒钟才能运行的数据库返回的结果集。 为了提高性能,我将缓存结果最多30分钟。 对数据库的调用采用日期参数,即用户从日历中选择的未来60天内的日期。
这是一个使用字符串列表使示例保持简单的代码示例:
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;
}
有没有一种更好的方法可以使用键中的日期来缓存每天的列表?
我们在这里谈论的数据集有多大? 如果不是很大,则可能需要缓存整个列表,然后根据用户的选择日期拉出子集。
更好的方法是保留一个主数据集,该数据集是所有用户要求的日期的总和。 基本上,您只需将所有尚未请求的新结果附加到缓存的数据集中,然后从那里开始工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.