Too slow!

“HTTP you come over, there is something I want to discuss with you!” The browser boss wandered into the HTTP operating room and said.

“How can you come? I’m busy. Sit down.” So saying, I put down my utensil and pulled out my stool.

“Not not, I want to understand understand the situation, the door still have more URL waiting for you to send!” The browser boss sighed.

I looked out of the door and was startled to find that the URL had circled my operating room. “What’s the matter?” I asked.

“There are a lot of HTTP requests for this page!” The browser sighed and said slowly, “Can you think of a way to make it faster?”

“This I also have no way ah, I this have 6 people only, don’t you add a point?”

“No line, browser space is too small, operating system eldest brother is not willing to send a person to come over, calculate, you first busy, I think of a way!” Browser boss a face of frustration.

negotiation

The URL outside the operating room gradually decreased. After about 500ms, I finally put down the scalpel in my hand and found the browser boss pacing back and forth in the operating room with a sigh of relief.

“HTTP have you noticed that there are some urls you sent several times!” The browser boss mused.

“I didn’t notice it, but I remember it.” Then I thought.

“Do you recall the same data they brought back?” The browser boss hit the nail on the head.

“Let me see! Lost in memory, I muttered to myself, “There was a URL that came in three times, there was, and the result is the same!”

“Really? That actually all have no need to send, take the data of the last time directly to me line!” The stately browser was suddenly as excited as a child.

“Yes, but…” I was about to say something.

“No buts! First try so, before the data to save a storage, disk memory space you allocate.” The excited browser suddenly interrupted me and hopped out.

“This eldest brother has a little spirit big bar, no line, this how line.” I said to myself, “But an idea is a good idea, how to save, what resources need to save the problem. First of all, FOR data requests, I should not use caching, that is, for json requests in the Content-Type. Secondly, you can’t keep the file all the time, if it changes how to do! Then give a fixed time first! Good, ha ha! I am a genius!!”

Off-screen rage

“Damn it, Chrome, every time I post it takes forever to respond, Google says this is a fixed cache time! What? Still let not let programmer work!”

“Add a time stamp and everything will be solved!”

“Hey, it’s not smart, this is Chrome!”

Browser boss heard slightly angry voice Cue to him, can not help trembling, quickly came to the HTTP operating room!

“What are you doing with HTTP? Now everyone is complaining off-screen. I can hear everyone getting ready to use FireFox!” The browser boss was sweating.

“Is not the last time you said to save the data, I also optimized the next, at least not to save json data, otherwise…” Suddenly I thought of FireFox a face satisfied appearance, also anxious to get up.

“What! Or we don’t optimize it!” “Said the browser boss.

“Well, I’ve been thinking about it for a few days now, and it’s not up to us to decide how to cache files. You think they send files to the server first, and if the server notifies us of updates, that would be great!”

“Fair point, but one thing, the server can’t notify us, because the connection is one-way.” Suddenly excited and lost, browser boss sighed: “look or not!”

“No, no, no! This matter I want to talk with my elder brothers in the server, eldest brother you don’t get depressed first, I have a very good method, I first verify verify. If it passes, THEN I am confident I can do it well.”

“Oh…” The browser boss was still in a disappointed mood and didn’t seem to believe my idea very much.

Strong cache

Pick up the phone on your desk, turn it a few times, and a beep comes from the receiver.

“Hey, you dumb ass, what’s the big deal about being assigned to Chrome? So long for a phone call!”

“So busy! I’ve got an idea I’d like to talk to you about! ‘

“What idea? I this also very busy, don’t say a bit have no of, old niang busy!”

“Once this idea is realized, our workload will be greatly reduced.” I paused. “Do you realize that many of the requests are the same?”

“Yes, and sometimes quite a lot! Speaking of which, I also want to ask, why does your client HTTP always request the same thing? Keep asking for straightness?”

“We can’t help it, people refresh the page all the time, and some people visit the same pages every day, so we often ask you for the same things.”

“That you cache down ah! Don’t ask me for it so easily!”

“We’ve been experimenting with it, but it hasn’t worked out so well. We set the cache time at one day, but we didn’t know anything about the page changes!”

“Yes, there are fewer requests from you these days.”

“Now the browser boss has decided to give up caching because the cache does not update data! But I have an idea that will solve this problem, and I need to give it a try!”

“What do you think? Tell me, and I’ll cooperate with you!”

“Remember the header?”

“Of course, isn’t that what we do?”

“Can you give us an expiry date for the resource?”

“Effective time? What do you mean?”

“The amount of time that the resource doesn’t change.”

“I don’t know that either!”

