ctrlplusb
ctrlplusb

Reputation: 13137

Dynamically disable/enable outputcache for a page during runtime

I am using a CMS system, by the name of Composite C1. It renders all of it's content through single page (Page.aspx), which has a custom output cache profile attached.

This is all good, but I have run into a problem.

I want to have caching, but there are certain URLs that I would like to disable outputcaching for.

I know there is the varybycustom attribute that I can add to the cache profile, but I don't think this will give me exactly what I want. I want to be able to disable the cache completely when hitting specific URLs (or perhaps some other condition).

This seems to be very tricky as every page/url renders through the single Page.aspx file with it's outputcache profile defined.

Does anyone have any advice on how I might be able to solve this problem?

Upvotes: 2

Views: 1344

Answers (2)

Dmitry Dzygin
Dmitry Dzygin

Reputation: 1308

You can insert the following code to functions which are on the pages that shouldn't be cached. Or add a small function that would do just that:

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

Upvotes: 1

Magnus Kragelund
Magnus Kragelund

Reputation: 370

In \Global.asax you'll find an override of GetVaryByCustomString that calls into Composite C1 to evaluate if the response should be cached or not. You could intervene that, and only call into Composite if the request is not for one of the urls that you don't want cached:

if (context.Request.Url.AbsolutePath != "/dont-cache-this")
{
    return ApplicationLevelEventHandlers.GetVaryByCustomString(context, custom) ?? base.GetVaryByCustomString(context, custom);
}

return null;

Note that upgrading Composite C1 later on might replace \Global.asax and wipe your changes.

Also see http://msdn.microsoft.com/en-us/library/5ecf4420.aspx

Upvotes: 1

Related Questions