1

Using Sitecore 9.0.2, I've got code in two areas: SitecoreComponent (for controller renderings) and API (for JS calls). I've got a controller that inherits from SitecoreController in the API folder that I'm trying to apply the OutputCache attribute to, whether using a profile name or the parameters specifically in the attribute. This is also marked as HttpPost and called by an Ajax JQuery call to /api/[controller]/[method].

Whenever I make this call, either via the web application or Postman, I see in Fiddler or Postman that the cache-control is set to "no-cache". If I set up a VS debug against it, the method is hit, when it shouldn't need to be. So it appears the output caching is being bypassed/ignored.

Is this a scenario where the path needs to be added to the ignore path list, perhaps? If so, we'd probably need to institute a path other than "/api" since Sitecore can/does use that as well, I believe? Or is there something in Sitecore that overrides the OutputCache because it's taken over for use in the rendering caches and the like?

Code stub:

public class StockQuoteController : SitecoreController {
    private IStockQuoteAgent _stockQuoteAgent;

    public StockQuoteController(IStockQuoteAgent stockQuoteAgent) {
        _stockQuoteAgent = stockQuoteAgent;
    }

    [OutputCache(CacheProfile = "StockQuoteCacheProfile")]
    [HttpPost]
    public JsonResult GetStockPrice() {
        ...
    }
}

Cache definition (in web.config, inside system.web node):

<caching>
  <outputCache enableOutputCache="true" enableFragmentCache="true" sendCacheControlHeader="true" omitVaryStar="false"></outputCache>
  <outputCacheSettings>
    <outputCacheProfiles>
      <add name="StockQuoteCacheProfile" duration="1800" varyByParam="none" location="ServerAndClient" />
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>
  • Is this an asp dot.net core or the old asp.net? – Hugo Santos Jun 19 at 18:27
  • Can you post your Controller code snippet here? – Ahmed Okour Jun 19 at 20:54
  • @HugoSantos ASP.NET Framework (4.7.1) - I didn't think you could build renderings and the like off .NET Core for Sitecore? – Ken McAndrew Jun 20 at 19:45
  • @AhmedOkour I've updated with some code stubs. – Ken McAndrew Jun 20 at 19:45
  • @Ken Check in config if no-cache is set, postman will show no-cache for these settings..<configuration> <system.webServer> <httpProtocol> <customHeaders> <add name="Cache-Control" value="no-cache, no-store" /> <add name="Pragma" value="no-cache" /> <add name="Expires" value="-1" /> </customHeaders> </httpProtocol> </system.webServer> </configuration> – Umar D Jun 20 at 23:29
2

This is most likely down to the DisableBrowserCaching setting in Sitecore.

When set to true this setting issues no-cache headers which leaves Sitecore to optionally handle the caching of the page via several layers of caching. This is obviously useful when you want to harness the power of data sources and don't want the browser to cache pages with personalised content.

When set to false, the no-cache headers are not sent and the browser is free to cache. This is more suited to sites with purely static content but you miss out on some of the benefits of Sitecore's features.

From the documentation:

The DisableBrowserCaching setting in the web.config file controls whether Sitecore instructs clients to cache entire pages. When DisableBrowserCaching is true, Sitecore sets the following HTTP headers for each requested item (excluding media items):

Cache-Control: no-cache, no-store Pragma: no-cache

These HTTP headers instruct Web clients, including edge-caching devices, not to cache the page. Web clients can cache CSS, media, and other resources used by the page.

Note: If DisableBrowserCaching is true, Sitecore does not apply ASP.NET OutputCache directives in layouts.3 To instruct Web clients to cache entire pages, set DisableBrowserCaching to false, and use the ASP.NET OutputCache directive in layouts and sublayouts, or manipulate HTTP headers using the appropriate ASP.NET Application Programming Interfaces (APIs).

  • Thanks...just to note, this is now in Sitecore.config (since the documentation link is for 6.2), but I do see it set to true by default. Talk about a buried setting! – Ken McAndrew Jun 21 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.