AddTimestampToStaticLinks 慢吗?
T4MVC 有一个设置 AddTimestampToStaticLinks,它添加到 url 文件的上次更改时间:
/Content/nerd.jpg?2009-09-04T12:25:48
在开发过程中非常方便,因为浏览器不会缓存经常更改的文件。但我应该保留它用于生产吗?有多慢?我什至不确定它是如何工作的。谁能神奇地将“/Content/nerd.jpg?2009-09-04T12:25:48”转换为“/Content/nerd.jpg”?一些 IIS 模块?
T4MVC has a setting AddTimestampToStaticLinks which adds to the url file's last change time:
/Content/nerd.jpg?2009-09-04T12:25:48
It is very convenient during development as files which are often changed are not cached by the browser. But should I keep it for production? How slow is it? I even not sure how it works. Who does make the magic of converting "/Content/nerd.jpg?2009-09-04T12:25:48" to "/Content/nerd.jpg"? Some of IIS modules?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。它为您的用户提供与您在开发中获得的相同的好处。
我没有时间安排,但对于您的目的来说,它的速度可能快得可以忽略不计。它检查实际文件的最后修改日期,生成滴答计数差异的哈希值,并使用字符串连接将其附加到 URL。你可以自己检查一下,因为 t4 模板只是文本文件。它的代码在我的版本中看起来像这样:
如果它对于您的目的来说不够快,您可以修改将时间戳附加到对您来说足够快的东西的方法。不太精确但更快的方法就像程序集构建编号或在构建过程中手动更改的内容。然而,坚持使用默认值将使得只有已修改的文件才会被缓存失效。
使用 HTTP,您可以将查询字符串附加到任何请求。大多数服务器实现只是忽略静态文件的查询字符串,但是它们将不同的查询字符串视为单独的请求,因此在更新后立即获取更新。
即使您不更改文件,您也可以通过简单地将 url + 查询字符串更改为客户端未缓存的内容来强制浏览器重新加载内容。
Yes. It gives your users the same benefits you get in development.
I don't have timings, but it's probably negligibly fast for your purposes. It checks the last modified date on the actual file, generates a hash of the tick count difference, and uses string concatenation to append it to the url. You can inspect yourself because t4 templates are just text files. The code for it looks like this in the version I have:
If it is not fast enough for your purposes, you can modify the method which appends the time stamp to something fast enough for your. Less precise but faster methods would be like the assembly build number or something you change manually in the build process. Sticking with the default will make it so that only files which have been modified will be cache invalidated however.
With HTTP, you can append a query string to any request. Most server implementations simply disregard the query string for static files, however they treat different query strings as a separate requests, hence getting the updates as soon as they are made.
Even if you don't change the file, you can force browsers to reload the content by simply changing the url + query string to something the client does not have cached.