Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use dynamic duration value in Output Caching?

I'm using ASP.NET MVC3.
I've used Output Caching on controller method.

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

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

How can i do this?

like image 831
Dharmik Bhandari Avatar asked May 03 '12 07:05

Dharmik Bhandari


1 Answers

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

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 trough the CacheConfig.Duration

And you can still override the global setting on every action if you want:

[MyOutputCache(Duration = 100, VaryByParam = "none")]
public ActionResult OtherAction()
{
    return View();
}
like image 147
nemesv Avatar answered Sep 30 '22 20:09

nemesv