【问题标题】:How to use: HttpResponse.RemoveOutputCacheItem when using varyByCustom使用方法:使用varyByCustom时的HttpResponse.RemoveOutputCacheItem
【发布时间】:2017-03-02 09:15:34
【问题描述】:

我已经使用 Azure Redis 服务实现了 Redis OutputCache (Microsoft.Web.RedisOutputCacheProvider)。

我发现了here 和here 类似的未回答/未解决的问题。

上面需要缓存的页面: <%@ OutputCache VaryByParam="*" Duration="600" %>

访问随机页面后,我检查 Redis 数据库中的密钥是否已保存,例如:/_a2/monitor.aspx

所以运行下面的代码行,将删除这个缓存项(并且它起作用了)。

HttpResponse.RemoveOutputCacheItem("/monitor.aspx", "RedisOutputCache")

现在我更新了相同的页面,添加了 vSphereByCustom 和 OutputCache 指令: <%@ OutputCache VaryByParam="*" varyByCustom="userhash" Duration="600" %>

现在访问该页面并检查 Redis 数据库中的密钥,它保存为:/_a2/monitor.aspxHQFCNuserhashVcc6ef5b7173286704cef942d5577b88bd81f2cce71a0dcdc8676d3a815e68b59DE

您会看到添加的 userhash 哈希值,这很好并且按预期工作。

但是现在问题来了:如何清除这个缓存项。

这不起作用:

`HttpResponse.RemoveOutputCacheItem("/monitor.aspx", "RedisOutputCache")`

还尝试过: HttpResponse.RemoveOutputCacheItem("/monitor.aspx?userhash=cc6ef5b7173286704cef942d5577b88bd81f2cce71a0dcdc8676d3a815e68b59", "RedisOutputCache")

并且还尝试过: HttpResponse.RemoveOutputCacheItem("/monitor.aspx {userhash:cc6ef5b7173286704cef942d5577b88bd81f2cce71a0dcdc8676d3a815e68b59}", "RedisOutputCache")

还尝试结合上述代码使用以下一些选项:

Response.Cache.SetVaryByCustom("userhash")
Response.AddCacheItemDependency("action")
HttpContext.Current.Cache.Item("action") = "test"
Response.Cache.VaryByParams("userhash") = True
HttpResponse.RemoveOutputCacheItem("/monitor.aspx")

有没有办法在使用 varyByCustom 选项时删除此缓存项?

我不使用 system.web.mvc 对象。所以我不能为 URL 助手访问这个对象。

欢迎任何帮助!

乔尔

【问题讨论】:

  • 这个问题你解决了吗,有更新吗?

标签: asp.net redis outputcache azure-redis-cache


【解决方案1】:

根据您的描述,我尝试实现我的OutputCacheProvider 将缓存存储到文件中以测试我这边的这个问题。这是我的测试结果,你可以参考一下。

OutputCache 我的 Monitor.aspx

<%@ OutputCache VaryByParam="None" Duration="60" Location="Server" %>

访问上述页面时,我会从我的 OutputFileCacheProvider 中获得以下日志,如下所示:

在我访问另一个 .aspx 以清除缓存后,当前的 DateTime 字符串将在我的 monitor.aspx 页面中刷新。

<%@ OutputCache VaryByParam="*" Duration="60" VaryByCustom="userhash" Location="Server" %>

注意:调用HttpResponse.RemoveOutputCacheItem("/monitor.aspx")从另一个.aspx端点清除缓存的方法相同。

VaryByParam 和 varyByCustom 都可以按我的预期工作。我假设您可以在 OutputCacheProvider 中添加日志并尝试查找是否遗漏了某些内容。

【讨论】:

  • 嗨,布鲁斯,感谢您的回复。我不确定 Microsoft.Web.RedisOutputCacheProvider 是否具有调试日志记录选项。但我注意到一些事情。在第二张图片中,您使用 VaryByCustom="userhash" 参数的缓存项仍未删除。它删除了用户哈希中的项目。所以我认为这是 RedisOutputCacheProvider 中没有提供的东西。谢谢!乔
  • 您是否尝试在使用 VaryByCustom 时将 VaryByParam 更改为 None,如下所示:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
  • 2014-02-13
  • 2010-09-13
  • 2013-10-12
  • 1970-01-01
  • 2018-02-09
  • 2011-05-10
相关资源
最近更新 更多