“Let the developer set it, give it to them, and let’s put it in the header.”

“Well, that’s a good idea, and then you’re going to cache the corresponding resources based on that setting?”

“Bingo ~”

“OK, I’ll open this setting to my application layer. In the header, I’ll add a Expires field for you. You can use it directly!”

“So sweet! Thanks!”

“My husband! That I go to inform first!”

Off-screen self-reproach

“Brother, sorry ha, I just set this resource to expire tomorrow ~~ you strong brush try?”

“…” Silently pressing CTRL + F5 “Dude, it doesn’t work…”

“Ah! The middle of this resource to go proxy server…”

“So what?”

“A time stamp?”

“No way!

“What if you didn’t succeed?”

“…”

Negotiate the cache

“Hello? At?”

“What is it now? I’m busy!”

“Remember the last time I called on you? Did we add an Expires header?”

“Well, why, isn’t it all right? You’ve dropped the pot on us!” The other end of the phone spread an angry voice: “OLD niang really caught your way! “These guys, when they set Expires, they think they didn’t send it right, and they’re just uploading things to the server all day. They’re sick of it.”

“Calm down ~ calm down ~ I still have a matter.” I looked embarrassed and said, “After the revision, there are many fewer requests, but we found that once the resource expires, we still have to ask you for it, but the returned data we compared are still the same! So I still need to work with you to optimize it.”

“Come back! The other end of the phone spread slightly angry voice: “still want to shake the pot?”

“Just think how much less work it would save! Just add a response header this time.”

‘Speak!

“Can you generate a unique identifier based on the resource? It’s the kind of logo that changes when the resource changes.”

“Can ah, this return not simple, according to modify time good ah!”

“It turns out it’s that simple. If you do change the time to meet this requirement, you give it to us, we save it, and then when we request the same resource, we will give you this identifier. You compare it, and if it’s the same, we’ll return 304 and we’ll just use the cache, ok? Is it a lot less work?”

“Indeed! That’s a good idea! Can reduce a lot of workload. All right, add it to your list! Last-modified, if-Modified-since in your request header, and I’ll notify the application!”

“W ~”

renovation

“Feed! At?”

“Oh, busy people start to idle?” A slightly jealous voice came from the other side.

“Yeah, it’s a lot less work since we started using the headers we agreed on last time. Did you hear that THE HTTP protocol has been upgraded?” I’m a little proud of myself.

“Yes, rose 1.1, finally can long connect, but the effect is not very ideal ah, the blocked place or blocked.” Sounds like we’re having some trouble with the server.

“Anyway, remember the Expires header?”

“Remember, be expiration time.”

“Now this description is not accurate, our local time is not consistent with what you gave, our client actually wants a cache policy, such as how long to cache, how to cache and so on, while the protocol upgrade, we also change.”

“Well, yes, it’s a hassle to recalculate the expiration time every time, so I’ll leave that to you and I’ll just set the cache policy. Call it cache-control! It’s up to you to make the rules based on how you use them.”

“Well, ok. We’ll sum it up and fax it back to you.”

“There’s one other thing I should mention about caching, too.” The other end of the phone, slightly apologetic tone.

“Yes, yes.”

“In fact, it is not very accurate to determine the cache resources according to the modification time. Our rule is that as long as the file is opened, it is considered to be modified, so there is error, and if the modification time of the file is less than 1 second, that this value is actually the same!”

“Is there a solution to this problem?”

“We can generate identifiers based on the content of the article. I gave it to you through the new header ETag, which we are ready to support. This time, it will be included in the new protocol.

“OK that’s good, so which field do we use for you?”

“If – None – Match”

“Ok, I’ll make a note of that.”

“URL came, I hung up first ha ~ strategy and so on give you fax, you receive!”

“Well, go ahead.”

Cache-Control

About 100ms later, the fax machine on the server starts to print the following information:

instruction role
public Both consumers can cache (client and proxy).
private Only clients can cache (e.g., browsers).
max-age = time Only the cachetimeThe specified time, once the cache expires, a new request is required.
s-max-age = time Overrides only on proxy serversmax-ageThe effect.
no-store Responses are not cached.
no-cache Cache, but cache immediately invalid, cooperateETagUse.
max-stable = time intimeThe cache is used even if the cache is invalid.
min-fresh = time Need to be intimeTime, take resources again.

Invalidation doesn’t mean the cache is clean, it just means the cache is expired, and if the server (that is, you) confirms that the cache is valid, the cache is used.

Instructions can be combined, and the combined use structure diagram is assigned:

This figure can be used as a developer document.

The following figure is our summary of cache hitting rules for your reference:

reading

  • HTTP operating Room