简体   繁体   English

ASP.NET数据缓存

[英]ASP.NET Data Caching

I have a result set returned from a database that takes a few seconds to run. 我有一个需要几秒钟才能运行的数据库返回的结果集。 To increase performance I'm going to cache the results for up to 30 minutes. 为了提高性能,我将缓存结果最多30分钟。 The call to the database takes a date parameter, a date within the next 60 days that the user selects from a calendar. 对数据库的调用采用日期参数,即用户从日历中选择的未来60天内的日期。

Here is a code sample that uses a list of strings to keep the example simple: 这是一个使用字符串列表使示例保持简单的代码示例:

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

Is there a better approach then using the date in the key to cache each day's list? 有没有一种更好的方法可以使用键中的日期来缓存每天的列表?

How large is the dataset we're talking about here? 我们在这里谈论的数据集有多大? If it's not huge, you may want to Cache the whole list and then just pull out the subset based on the user's selected date. 如果不是很大,则可能需要缓存整个列表,然后根据用户的选择日期拉出子集。

A better way to do this would be to keep a master dataset that's an aggregate of all of the user-requested dates. 更好的方法是保留一个主数据集,该数据集是所有用户要求的日期的总和。 Basically, you would just append any new, not-yet-requested results to your cached dataset and work from there. 基本上,您只需将所有尚未请求的新结果附加到缓存的数据集中,然后从那里开始工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM