MGae2M MGae2M - 3 years ago 242
C# Question

How can I use [OutputCache (Duration=2000)] with variable duration value and How can reset Server catch? (in ASP.Net MVC by C#)

I have the code bellow and want

Duration
in
[OutputCache(Duration = 10)]
line that has variable value so that I can read it from
DB
or from a
List
Collection.

And I want been able to reset above cache immediately, when
Duration
had changed.

How can I made
Duration
varied, (For example readable from a
List
collection); and resetting cached
HTML
data, when
Duration
get changed; in code bellow?

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();
}
}
}

Answer Source

How to use dynamic duration value in Output Caching? (please credit original author)


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();
}

Edit:

You can reset the cache yourself.

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.

With .Net 3.5 you could only use InProc cache, with .NET 4 you can store your cache outside the process as well using custom CacheProviders...

REF: Obsolete MSDN Source: Caching Architecture Guide for .NET Framework Applications


Also check its being cached on the server: http://msdn.microsoft.com/en-us/library/system.web.ui.outputcachelocation.aspx

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download