我已经使用 Azure Redis 服务实现了 Redis OutputCache (Microsoft.Web.RedisOutputCacheProvider)。
我发现了类似的未回答/ Unresolved 问题 here和 here .
在需要缓存的页面之上:
<%@ OutputCache VaryByParam="*" Duration="600" %>
访问随机页面后,我检查 Redis 数据库中的 key 是否已保存为:/_a2/monitor.aspx
因此运行下面的代码行,将删除此缓存项(并且有效)。
HttpResponse.RemoveOutputCacheItem("/monitor.aspx", "RedisOutputCache")
现在我更新了同一页面,添加了 varyByCustom 和 OutputCache 指令:
<%@ OutputCache VaryByParam="*" varyByCustom="userhash" Duration="600" %>
现在访问页面并检查 Redis 数据库中的 key ,它保存为:/_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 助手的这个对象。
欢迎任何帮助!
乔尔
最佳答案
根据您的描述,我尝试实现我的 OutputCacheProvider将缓存存储到文件中以在我这边测试这个问题。这是我的测试结果,你可以引用。
我的 Monitor.aspx 的OutputCache
<%@ 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 中添加日志并尝试查找您是否遗漏了什么。
关于asp.net - 使用 varyByCustom 时如何使用 : HttpResponse. RemoveOutputCacheItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42551182/