代码之家  ›  专栏  ›  技术社区  ›  Josh

ASP.NET数据缓存

  •  2
  • Josh  · 技术社区  · 15 年前

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

    有没有比使用键中的日期来缓存每天的列表更好的方法?

    1 回复  |  直到 15 年前
        1
  •  1
  •   Jemes    15 年前

    我们这里讨论的数据集有多大?如果不是很大,您可能需要缓存整个列表,然后根据用户选择的日期拉出子集。

    更好的方法是保留一个主数据集,它是所有用户请求日期的集合。基本上,您只需将任何新的、尚未请求的结果附加到缓存的数据集中,然后从那里开始工作。

    推荐文章