2

I started using OutputCache for my website. The problem that I encounter is that when a user update an item I need to reset the cache for that item.

I did that using:

var urlToRemove = Url.Action("Details", "Dress", new {id = model.Id});
Response.RemoveOutputCacheItem(urlToRemove);

In Edit action I also set to TempData the update success message and I display it on the next request. The problem is that the message remains in the cached response.

Do you know how can I avoid caching in an action. Something like:

[OutputCache(Duration = 3600, VaryByParam = "id")]
public ViewResult Details(int id)
{  
  if(NotificationHelper.HasNotifications)
    Response.DoNotCache();
    .....

I cannot use the same trick ... because the page is added to the cache after its rendered. So I cannot exclude an action from cache in its body.

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
Radu D
  • 3,505
  • 9
  • 42
  • 69

3 Answers3

2

What you are describing is sometimes referred as "Donut Hole Caching" because you want to cache everything except some bit of dynamic content in the middle.

Here is are a couple resources you might want to look at:

Erv Walter
  • 13,737
  • 8
  • 44
  • 57
0

what about this?

class CustomOutputCacheAttribute : OutputCacheAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.HttpContext.Response.StatusCode >= 400)
        {
            this.Location = System.Web.UI.OutputCacheLocation.None;
            this.Duration = -1;
        }

        filterContext.HttpContext.Response.Cache.SetOmitVaryStar(true);
        base.OnActionExecuted(filterContext);
    }

}
gustavomick
  • 115
  • 1
  • 9
0

I don't know if there is an easy way to do what you are asking. However, my experience with messages and OutputCache is don't put them in the response. I ended up making sure there were no messages that are ever displayed on a cached page. If I absolutely had to have a message on a cached page I would set up an ajax call that would grab the messages once the response reached the client.

GavinW
  • 36
  • 4
  • Erv's suggestion of donut holing the message is also a good idea if you are able to integrate a third party caching library. – GavinW Feb 07 '12 at 21:37