簡體   English   中英

ASP.NET數據緩存

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM