0

I have method:

    [HttpGet]
    [OutputCache(Duration = 300, Location = OutputCacheLocation.Client)]     
    public JsonResult TopNotification(Guid? portalIdentifier, string url)
    {
        var notificationMessages = new List<NotificationModel>();

        //Filling notification messages

        return Json(notificationMessages, JsonRequestBehavior.AllowGet);
    }

It is called from the base layout: @Html.Partial("TopNotification")

I need to rewrite my notificationMessages when certain action occurs before the duration of cache ends. I meen when User goes to certain URL I need to refill the messages.

  • 1
    http://stackoverflow.com/questions/16194140/how-to-invalidate-cache-data-outputcache-from-a-controller ? However: if you're caching ***at the client*** : no chance. You can't make clients forget, unless they also come back to the server to check for a 304 – Marc Gravell Oct 06 '14 at 10:07

1 Answers1

1

If you move caching at server side then you could use OutputCache's VaryByParam property to store cache version information. Then in Global.asax override GetVaryByCustomString and return the current version of the cache.

Every time the reset URL is visited, you can raise the version of the cache and thus invalidate output cache.

Example:

[OutputCache(Duration = 300, Location = OutputCacheLocation.Server, VaryByCustom = "cache")]
public JsonResult TopNotification(Guid? portalIdentifier, string url) {...}

And in Global.asax:

public override string GetVaryByCustomString(HttpContext context, string custom)
{
    if (custom.Equals("cache"))
        return Cache.Version; // something like that

    return base.GetVaryByCustomString(context, custom);
}

When the reset URL is visited, raise the version of the cache. All subsequent TopNotification calls will get the new data.

danielK
  • 36
  • 4