How to use dynamic duration value in output cache?

I am using ASP.NET MVC3.
I used output caching according to the controller method.

[OutputCache(Duration = 3660, VaryByParam = "none")] public ActionResult Index() { some code; return View(); } 

I want to set dynamic duration using some static variable or something else in Output Caching.

How can i do this?

+6
source share
1 answer

I would inherit from the OutputCache attribute and set Duration there:

 public static class CacheConfig { public static int Duration = 36600; } public class MyOutputCacheAttribute : OutputCacheAttribute { public MyOutputCacheAttribute() { this.Duration = CacheConfig.Duration; } } [MyOutputCache(VaryByParam = "none")] public ActionResult Index() { return View(); } 

Then you can change the Duration dynamically and globally through CacheConfig.Duration

And you can still override the global setting for each action if you want:

 [MyOutputCache(Duration = 100, VaryByParam = "none")] public ActionResult OtherAction() { return View(); } 
+11
source

Source: https://habr.com/ru/post/914787/


All Articles