7

Is there a was to set the duration of caching in the web.config for MVC4 .net pages? I have :

[OutputCache(Duration = Convert.ToInt32(ConfigurationManager.AppSettings["cache.eventPage"]), VaryByParam = "Id")]
public ActionResult....

Which will not compile because

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

We have very spikey traffic and would like to be able to change this value very quickly with out pushing out a new build. Is this possible?

p.campbell
  • 98,673
  • 67
  • 256
  • 322
Nick Van Brunt
  • 15,244
  • 11
  • 66
  • 92

1 Answers1

16

You can use OutputCache profiles; define a section in web.config

<system.web>
 <caching>
  <outputCacheSettings>
    <outputCacheProfiles>
       <add name="CacheProfile1" duration="10" />  <!--10 seconds -->
       <add name="CacheProfile2" duration="3600" /> <!--one hour-->
       <add name="CacheProfileNone" duration="0" /> <!--disabled-->
    </outputCacheProfiles>
  </outputCacheSettings>
 </caching>
</system.web>

Use it on your controller action methods via an attribute as you've done already. Just use the CacheProfile property.

[OutputCache(CacheProfile = "CacheProfile1",VaryByParam = "Id")]

You can create different profiles for each caching scenario that you have.

More info on caching at MSDN

ono2012
  • 4,967
  • 2
  • 33
  • 42
Overmachine
  • 1,723
  • 3
  • 15
  • 27
  • This is how I ended up doing it. I am wondering if there is a more programatic way of accessing these values. This may be the only answer though. – Nick Van Brunt Jul 03 '13 at 14:44
  • 1
    Bounty will be awarded tomorrow for this, saved me a lot of time. –  Apr 24 '17 at 09:27