Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET OutputCache varyByParam does not work in Web.config

I am trying to cache some of my expensive to generate charts. So I did this in Web.config:

<caching>
  <outputCacheSettings>
      <outputCacheProfiles>
          <!-- 4 hours : 60 sec x 60 min x 4 hour = 14400 sec -->
          <add name="ChartCacheProfile" duration="14400" varyByParam="none" />
      </outputCacheProfiles>
  </outputCacheSettings>
</caching>  

And I added this in my Controller:

[OutputCache(CacheProfile="ChartCacheProfile")]
public ActionResult GenerateChart()

But this does not work..., the result is still not cached and the Action is being executed always. This can take up to 1 minutes to complete.

Please note that the url being called with different parameter every time. The parameter is unrelated to the chart being generated. That is why I put varyByParam="none".

like image 495
Rosdi Kasim Avatar asked May 20 '14 16:05

Rosdi Kasim


2 Answers

I suspect it is a bug indeed. What worked for me is to set VaryByParam explicitly in the OutputCache attribute:

[OutputCache(CacheProfile="ChartCacheProfile", VaryByParam="None")]
public ActionResult GenerateChart()
like image 196
haim770 Avatar answered Sep 18 '22 13:09

haim770


I've got tired of this issue and having to set varyByParam for each action. So here's simple code for output cache attribute that gets varyByParam from web.config. https://github.com/unconnected4/MvcOutputCacheFix/blob/master/ParameterizedOutputCacheAttribute.cs

like image 36
unconnected Avatar answered Sep 17 '22 13:09

unconnected