简体   繁体   中英

Caching in asp.net for dynamic data in gridview

I've a webpage with GridView which is populated from database table. User can click on column names which gives an option of distinct values through which user can select the filter.. Just like how we have AutoFilter option in Excel . It is building dynamic queries based on user filter selection and populating the Gridview.

I've to use Caching mechanism for this webpage as the data would change once in a day.

I tried using <%@ OutputCache Duration=3600 VaryByParam="None" %> when I select the filter it populates for the first time. But when I select for further filter, IE gives error 'Internet Explorer cannot display the webpage'

I'm not sure whether caching is feasible for this webpage as queries are dynamic. Please let me know is there any other mechanism to achieve this or to enhance the website performance.

Output caching is not what you want in this situation, it caches the HTML output of the page so ASP.NET doesn't have to regenerate it.

It sounds like you're looking for the built in Cache class that can hold arbitrary objects for given amounts of time.

Depending on how fast your queries are you could either cache the whole data set and then filter it with LINQ, or you could store the filtered versions you got from SQL Server.

I use it something like this:

var cachedList = HttpContext.Current.Cache["cacheKey"];
if (cachedList == null)
{
    var db = new YourDataContext();

    //get your data from the db here somehow
    cachedList = db.table
        .Where(x => true)
        .ToList();

    HttpContext.Current.Cache.Add(
        "cacheKey",
        cachedList,
        null,
        DateTime.Now.AddMinutes(10),
        System.Web.Caching.Cache.NoSlidingExpiration,
        CacheItemPriority.Normal,
        null);
}
return (List<YourDataType>)cachedList;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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