views:

175

answers:

1

For my case, i have a controller, which query then forward the user using RedirectResult, which actually did a header "Location".

Then i apply the cache to the controller like this

[OutputCache(Duration = int.MaxValue, VaryByParam = "none", NoStore=false)]

I try to re-run the page, and I check on my Linq profiler, I still able to see all the query of that page re run in like 1s.

How can I prevent this from happen?

A: 

You can do the manual cache, instead of using output cache, which will cache your queries:

public IQueryable<Category> FindAllCategories()
{
    if (HttpContext.Current.Cache["AllCategories"] != null)
        return (IQueryable<Category>)HttpContext.Current.Cache["AllCategories"];
    else
    {
        IQueryable<Category> allCats =  from c in db.Categories
                                          orderby c.Name
                                          select c;

        // set cache
        HttpContext.Current.Cache.Add("AllCategories", allCats, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 0, 30, 0, 0), System.Web.Caching.CacheItemPriority.Default, null);
        return allCats;
    }
}
DucDigital
If this is the answer you found, you should consider accepting it.
Dan Atkinson
should be able to accept by tomorow, dan :)
DucDigital