I’m encountering issues with output caching in my ASP.NET Core application. Here are the scenarios I’ve run into:
- Base Policy Override:
I’ve configured a basePolicy in my output caching options. However, when this basePolicy is added, it gets applied to every controller, including those where I’ve explicitly specified a NoCache policy. This is causing unwanted caching behavior on controllers that should not be cached. - No Policies Applied Without Base Policy:
When I remove the basePolicy, none of my other policies are applied, not even the SearchData policy that I have configured for a specific controller. It’s as if the caching mechanism stops working entirely without the basePolicy.
outputCache options:
builder.Services.AddOutputCache(options =>
{
options.AddBasePolicy(builder => builder.Expire(TimeSpan.FromSeconds(10)));
options.AddPolicy("SearchDataCache", builder => builder.Expire(TimeSpan.FromMinutes(1)).Tag("CatalogSearchData"));
options.AddPolicy("NoCache", builder => builder.NoCache());
});
Controllers:
[HttpGet]
[OutputCache(PolicyName = "SearchDataCache")]
public async Task<ActionResult<ApiResponse<SearchResponse>>> GetSearchData()
{
// some db call
}
[Route("RefreshSearchData")]
[HttpGet]
[OutputCache(PolicyName = "NoCache")]
public async Task<ActionResult<ApiResponse<string>>> RefreshSearchData(IOutputCacheStore cacheStore)
{
await cacheStore.EvictByTagAsync("CatalogSearchData", default);
return Ok(new ApiResponse<string>(true, "Success", null));
}
Has anyone encountered similar issues, or can someone explain how to properly configure output caching so that the basePolicy doesn’t override specific policies like NoCache, and policies are applied correctly even when a basePolicy is not present?
I tried removing base policy and applying specific policies.