2

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?

ctrlplusb
  • 12,847
  • 6
  • 55
  • 57
  • I am wondering if a custom OutputCacheProvider may be a potential answer. Trying to find the source for the default provider... which is proving to be quite a difficult task, even with reflection. – ctrlplusb Sep 23 '13 at 19:50
  • Are your other pages also have the OutputCache directive present ?? – R.C Sep 24 '13 at 04:27
  • Well, everything except for the admin pages, which I am not worried about. But all the frontend gets rendered through Page.aspx. Page.aspx.cs reads the url and then fetches the appropriate page from the datastore and renders it. So all frontend URLs use the same aspx page for rendering. – ctrlplusb Sep 24 '13 at 09:12
  • You said Page.aspx.cs reads the url and then fetches the appropriate page. So this means there is the info inferred from QueryString ?? like ...aspx?id=5 – R.C Sep 24 '13 at 09:29
  • Not only querystring no, it is more to do with URL interpretation. All URLs are routed through and rendered via the same Page.aspx page. That is how Composite C1 works. This works well actually, but it's difficult to control caching because it is either on/off for all pages. :) – ctrlplusb Sep 24 '13 at 10:10

2 Answers2

1

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

Magnus Kragelund
  • 370
  • 2
  • 10
  • Are you 100% sure this won't cache? Wouldn't this still cache, just using the base custom string? I would have thought a method allowing the cache to be triggered or not would be returning a bool. – ctrlplusb Sep 30 '13 at 15:06
  • 1
    I've updated my answer to return null if the requested path should not be cached. As you mentioned, returning the base custom string did not do any good. – Magnus Kragelund Sep 30 '13 at 16:07
  • Awesome, thanks Magnus. I am just doing some thorough testing on this implementation to make sure that it will perform exactly as expected. I will mark this as the answer based on the results. – ctrlplusb Oct 01 '13 at 13:21
1

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

Dmitry Dzygin
  • 1,258
  • 13
  • 26