Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

programmatically control output caching - disable or enable cache according to parameter value

Tags:

We've got a fairly standard e-commerce scenario with paged lists of products within categories. For better or worse, about 80% of visitors never navigate past the first page, depending on the category there may then be 5-10 more pages of results which are viewed far less often. (Yes we do optimise what appears on the first page and have good search - but that's a different discussion)

We can't cache every single page of results, because we're constrained by memory, but the benefit of caching just the first page of results for each category would be huge.

I know I could do something similar using object caching to store the datasets in question, but is this possible using output caching, perhaps by using the response.Cache object?

Where in the page lifecycle could this be done? Pre-render?

Much simplified, the URL is something like "/ProductList?Category=something&Page=1" And I'd want logic something like (pseudocode):

If paramater "Page" equals 1
   Use output caching: vary by param = "categoryName; page"
else
   Don't use caching at all, just render the page from scratch.

We're using ASP.NET 2.0, on IIS 6/win2003.

like image 908
Andrew M Avatar asked Jun 25 '09 10:06

Andrew M


People also ask

How does output cache work?

The output cache enables you to cache the content returned by a controller action. That way, the same content does not need to be generated each and every time the same controller action is invoked. Imagine, for example, that your ASP.NET MVC application displays a list of database records in a view named Index.

Which namespace enables the caching parameters in ASP.NET MVC?

NET Framework, caching was available only in the System. Web namespace and therefore required a dependency on ASP.NET classes.

What is page output caching in asp net?

Output caching is an optimization that reduces Web server response time. Normally, when a browser requests an ASP.NET page, ASP.NET creates an instance of the page, runs any code in the page, runs database queries (if any), dynamically assembles the page, and then sends the resulting output to the browser.

Where is output cache stored?

The output cache is located on the Web server where the request was processed. This value corresponds to the Server enumeration value. The output cache can be stored only at the origin server or at the requesting client. Proxy servers are not allowed to cache the response.


4 Answers

Instead of using the OutputCache directive, you can do the same thing programmatically, as follows:

if (yourArbitraryCondition) {
  OutputCacheParameters outputCacheSettings = new OutputCacheParameters();
  outputCacheSettings.Duration = 60;
  InitOutputCache(outputCacheSettings);
}

Doing this from OnInit should work fine. And obviously, you can tweak the caching behavior by setting the various properties on the OutputCacheParameter, which has all the same knobs as the directive (in fact, that's what we generate when you use the directive).

The key point is that you're only executing this logic conditionally, while the directive makes it unconditional.

UPDATE:

As an alternative, you can use the low level cache API that the code above is built on. e.g.

HttpCachePolicy cache = Response.Cache;
cache.SetCacheability(HttpCacheability.Public);
cache.SetExpires(Context.Timestamp.AddSeconds(60));
cache.VaryByParams["categoryName"] = true;

Basically, it's another way of doing the same thing, without using any API's marked as 'should not be called'. In the end, either way will work, so take your pick.

like image 82
David Ebbo Avatar answered Sep 18 '22 09:09

David Ebbo


edit: I like David Ebbo's answer a lot better than my own.


You could use

<%@ OutputCache Duration="60"  VaryByParam="none" VaryByCustom="pageOne" %>

and implement it in a way that returns a fixed key for the first page and a random key for all other pages. You can (and should) let the scavenging mechanism take care of memory but you can use HttpResponse.RemoveOutputCacheItem to remove cache items if you must.

public override string GetVaryByCustomString(HttpContext ctx, string custom)
{
    if(custom == "pageOne")
    {
        if(ctx.Request["page"] == "1")
        {
            return "1";
        }

        HttpResponse.RemoveOutputCacheItem("/Default.aspx");
        return Guid.NewGuid().ToString();
    }
    return base.GetVaryByCustomString(ctx, custom);
}
like image 38
Josef Pfleger Avatar answered Sep 19 '22 09:09

Josef Pfleger


I believe the best way to do this is to use HttpCachePolicy.AddValidationCallback

See http://www.hanselman.com/blog/AdvancedASPNETCachingAndAddValidationCallBack.aspx - There's a full example that answers precisely this question.

like image 25
candrews Avatar answered Sep 20 '22 09:09

candrews


You can still use the outputcache directive, and in my opinion, rather than litter your page code with a bunch of caching nuts and bolts, you're better off going with a reusable solution based on handling this in Global.asax the way you normally would any VaryByCustom scenario.

So, for example, if you are using a paging approach with a repeater, you might simply in your search scenario want to exclude any postback on a particular page from the cache. Here is a code example that does just that. The approach merely requires using the HttpContext object to access Response.Cache.SetNoServerCaching(), after trapping whatever criteria for which you wish to avoid caching. I hope this helps.

like image 26
Iucounu Avatar answered Sep 17 '22 09:09

Iucounu