I have the code below and want Duration
in [OutputCache(Duration = 10)]
line to have a variable value so that I can read it from DB
or from a List
Collection.
And I want be able to reset the server cache immediately, when Duration
had changed.
How can I make Duration
varied and reset cached HTML
data when the Duration
is changed? Here is my code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Cache_Example.Controllers
{
public class HomeController : Controller
{
// GET: Home
// [OutputCache(Duration = 10)]
public ActionResult Index()
{
return View();
}
[OutputCache(Duration = 10)]
public ActionResult ShowDate()
{
return PartialView();
}
}
}
To change the duration follow this:
How to use dynamic duration value in Output Caching? (please credit original author)
I would inherit from the
OutputCache
attribute and set theDuration
:
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 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();
}
Then you can reset the server cache immediately when Duration has changed yourself. Here's how:
"The ASP.NET cache object is located in the System.Web namespace, and because it is a generic cache implementation, it can be used in any application that references this namespace.
The System.Web.Caching.Cache class is a cache of objects. It is accessed either through the static property System.Web.HttpRuntime.Cache or through the helper instance properties System.Web.UI.Page and System.Web.HttpContext.Cache. It is therefore available outside the context of a request. There is only one instance of this object throughout an entire application domain, so the HttpRuntime.Cache object can exist in each application domain outside of Aspnet_wp.exe.
The following code shows how you can access the ASP.NET cache object from a generic application.
HttpRuntime httpRT = new HttpRuntime();
Cache cache = HttpRuntime.Cache;
After you access the cache object for the current request, you can use its members in the usual way."
REF: Obsolete MSDN Source: Caching Architecture Guide for .NET Framework Applications
Note: With .Net 3.5 you could only use InProc cache, with .NET 4 you can store your cache outside the process as well as using custom CacheProviders.
I want to emphasize this point, if the cache is supposed to last longer than the AppPool recycles (eg daily) then you need to cache Out-Of-Process.
Also check its being cached on the server: http://msdn.microsoft.com/en-us/library/system.web.ui.outputcachelocation.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With