3

I try this code

public ActionResult RemoveCache(Guid product_Id)  
{  
    var url = Url.Action("ProductPreview", "Common", new { product_Id = product_Id });  
    HttpResponse.RemoveOutputCacheItem(url);  
    return RedirectToAction("Index");  
}  

to remove the output cache for a child action, but it does not work. Please help me, thanks!

Robert Anton Reese
  • 535
  • 1
  • 7
  • 17
  • Take a look at this answer http://stackoverflow.com/a/1169671/1342180 - specifically the part about setting `[OutputCache(Location = OutputCacheLocation.Server, ...)[`, on the action methods being cached, of course. – Alex Schimp Sep 27 '12 at 03:16
  • @AlexSchimp - that works for donut hole caching? – Adam Tuliper - MSFT Sep 27 '12 at 05:20
  • @AdamTuliper - I probably misunderstood the question. I thought the OP wanted the action cached initially, and was asking how to clear it later. – Alex Schimp Sep 27 '12 at 14:17
  • @Alex Schimp - you don't misunderstood the question. My problem is this. But it not work with a child action what called by Html.RenderAction. I found http://stackoverflow.com/questions/8968508/httpresponse-removeoutputcacheitem-is-not-working – Dương Tấn Thành Sep 27 '12 at 22:02

2 Answers2

3

Use the MVCDonutCaching nuget package at: http://mvcdonutcaching.codeplex.com/

See the section entitled "Usage" at: http://www.devtrends.co.uk/blog/donut-output-caching-in-asp.net-mvc-3

for removing items from the cache. There are Html.Action overloads with a param you add to force the donut hole (and thus exclude caching)

@Html.Action("Login", "Account", true)

The true above means 'don't cache this child action - create a donut hole'

Adam Tuliper - MSFT
  • 29,569
  • 4
  • 49
  • 68
  • can you give me a sample for store and remove cache a child action by donut caching? thankyou very much! – Dương Tấn Thành Sep 27 '12 at 07:15
  • i try use donut caching for child action. var cacheManager = new OutputCacheManager(); cacheManager.RemoveItem("Home", "List", new { page = 1 }); to remove cache, but it not removed! Please help me! – Dương Tấn Thành Sep 27 '12 at 22:41
  • it worked very good! thankyou very much! But it not work with a area. Ex: cacheManager.RemoveItem("Home", "List", new { area = "Forum", page = 1 }); NOT WORK! Can you help me? Thankyou very much! – Dương Tấn Thành Sep 28 '12 at 00:10
3

If it is accepted to clear the output cache for ALL chid actions, here is a way:

OutputCacheAttribute.ChildActionCache = new MemoryCache("NewRandomStringNameToClearTheCache"); 

NewRandomStringNameToClearTheCache should be a random string.

Source References: http://dotnet.dzone.com/articles/programmatically-clearing-0

user2173353
  • 3,852
  • 4
  • 39
  • 64