5

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();
        }
    }
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
mgae2m
  • 1,134
  • 1
  • 14
  • 41
  • you can not, you would need to implement your own cache, with your requirement built around it – Seabizkit Oct 01 '17 at 14:24
  • -@Jeremy Thompson, why in edit heading of question: `reset Server catch? (in ASP.Net MVC by C#)`, the `(in ASP.Net MVC by C#)` was deleted? – mgae2m Oct 02 '17 at 06:42
  • 1
    Hi, there a good reason why I did that: https://meta.stackexchange.com/q/19190/156316, Google & etc read the tags so when you Google you just prefix your search with the tags. It's a heaps better way than free-text. I removed the server **catch**, because it's is spelled *cache*. – Jeremy Thompson Oct 02 '17 at 07:02
  • -@Jeremy Thompson, Regard and thanks, I meant that `(in ASP.Net MVC by C#)` why removed? – mgae2m Oct 02 '17 at 07:13
  • `(in ASP.Net MVC by C#)` is better specified in tags (indexed by search engines) than free-text in a title. – Jeremy Thompson Oct 02 '17 at 07:22
  • Understand. Thankful. – mgae2m Oct 02 '17 at 07:24
  • You can create your own functionality by playing with source :P (WARNING: You may spoil your life) https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Mvc/OutputCacheAttribute.cs – G J Oct 05 '17 at 14:40

1 Answers1

7

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 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 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

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • 1
    With regard, Please note that, The above original question contains addition matter than [How to use dynamic duration value in Output Caching?](https://stackoverflow.com/questions/10426843/how-to-use-dynamic-duration-value-in-output-caching); revise your question and don't copy another; with pay attention (How can reset Server catch?) part of original question. – mgae2m Oct 02 '17 at 01:45
  • 2
    Unless i'm missing something, this is not what OP wants. This just sets a global default for cache, but can't be changed dynamically. CacheConfig.Duration will be copied to each instance of MyOutputCacheAttribute, and updating it will not take effect. – Ortiga Oct 02 '17 at 14:22