我有一個從數據庫返回的結果集,需要幾秒鐘才能運行。爲了提高性能,我將緩存結果長達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