2

I am using OutputCaching on my Index page where a list of employee records are displayed

[OutputCache(Duration = 120, VaryByParam = "empId;page;sort;")]

But when I delete one record from the list , because of caching ,the deleted record is also is getting listed until the Outputcache expires . Is there a way I can refresh the outputcache if a delete operation happens ?. I am using this on my MVC3 razor pages.

I have 100's of records listed on my page and each record has a "Delete" button to delete that particular record.I am also using OutputCaching on the page so that the page loads faster.

Now if I delete one record by hitting the delete button ,due to OutputCaching ,even the deleted record is also shown on the page until the outputcache expires. Is there a way ,I can get around with this problem?

Is there a way,I can just remove only the deleted record from my outputCache?

or If am adding a new record ,I don't want to delete the OutputCache and recreate theCache again,instead i want append my new record to existing outputCache ?

Millar
  • 1,095
  • 6
  • 16
  • 34
  • http://stackoverflow.com/questions/1167890/how-to-programmatically-clear-outputcache-for-controller-action-method – Shyju Aug 01 '12 at 18:42

2 Answers2

0

Could you try appending a querystring parameter to your url that will break the cache when you delete a record? This is a common approach that I use for breaking client cache for ajax get requests.

Then just add that querystring parameter to your VaryByParam list. I just tested this locally and was able to force outputcaching to break when sending a different value in my param which was deletedRecordId.

So whenever you've finished removing the item and are about to redirect back to the list action just make sure to pass the parameter in the route values if you are using RedirectToAction then just create an annonymous object for route values to hold your querystring parameter for cache busting. It will look something similar to this:

return RedirectToAction("list", "category", new { deletedRecordId = deletedRecordId });
JustinMichaels
  • 1,092
  • 7
  • 12
  • Is there a way,I can just remove only the deleted record from my outputCache? – Millar Aug 01 '12 at 20:07
  • I want to start off by saying in no way am I an expert in asp.net mvc outputcache at the server level. Since you just want to show the updated list without that record you're going to need to regenerate the html based on what is now valid. I understand what you are saying about just removing that one piece from the cached version that you have already but I honestly don't know if that's possible. I hardly ever use OutputCache on a dynamic page and when I have it's for a high traffic page. I've provided a way for your action to be hit again and your page to reflect the delete. – JustinMichaels Aug 01 '12 at 20:20
0

In controller or page load code use:

HttpContext.Cache.Insert("Record"+ recordid, 1);
Response.AddCacheItemDependency("Record"+ recordid);

In record delete method use

    HttpContext.Cache.Remove("Record"+ recordid);
Andrus
  • 26,339
  • 60
  • 204
  • 378