kamal hamidi
kamal hamidi

Reputation: 725

Remove OutputCache with multiple VaryByParam

I have a action like this:

    [OutputCache(Duration = 3600, Location = OutputCacheLocation.ServerAndClient, VaryByParam = "page;categor;searchString")]
    public virtual async Task<ActionResult> Index(int? category, int page = 1, string searchString = null)

This action can have multiple cache result. its fine so far. but when i post a new content, i like to remove all those multiple cache result.

I know i can use RemoveOutputCacheItem with a list of urls in a foreach or so..

var urls = new List<string>();
foreach (var url in urls)
{
   HttpResponse.RemoveOutputCacheItem(url);
}

But for that i have to make several connection to my database and get categories, pages and even if somehow i get all the search string, it's still cause too much trouble.

My question is, Is there a better way to remove all those cache at once?

Upvotes: 4

Views: 1180

Answers (1)

Rahul
Rahul

Reputation: 77886

Is there a better way to remove all those cache at once

Not that I know off. But You can remove cached item for a specific controller action method like below (assuming that your controller name is controller1). Idea taken from Clearing the ASP.Net MVC Cache

HttpResponse.RemoveOutputCacheItem(Url.Action("Index", "controller1", new { category = 1234  }));

Upvotes: 1

Related Questions