AddTimestampToStaticLinks 慢吗?

发布于 2024-11-30 23:02:52 字数 287 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当爱已成负担 2024-12-07 23:02:52

我应该保留它用于生产吗?

是的。它为您的用户提供与您在开发中获得的相同的好处。

有多慢?我什至不确定它是如何工作的。

我没有时间安排,但对于您的目的来说,它的速度可能快得可以忽略不计。它检查实际文件的最后修改日期,生成滴答计数差异的哈希值,并使用字符串连接将其附加到 URL。你可以自己检查一下,因为 t4 模板只是文本文件。它的代码在我的版本中看起来像这样:

static DateTime CenturyBegin=new DateTime(2001,1,1);
public static string TimestampString(string virtualPath) {
    if (!HostingEnvironment.IsHosted) return string.Empty;
    string filePath = HostingEnvironment.MapPath(virtualPath);
    return Convert.ToString((System.IO.File.GetLastWriteTimeUtc(filePath).Ticks-CenturyBegin.Ticks)/1000000000,16);            
}

如果它对于您的目的来说不够快,您可以修改将时间戳附加到对您来说足够快的东西的方法。不太精确但更快的方法就像程序集构建编号或在构建过程中手动更改的内容。然而,坚持使用默认值将使得只有已修改的文件才会被缓存失效。

是谁创造了将“/Content/nerd.jpg?2009-09-04T12:25:48”转换为“/Content/nerd.jpg”的魔力?

使用 HTTP,您可以将查询字符串附加到任何请求。大多数服务器实现只是忽略静态文件的查询字符串,但是它们将不同的查询字符串视为单独的请求,因此在更新后立即获取更新。

即使您不更改文件,您也可以通过简单地将 url + 查询字符串更改为客户端未缓存的内容来强制浏览器重新加载内容。

Should I keep it for production?

Yes. It gives your users the same benefits you get in development.

How slow is it? I'm not even sure how it works.

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:

static DateTime CenturyBegin=new DateTime(2001,1,1);
public static string TimestampString(string virtualPath) {
    if (!HostingEnvironment.IsHosted) return string.Empty;
    string filePath = HostingEnvironment.MapPath(virtualPath);
    return Convert.ToString((System.IO.File.GetLastWriteTimeUtc(filePath).Ticks-CenturyBegin.Ticks)/1000000000,16);            
}

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.

Who does make the magic of converting "/Content/nerd.jpg?2009-09-04T12:25:48" to "/Content/nerd.jpg"?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文