清除 Web 服务 WebMethod 的 OutputCache

Clear the OutputCache of a web service WebMethod

我有一个 Web 服务包含使用 CacheDuration:

缓存的方法
[WebMethod(CacheDuration = 300)]
public bool CacheTest()
{
    return true;
}

在 ASP.NET MVC 项目中依次调用:

[HttpGet]
public IHttpActionResult Test()
{
    var cacheResult = _testWebService.Test();
    return Ok(cacheResult.ToString());
}

我希望能够从我的 Web 服务或 ASP.NET MVC 项目中清除 CacheTest 的缓存。

我研究过使用 HttpResponse.RemoveOutputCacheItem,但是,这似乎只清除了 Test 控制器方法的缓存,而不是 CacheTest Web 服务方法。

我也尝试过使用 HttpContext.Current.Cache.Remove(),但我没有 CacheTest 缓存的密钥。

一个选项是将 CacheDuration 减少 CacheTest(比如 10 秒)。然后将 CacheTest 更改为在内部使用 MemoryCache,缓存持续时间设置为 300(与现在一样)。

当您要从缓存中删除某个项目时,请将其从 MemoryCache 中删除。然后,在 10 秒内(或您将 CacheDuration 设置为的任何值)您的 Web 服务将反映任何最近的更改。