1

I have added some caching to my site but wish to create a method to clear the cache on one page. I have looked around and tried the following but none of them work:

HttpContext.Cache.Remove( Response.RemoveOutputCacheItem(Request.RawUrl); HttpResponse.RemoveOutputCacheItem(Request.RawUrl)

Does anyone have any idea how to do this or where I am going wrong? I have created my caching profiles in the web.config:

Funky
  • 12,890
  • 35
  • 106
  • 161

2 Answers2

0

Using the http method should work for this, are you sure you have linked the relevant URL/ file correctly in the method?

The RemoveOutputCacheItem will only work if you have entered in the correct route parameter. You cannot use queries for example in the method. Can you not update your question with the attempts you have made so far for a bit more clarity as to what is going wrong?

an example as to what you could is declare a route definition like this:

route.MapRoute(
                "default",
                "[controller]/[action]/[page_id]"
              ) 

Because this has now been declared you should be able to call it through:

    public ActionResult EmptyCache(Guid page_Id)
    {

        var url = Url.Action("SpecificPage", "default", new {   page_id=pageid   } );
        // the url will therefore be like this: /default/Specificpage/page_id
        HttpResponse.RemoveOutputCacheItem(url);
        return RedirectToAction("Index");
    }


Update

Make sure you are not using a method that therefore makes your current caching into a child action. For example if you use a method such as Html.RenderAction it will result in action after it being made into its children. These are cached in a different location and as a result the removeoutputcacheitem helper wouldn't be able to work.

Richard Cripps
  • 193
  • 1
  • 11
0

Visit : How to clear MemoryCache?

when you use [OutPutCacheAttribute]

you can use

List<string> cacheKeys = MemoryCache.Default.Select(kvp => kvp.Key).ToList();
foreach (string cacheKey in cacheKeys)
{
  MemoryCache.Default.Remove(cacheKey);
}
Community
  • 1
  • 1
wangengzheng
  • 6
  • 1
  • 1