在ASP.NET Web Api中使用MD5实现HTTP缓存Etag功能 [英] Implementing HTTP caching Etag feature using MD5 in ASP.NET Web Api

查看:434
本文介绍了在ASP.NET Web Api中使用MD5实现HTTP缓存Etag功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在上阅读有关HTTP缓存的Google效果文档https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching ,本文档说我们应尽可能使用ETag。我正在使用ASP.NET Web Api 2.2。我现在正试图在我的所有公共api中实现ETag。我正在考虑使用MD5实现ETag。我的意思是,我将使用MD5在每个请求上散列json响应。在每个请求上使用MD5.calculateHash是否有任何性能损失?我的json响应大小不是太大(在1到20KB的范围内)。

I was reading Google performance document about HTTP caching at https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching, This document says that we should use ETags when possible. I am using ASP.NET Web Api 2.2. I am now trying to implement ETag in all of my public apis. I am thinking to implement ETag using MD5. I mean, I will hash the json response on each request using MD5. Is there any performance hit of using MD5.calculateHash on each request? My json response size are not too big(within the range of 1 to 20KB).

推荐答案

是的,会有性能损失。计算哈希需要时间。但是,您可能会发现计算该哈希值的成本与通过线路将未更改的字节传输到客户端所节省的性能相比并不重要。

Yes there will be a performance hit. Calculating a hash takes time. However, you may find that the cost of calculating that hash is not significant in comparison to the performance saved by transferring unchanged bytes over the wire to the client.

但是,那里不能保证你会用Etags获得性能提升。这取决于很多事情。您是否要在每次请求时重新生成服务器上的Etag以将其与传入请求进行比较?或者您是否要创建etag值的缓存并在资源更改时使其无效?

However, there is no guarantee that you will get a perf improvement with Etags. It depends on many things. Are you going to regenerate the Etag on the server on every request to compare it with the incoming request? Or are you going to create a cache of etag values and invalidate them when the resource changes?

如果要在每个请求上重新生成etag,那么从数据库中提取数据和格式化表示所花费的时间可能会显着高于时间通过线路发送几个字节需要。特别是如果整个表示可以放在一个网络数据包中。

If you are going to regenerate the etag on every request then it is possible that the time spent pulling data from the database and formatting the representation will be significantly higher than the time it takes to send a few bytes over the wire. Especially if the entire representation can fit in a single network packet.

这里的关键是询问你是否真的需要Etags的性能提升,是否值得花费做实施。设置缓存控制标头以启用客户端私有缓存可以为您提供所需的所有好处,而无需实现etags。

The key here is to ask if you really need the performance gain of Etags and is it worth the cost of doing the implementation. Setting cache control headers to enable client side private caching may give you all the benefits that you need without having to implement etags.

我有很多帖子都涉及到主题更详细:

I have a number of posts that go into this subject in more detail:

  • http://www.bizcoder.com/using-etags-and-last-modified-headers-to-improve-performance-with-http-conditional-requests
  • http://bizcoder.com/implementing-conditional-request-handling-for-your-api

这篇关于在ASP.NET Web Api中使用MD5实现HTTP缓存Etag功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