简体   繁体   English

ASP.NET应用程序中的浏览器缓存

[英]Browser Caching in ASP.NET application

Any suggestions on how to do browser caching within a asp.net application. 有关如何在asp.net应用程序中进行浏览器缓存的任何建议。 I've found some different methods online but wasn't sure what would be the best. 我在网上找到了一些不同的方法,但不确定哪种方法最好。 Specifically, I would like to cache my CSS and JS files. 具体来说,我想缓存我的CSS和JS文件。 They do change, however, it is usually once a month at the most. 它们确实会发生变化,但通常最多每月一次。

Another technique is to stores you static images, css and js on another server (such as a CDN ) which has the Expires header set properly. 另一种技术是将静态图像,css和js存储在另一个正确设置了Expires标头的服务器(例如CDN )上。 The advantage of this is two-fold: 这样做的好处是双重的:

  1. The expires header will encourage browsers and proxies to cache these static files expires头将鼓励浏览器和代理缓存这些静态文件
  2. The CDN will offload from your server serving up static files. CDN将从服务器卸载静态文件。
  3. By using another domain name for your static content, browsers will download faster. 通过为静态内容使用其他域名,浏览器将更快下载。 This is because serving resources from four or five different hostnames increases parallelization of downloads . 这是因为从四个或五个不同的主机名提供资源会增加下载的并行化
  4. If the CDN is configured properly and uses cookieless domain then you don't have unnecessary cookies going back and forth. 如果CDN配置正确并使用无cookie域,那么您就没有不必要的cookie来回传递。

Its worth bearing in mind that even without Cache-Control or Expires headers most browsers will cache content such as JS and CSS. 值得注意的是,即使没有Cache-Control或Expires标头,大多数浏览器也会缓存JS和CSS等内容。 What should happen though is the browser should request the resource every time its needed but will typically get a "304 Unmodified" response and the browser then uses the cached item. 应该发生的事情是浏览器应该在每次需要时请求资源,但通常会得到“304未修改”响应,然后浏览器使用缓存项。 This is can still be quite costly since its a round trip to the server but the resource itself isn't sent so the bytes transfered is limited. 这可能仍然非常昂贵,因为它往返服务器但资源本身没有被发送,因此转移的字节是有限的。

IE left with no specific instructions regarding caching will by default use its own heuristics to determine if it should even bother to re-request an item its cached. IE没有关于缓存的具体说明,默认情况下会使用自己的启发式方法来确定是否应该重新请求缓存的项目。 This despite not being explicitly told that it can cache a resource. 尽管没有明确告知它可以缓存资源。 Its hueristics are based on the Last-Modified date of the resource, the older its the less likely it'll have changed now is its typical reasoning. 它的hueristics基于资源的Last-Modified日期,它越老,它现在改变的可能性就越小,这是它的典型推理。 Very wooly. 非常愚蠢。

Frankly if you want to make a site perfomant you need to have control over such cache settings. 坦率地说,如果你想建立一个站点性能,你需要控制这样的缓存设置。 If you don't have access to these settings then don't wouldn't worry about performance. 如果您无权访问这些设置,则不必担心性能问题。 Just inform the sponsor that it may not perform well because they haven't facilitated a platform that lets you deliver that. 只需告知赞助商它可能效果不佳,因为它们没有促成一个可以让您实现这一目标的平台。

You best bet to do this is to set an Expires header in IIS on the folders you want the content cached. 最好的办法是在IIS中为要缓存内容的文件夹设置Expires标头。 This will tell most modern browsers and proxies to cache this static content. 这将告诉大多数现代浏览器和代理缓存此静态内容。 In IIS 6: 在IIS 6中:

  1. Right click on the folder (example CSS or JS) you want to be cached by the browser. 右键单击要由浏览器缓存的文件夹(示例CSS或JS)。
  2. Click properties 单击属性
  3. Go to the HTTP Headers Tab 转到HTTP标头选项卡
  4. Check "Enabled content expiration" 检查“已启用内容过期”
  5. Set some long period for expiration, like "Expires after 90 days" 设置一段很长的到期时间,例如“90天后过期”

Yahoo Developer's Blog talks about this technique . 雅虎开发者博客谈论这种技术

You can cache static content by adding following code in web.config 您可以通过在web.config中添加以下代码来缓存静态内容

 <system.webServer>
    <staticContent>
      <clientCache httpExpires="Tue, 12 Apr 2016 00:00:00 GMT" cacheControlMode="UseExpires" />
    </staticContent>
</system.webServer>

See the clientCache documentation for more details. 有关更多详细信息,请参阅clientCache文档

Unless you configure IIS to give asp.net control of js/css/image requests it won't see them by default, hence your best plan (for long term maintainability) is to deliberately tweak the response headers at your firewall/trafficmanager/server or (better and what most of the world does at this point) to version your files in path, ie: 除非您将IIS配置为对js / css / image请求进行asp.net控制,否则默认情况下不会看到它们,因此您的最佳计划(长期可维护性)是故意调整防火墙/ trafficmanager / server上的响应头或者 (更好,以及世界上大多数人在这一点上做了什么) 在路径中对文件进行版本化即:

Instead of writing this in your mark-up: 而不是在你的加价中写这个:

http://www.foo.com/cachingmakesmesad.css

Use this: 用这个:

http://www.foo.com/cachingmakesmesad.css?v1

..and change the version number when you need to effectively clear cache. ..并在需要有效清除缓存时更改版本号。 If that's every time then you could even append a GUID or datestamp instead, but I can't think of any occasion where I would want to do that really. 如果那是每次,那么你甚至可以添加一个GUID或日期戳,但我想不出任何我想要真正做到这一点的场合。


I thought your question was anti-cache but re-reading it I see I wasted a good answer there :P 我认为你的问题是反缓存但重新阅读它我看到我在那里浪费了一个很好的答案:P

Long story short, browsers are normally very aggressively pro-caching "simple" resources so you shouldn't have to worry about this, but if you did want to do something about it you would have to have access to a firewall/trafficmanager/IIS for the reasons above (ASP.NET won't be given a chance by default). 简而言之,浏览器通常非常积极地支持“简单”资源,因此您不必担心这一点,但如果您确实想要对其执行某些操作,则必须能够访问防火墙/流量管理器/ IIS由于上述原因(默认情况下ASP.NET不会有机会)。

However... there is no way you can absolutely force caching, and nor should you. 但是......你绝对不能强制缓存,也不应该。 What is and isn't cached is rightfully the decision of the end-user, all you can do is strongly request. 什么是和不是缓存是最终用户的决定,你所能做的就是强烈要求。

In .net you can set up your JavaScript, CSS and Images as embedded resources. 在.net中,您可以将JavaScript,CSS和图像设置为嵌入式资源。 .Net will then handle the file expiration for you. 然后.Net将为您处理文件过期。 The downside to this approach is that you have to do a new build for each set of changes (this might be an upside, depending on your deployment and workflow). 这种方法的缺点是您必须为每组更改执行新的构建(这可能是一个好处,具体取决于您的部署和工作流程)。

You could also use ETags, but from what I understand in some cases it doesn't work well if you have mix of IIS and apache Webservers hosting your images , (or if you plan to switch in the future). 您也可以使用ETag,但根据我的理解,在某些情况下,如果您混合使用IIS和承载图像的Apache Web服务器 ,(或者如果您计划将来切换),则无法正常工作。

You can just make sure the file date is newer, and let the server handle it for you, but you've got to make sure the server is configured correctly. 您可以确保文件日期更新,让服务器为您处理,但您必须确保服务器配置正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM