etag

Getting ETags right

笑着哭i 提交于 2019-11-28 16:58:58
I’ve been reading a book and I have a particular question about the ETag chapter. The author says that ETags might harm performance and that you must tune them finely or disable them completely. I already know what ETags are and understand the risks, but is it that hard to get ETags right? I’ve just made an application that sends an ETag whose value is the MD5 hash of the response body. This is a simple solution, easy to achieve in many languages. Is using MD5 hash of the response body as ETag wrong? If so, why? Why the author (who obviously outsmarts me by many orders of magnitude) does not

Create ETag filter in ASP.NET MVC

拜拜、爱过 提交于 2019-11-28 15:54:51
I would like to create an ETag filter in MVC. The problem is that I can't control the Response.OutputStream, if I was able to do that I would simply calculate the ETag according to the result stream. I did this thing before in WCF but couldn't find any simple idea to do that in MVC. I want to be able to write something like that [ETag] public ActionResult MyAction() { var myModel = Factory.CreateModel(); return View(myModel); } Any idea? This is the best I could come up with, I didn't really understand what you meant by you can't control the Response.OutputStream. using System; using System.IO

Http缓存策略

假装没事ソ 提交于 2019-11-28 15:33:17
浏览器一般缓存图片、 CSS 、 JS 等静态文件,因为这些文件的更新频率相对来说比较低,合理利用浏览器的缓存对网站的性能提升有很大帮助。 HTTP 缓存分为两部分,分别是本地缓存和缓存协商,当本地缓存不生效时会启用缓存协商。 HTTP 缓存主要由 HTTP 协议的头(Header)信息来制定。 本地缓存 本地缓存是指浏览器请求资源时命中了浏览器本地的缓存资源,浏览器并不会发送真正的请求给服务器了。它的执行过程是: 第一次浏览器发送请求给服务器时,此时浏览器还没有本地缓存副本,服务器返回资源给浏览器,响应码是200 OK,浏览器收到资源后,把资源和对应的响应头一起缓存下来。 第二次浏览器准备发送请求给服务器时候,浏览器会先检查上一次服务端返回的响应头信息中的Cache-Control,它的值是一个相对值,单位为秒,表示资源在客户端缓存的最大有效期,过期时间为第一次请求的时间减去Cache-Control的值,过期时间跟当前的请求时间比较,如果本地缓存资源没过期,那么命中缓存,不再请求服务器。 如果没有命中,浏览器就会把请求发送给服务器,进入缓存协商阶段。 与本地缓存相关的头有:Cache-Control、Expires,Cache-Control有多个可选值代表不同的意义,而Expires就是一个日期格式的绝对值。 Cache-Control Cache

How do I support ETags in ASP.NET MVC?

帅比萌擦擦* 提交于 2019-11-28 15:20:49
How do I support ETags in ASP.NET MVC? jaminto @Elijah Glover's answer is part of the answer, but not really complete. This will set the ETag, but you're not getting the benefits of ETags without checking it on the server side. You do that with: var requestedETag = Request.Headers["If-None-Match"]; if (requestedETag == eTagOfContentToBeReturned) return new HttpStatusCodeResult(HttpStatusCode.NotModified); Also, another tip is that you need to set the cacheability of the response, otherwise by default it's "private" and the ETag won't be set in the response: Response.Cache.SetCacheability

Syntax for ETag?

依然范特西╮ 提交于 2019-11-28 09:40:31
Redbot reports that my webpage has invalid header: The ETag header's syntax isn't valid. My headers are set to: ETag: 4ae413bd Why is it invalid? What is the syntax for an ETag? Try ETag: "4ae413bd" . The value of an ETag must follow the ABNF form: entity-tag = [ weak ] opaque-tag weak = "W/" opaque-tag = quoted-string quoted-string = ( <"> *(qdtext | quoted-pair ) <"> ) qdtext = <any TEXT except <">> quoted-pair = "\" CHAR CHAR = <any US-ASCII character (octets 0 - 127)> TEXT = <any OCTET except CTLs, but including LWS> OCTET = <any 8-bit sequence of data> LWS = [CRLF] 1*( SP | HT ) CTL =

NSURLCache and ETags

最后都变了- 提交于 2019-11-28 08:25:16
Does NSURLCache transparently handle ETags received by server? I mean: does it automatically store ETags for each URL request and then send the appropriate If-None-Match when a request to the same URL is submitted? Or do I have to manage it by myself? yes it does handle it transparently if you set its cache mode: NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval:60]; note: you cannot see the header in the request at all and if a 304 response is returned by the server you will only

How to use etags in a PHP file?

烂漫一生 提交于 2019-11-28 07:30:21
How do you implemented etags inside a PHP file? What do I upload to the server and what do I insert into my PHP file? Create / edit your .htaccess file and add the following: FileETag MTime Size Either place the following inside a function or put it at the top of the PHP file that you need etags to work on: <?php $file = 'myfile.php'; $last_modified_time = filemtime($file); $etag = md5_file($file); header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT"); header("Etag: $etag"); if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time || trim($_SERVER[

