gpt4 book ai didi

asp.net-mvc - 利用 Azure 上的 MVC 站点的浏览器缓存

转载 作者:行者123 更新时间:2023-12-02 07:48:49 25 4
gpt4 key购买 nike

我正在使用 gtmetrix.com 来检查我的小网站的性能。网站并不是什么关键的东西。我只是想更多地了解性能优化。我需要优化的最后一件事是浏览器缓存。我已经阅读并尝试了不同的东西,但无论如何我仍然收到这些警告。利用浏览器缓存以下可缓存资源:

enter image description here

它是托管在 Azure 上的全新 MVC 站点。在我的 web.config 中,我添加了以下内容:

<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="CacheFor60Seconds" duration="360" location="Any"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>

<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="3.00:00:00" />
</staticContent>

我的 Controller 操作如下所示

    [OutputCache(CacheProfile = "CacheFor60Seconds")]
public ActionResult Index()
{
return View();
}

据我所知,资源将在 2 小时或 3 天后过期。为什么这还不够好?使用其他性能检查工具时,我得到了相同的结果。

最佳答案

您正在将 MVC Controller 操作的结果(即 View )缓存 60 秒。这些警告都是关于静态内容的,您似乎正在使用 clientCache 元素来解决这些问题。

首先想到的是使用 Azure CDN用于缓存和更快的交付。

使用 clientCache element of the staticContent element 缓存静态内容web.config 中的内容应该可以正常工作,但众所周知,它并不总是能兑现 promise 。通常,这是一个配置问题。请阅读我链接到的文章,因为它有类似的提示和技巧

To use the httpExpires attribute, you need to set the value of the cacheControlMode attribute to UseExpires

The value for the httpExpires attribute must be a fully-formatted date and time that follows the specification in RFC 1123

为什么这还不够好?

为什么这不够好这个问题可能可以由 gtmetrix.com 的人们最好地回答,因为这是他们的阈值。我可以想象他们有一些提示和技巧来提高您的结果?

您可能需要增加 MaxAge 来缓存静态内容并向静态内容添加(虚构的)版本号,例如 some_stylesheet.css?ver=3。通过这种方式,它可以缓存更长的时间并且您可以通过增加版本号来强制浏览器尽快获取文件的新版本,例如 some_stylesheet.css?ver=4 .

这里有一篇由 Mads Kristensen 撰写的有趣文章,其中使用了指纹识别:Cache busting in ASP.NET

关于asp.net-mvc - 利用 Azure 上的 MVC 站点的浏览器缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46129691/

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