给定以下可能性,最高效的ETag生成是什么?

huangapple go评论90阅读模式
英文:

What's the most efficient ETag generation given the following possibilities?

问题

我最近一直在使用Go来缓存HTTP响应,并且我正在尝试找出生成正确ETag的最有效方法。

目前我有以下可用的数据:

  1. 当我渲染模板时,模板的名称。
  2. 传递给模板或JSON响应的动态数据。
  3. 整个响应体。
  4. 响应体的长度。
  5. 我可能遗漏的其他内容?

经过一些思考,我得出结论,如果我将模板的名称和生成的动态数据组合起来,理论上应该可以创建一个合法的唯一ETag,并且开销最小。但是,如果我想返回大约30kb的HTML数据库结果,我不知道这样做会有多复杂。

我正在使用Go的标准库中的crc32例程来从传递给它的数据生成ETag。

是否有更好的方法来生成ETag,甚至缓存动态数据?我不能仅仅监视文件的最后修改时间,因为数据可能在文件未更改的情况下发生变化。

英文:

I've been fooling around with caching http responses lately in Go and I'm trying to figure out the most efficient way possible to generate proper ETags.

Right now I have the following data available:

  1. The name of a template being rendered for when I ever render templates.
  2. The dynamic data being passed into a template or response for json responses.
  3. The entire response body.
  4. The length of the body.
  5. Something that I might be missing?

After some thinking I came to the conclusion that if I combine the name of the template and the dynamic data being produced this should in theory create a legit unique ETag with the least amount of overhead but I don't know how nasty this will get if I start wanting to return like 30kb of html worth of database results.

I'm using a crc32 routine from Go's stdlib to generate the ETag from the data I pass into it.

Is there a better way to generate ETags, or even cache dynamic data? I can't just monitor the last-modified time of a file because the the data can change without the file changing.

答案1

得分: 1

一般来说,你希望使用一些计算成本较低的东西作为ETag。这是因为如果客户端发送了一个条件请求(例如通过If-None-Match HTTP请求头),你可以决定是否适合发送一个304 Not Modified响应,而无需对页面进行所有处理。

例如,如果你有某种页面内容的修订标识符,那么它可能是一个很好的ETag。

如果你需要做所有必要的工作来渲染页面,只为了生成一个ETag,那么你可以使用渲染页面内容的哈希值,或者根本不使用ETag。

英文:

In general, you want to use something that is cheap to calculate as an ETag. The reason for this is that if the client sends a conditional request (e.g. via the If-None-Match HTTP request header), you can decide whether it is appropriate to send a 304 Not Modified response without having to do all the processing for the page.

For example, if you have some kind of revision identifier for the content of a page, then that might make a good ETag.

If you will need to do all the work necessary to render the page just to generate an ETag, then you may as well just use a hash of the rendered page content, or no ETag at all.

答案2

得分: 0

如果您正在寻找一种高效的ETag机制,并且不需要强加密的功能,我建议使用CRC-32。为了防止碰撞,您可以结合一些东西,比如模板名称、数据长度和CRC:

func etag(name string, data []byte) string {
    crc := crc32.ChecksumIEEE(data)
    return fmt.Sprintf(`W/"%s-%d-%08X"`, name, len(data), crc)
}

这个将生成一个类似W/"tpl-17-3074C885"的ETag。

英文:

If you're looking for an efficient ETag mechanism and don't need something cryptographically strong, I would suggest using CRC-32. To help prevent collisions, you could combine a few things like the template name, length of the data, and the crc:

func etag(name string, data []byte) string {
    crc := crc32.ChecksumIEEE(data)
    return fmt.Sprintf(`W/"%s-%d-%08X"`, name, len(data), crc)
}

This will produce an etag like W/"tpl-17-3074C885".

1: http://en.wikipedia.org/wiki/HTTP_ETag "Wikipedia entry on ETag"
2: http://play.golang.org/p/PyPMccIjYs "Playground link demonstrating etag with crc"

huangapple
  • 本文由 发表于 2013年2月17日 22:01:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/14921842.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定