浏览器缓存机制详解(一)

独自空忆成欢 提交于 2019-11-28 07:09:26
浏览器缓存机制可以极大的提升用户体验,另一方面会因为读取缓存而展示了错误的东西,因而在开发中要设法将其删除。 什么是浏览器缓存? 浏览器缓存就是把一个已经请求过的web资源(如html页面,图片,JS,数据)拷贝一份放在浏览器中。缓存会根据进来的请求保存输入内容的副本。当下一个请求到来的时候,如果是相同的URL,浏览器会根据缓存机制决定是直接使用副本响应访问请求还是向源服务器再次发起请求。 使用缓存的原因 (1)减少网络带宽消耗 当web缓存副本被使用时,只会产生极小的网络流量,可以有效降低运营成本。 (2)降低服务器压力 给网络资源设定有效期之后,用户可以重复使用本地缓存,减少对源服务器的请求,简介降低了对服务器的压力,同时搜索引擎的爬虫机器人也能根据过期机制降低爬取的频率,也能有效降低服务器压力。 (3)减少网络延迟 缓存的使用可以明显加快页面打开速度,达到更好的用户体验。 浏览器的缓存机制 对于浏览器的缓存来讲,这些规则是在HTTP协议头部和HTML页面的Meta标签中定义的。他们分别从新鲜度和校验值两个维度来规定浏览器是否可以直接使用缓存中的副本,还是需要去源服务器获取新版本。 过期机制:指的是缓存副本的有效期。一个缓存的副本必须满足以下条件,浏览器会认为它是有效的,足够新的: 1.含有完整的过期时间控制头信息(HTTP协议报头),并且仍在有效期内 2

HTTP缓存机制与原理详解

空扰寡人 提交于 2019-11-28 07:05:34
1.1 - 缓存 缓存可以重用已获取的资源能够有效的提升网站与应用的性能。 Web 缓存能够减少延迟与网络阻塞,进而减少显示某个资源所用的时间。 借助 HTTP 缓存,Web 站点变得更具有响应性。 缓存分为两点:强制缓存和协商缓存 1.2 - 强制缓存 概念 强制缓存就是向浏览器缓存查找该请求结果,并根据该结果的缓存规则来决定是否使用该缓存结果的过程。 简单来讲就是强制浏览器使用当前缓存 所请求的数据在缓存数据库中尚未过期时,不与服务器进行交互,直接使用缓存数据库中的数据。当缓存未过期时基本流程如下: 实现:通过服务器端设置响应头字段来控制强制缓存的过期时间 expires (http1.0) 其指定了一个日期/时间, 在这个日期/时间之后,HTTP响应被认为是过时的。但是它本身是一个HTTP1.0标准下的字段,所以如果请求中还有一个置了 “max-age” 或者 “s-max-age” 指令的Cache-Control响应头,那么 Expires 头就会被忽略。 cache-control (http1.1) 通用消息头用于在http 请求和响应中通过指定指令来实现缓存机制。 cache-control 优先级比 expires 高 expires 日期(new Date().toGMTString()) 缓存的最大有效时间 cache-control max-age(单位s)

聊一聊HTTP的缓存机制与原理

孤街醉人 提交于 2019-11-28 07:05:22
概述 缓存的重要性不言而喻,通过网络请求资源缓慢并且降低了客户端的用户体验,增添了服务端的负担。很多短期之内不会经常发生变化的资源文件没必要每次访问都想服务端进行数据请求,而缓存策略的使用就是为了改善客户端的呈现时间,降低服务端的负担。 对于HTTP的缓存机制来说,策略体现在HTTP的头部信息的字段上,而这些策略 根据是否需要重新向服务器端发起请求 可以分为 强缓存 和 协商缓存 两大类。接下来用UML时序图的形式来呈现这两大类缓存策略的大体过程。 Tips : Vscode 配合插件 plantUML 画(写)UML图很爽。相比之前用的 ProcessOn 拖拽的形式,你只需要熟悉 plantUML 的语法,在你的电脑上安装一下 java , Graphviz 的环境,不用操心样式的展现UML真心舒服。下面的图我就是用这个工具去画的,很推荐。 强缓存 强缓存紧密联系着一个缓存时间期限,当浏览器请求资源的时候会查看缓存中的资源是否存在并且确定该缓存的资源是否过了“保质期”,若没有超过保质期则将取得缓存中的资源进行下一步处理 协商缓存 可见协商缓存无论如何都会和服务器交互,比较强缓存稍微要复杂一点,但是二者是相辅相成并且可以共同存在的,强缓存优先级较高,意味着请求一个资源时会先比较强缓存的字段,如果命中则不会再执行接下来的协商缓存的过程。 接下来就是要介绍,两类缓存相关HTTP