gpt4 book ai didi

asp.net-mvc-3 - MVC3 OutputCache 未按预期在服务器和客户端上工作

转载 作者:行者123 更新时间:2023-12-04 14:47:47 24 4
gpt4 key购买 nike

我在 Microsoft 的 MVC3 框架中使用 OutputCache 属性时遇到问题。

请想象以下 Controller 操作,它可以用作 AJAX 调用的一部分,以获取基于特定制造商 ID 的产品列表:

public JsonResult GetProducts(long manufacturerId)
{
return Json(this.CreateProductList(manufacturerId), JsonRequestBehavior.AllowGet);
}

我希望将此结果缓存在服务器上,以避免进行过多的数据库查询。我可以通过这样配置属性来实现这一点:
[OutputCache(Duration = 3600, Location = OutputCacheLocation.Server, VaryByParam = "manufacturerId")]

这按我的预期工作 - 浏览器发出初始请求,导致服务器创建和缓存结果,来自相同或不同浏览器的后续请求获得缓存版本。

但是...我也希望浏览器在本地缓存这些结果;如果我首先筛选制造商 X,然后 Y 然后返回 X,我不希望它再次请求 X 的产品 - 我希望它只使用其缓存版本。

我可以通过将 OutputCache 更改为以下内容来实现这一点:
[OutputCache(Duration = 3600, Location = OutputCacheLocation.Client)]

这里的问题是:我如何将这些结合起来,以便我可以拥有两组行为?我尝试将 Location 设置为 ServerAndClient 但这只是使它的行为与 Location 为 Server 时的行为相同。

我确定该问题与我使用 ServerAndClient 获得的“Vary: *”响应 header 有关,但我不知道如何摆脱它。

我欢迎对我的意图的合理性发表评论 - 我没有得到我期望的结果这一事实让我觉得我可能在某处有一些根本的误解......

非常感谢。

PS:这是在本地开发环境中,来自 VS2010 的 IIS Express。

最佳答案

您可以使用指定的 OutputCacheLocation.Any

The output cache can be located on the browser client (where the request originated), on a proxy server (or any other server) participating in the request, or on the server where the request was processed. This value corresponds to the HttpCacheability.Public enumeration value.



您可能还希望在这些请求的 HTTP header 中将 Cache-control public 设置为。

编辑

看来,根据您的 Web 服务器的 .Net 版本,您可能需要包含 Response.Cache.SetOmitVaryStar(true);在您的 Controller 操作中,按照您的建议删除 Vary * header 。

.Net 4 中原因的详细信息 breaking changes release notes .

关于asp.net-mvc-3 - MVC3 OutputCache 未按预期在服务器和客户端上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12052294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com