How can I use [OutputCache (Duration=2000)] with variable duration value and reset Server cache
Asked Answered
E

1

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();
        }
    }
}
Entitle answered 4/8, 2017 at 2:27 Comment(7)
you can not, you would need to implement your own cache, with your requirement built around itTriode
-@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?Entitle
Hi, there a good reason why I did that: 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.Administrate
-@Jeremy Thompson, Regard and thanks, I meant that (in ASP.Net MVC by C#) why removed?Entitle
(in ASP.Net MVC by C#) is better specified in tags (indexed by search engines) than free-text in a title.Administrate
Understand. Thankful.Entitle
You can create your own functionality by playing with source :P (WARNING: You may spoil your life) github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/…Mcdermott
A
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

Administrate answered 2/10, 2017 at 0:32 Comment(2)
With regard, Please note that, The above original question contains addition matter than 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.Entitle
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.Libbi

© 2022 - 2024 — McMap. All rights reserved.