服务器缓存工作原理
A cache is a pretty simple concept that shows up in a lot of different areas in computing. In almost all cases, a cache serves basically one main purpose. The key idea is to speed up access to data that is needed frequently. What that data actually is could vary depending on the context.
缓存是一个非常简单的概念,它出现在计算的许多不同区域中。 几乎在所有情况下,缓存基本上都可以满足一个主要目的。 关键思想是加快对经常需要的数据的访问。 该数据实际是什么可能取决于上下文。
One example most people are familiar with is the apps on your phone. So let’s start there. Let’s say you’re listening to a song on your favorite music playing app. You’re absolutely hooked on to the song and you can’t get enough of it. So you keep mashing that repeat button and listening to the same song over and over again. Now, the actual contents of the song are not stored on your phone by default. When you install a music streaming service, you’re not installing the millions of songs in their library onto your phone automatically. That would be ridiculous.
大多数人都熟悉的一个例子是手机上的应用程序。 因此,让我们从那里开始。 假设您正在听自己喜欢的音乐播放应用上的歌曲。 您绝对迷上了这首歌,而且听不清。 因此,您一直将“重复”按钮混在一起,并一遍又一遍地聆听同一首歌曲。 现在,默认情况下,歌曲的实际内容不会存储在手机上。 当您安装音乐流媒体服务时,并不是将音乐库中的数百万首歌曲自动安装到手机上。 那太荒谬了。
Instead, once you listen to a song, the app is smart enough to make a copy of it on your phone. So the next time you try to listen to the same song, the app uses the copy for playback rather than downloading it again. This is great for two reasons. Not only are you doing the streaming service a favor by relieving some pressure from their servers, but you’re also saving some of your own precious bandwidth and data charges you’d spend grabbing the song each time.
相反,一旦您听了一首歌,该应用程序就足够智能,可以在手机上复制它。 因此,下次您尝试收听同一首歌曲时,该应用程序将使用该副本进行播放,而不是再次下载。 这很棒,有两个原因。 通过减轻服务器的压力,您不仅为流媒体服务提供了帮助,而且还节省了一些宝贵的带宽和数据费用,而这些费用和费用您每次都要花这首歌来抓。
You can already start to see how this approach would be useful for other things as well, such as images, videos, your message history, or anything else that doesn’t change too frequently.
您已经开始看到这种方法对其他事物也将很有用,例如图像,视频,您的消息历史记录或其他不经常更改的事物。
Another example you’ve probably already experienced is the browser cache on your computer. The idea, again, is very similar. Websites are rarely just blobs of text anymore. They’re comprised of images, videos, and a whole lot of data that can be expensive for your network. So instead of fetching every single resource every time you visit the website, some of theses resources are cached by your browser the first time you visit. Every subsequent visit then relies on reading at least some of the information from this cache.
您可能已经遇到的另一个示例是计算机上的浏览器缓存。 同样,这个想法非常相似。 网站已经不再只是文字的斑点了。 它们由图像,视频和大量数据组成,这对于您的网络可能是昂贵的。 因此,与您每次访问该网站时都不会获取所有单个资源相反,这些资源中的某些资源将在您首次访问时由浏览器进行缓存 。 然后,每个后续访问都依赖于从此缓存中读取至少一些信息。
This is all great, of course. Caches are great. They speed up access to things and save you some $$$ in network costs. They make lives easier for developers. Everybody wins. So what’s the catch?
当然,这一切都很棒。 缓存很棒。 他们加快了访问速度,并节省了一些网络费用。 它们使开发人员的生活更加轻松。 每个人都赢。 那么有什么收获呢?
Well, the problem is that at the end of the day, the data stored on your local device (whether your browser or your phone) is just a copy of whats on the server. It’s not the real thing.
好吧,问题在于,归根结底,存储在本地设备(无论是浏览器还是手机)上的数据只是服务器上内容的副本 。 这不是真的。
So if the data on the server has changed since the last time your cache was updated, the thing you’re seeing is likely out of date. This isn’t usually the end of the world. Sometimes, it just means you don’t see your friends’ latest profile pictures as soon as they‘re updated. But this is a conscious tradeoff that needs to be made by the developers, and it might not always be worth it. You might have noticed that clearing your browser and app caches can sometimes fix weird bugs.
因此,如果自上次更新缓存以来服务器上的数据已更改,那么您看到的内容可能已过时。 这通常不是世界末日。 有时,这只是意味着您不会在朋友更新后立即看到他们的最新个人资料照片。 但这是开发人员必须进行的自觉权衡,也许并不总是值得的。 您可能已经注意到,清除浏览器和应用程序缓存有时可以修复怪异的错误。
The other downside is that caches take up space. A lot of space. In fact, you can see just how much space some of the apps on your phone are using by taking a casual look through your phone’s settings. This isn’t a problem if you have a lot of storage at your disposal, but if you’re running even slightly out of date hardware, this can be noticeable.
另一个缺点是缓存占用空间。 很大的空间。 实际上,通过随意查看手机的设置,您可以看到手机上某些应用程序正在使用多少空间。 如果您拥有大量存储空间,这不是问题,但是如果您运行的硬件甚至有些陈旧,这将是显而易见的。
And lastly, even though we’ve talked about caches only in the contexts of your apps and browsers so far, they’re a critical building block for computers at the hardware level. The CPU in your computer executes, to use the scientific term, a crap ton of instructions every second.
最后,即使到目前为止,我们仅在您的应用程序和浏览器的上下文中讨论了缓存,但它们是硬件级别计算机的关键构建块。 用科学术语来说,计算机中的CPU每秒执行一次大量的指令。
Just like before, in order to speed up operations, the CPU maintains a local copy of data that it needs frequently in a hardware cache. Also, the CPU might have multiple levels of caches. The levels closest to the CPU are the fastest but can store the least amount of data. Each successive layer is slower but can store more data. For example, you might have heard of the terms L1, L2, and L3 caches when shopping for a new computer.
与以前一样,为了加快操作速度,CPU在硬件缓存中维护了它经常需要的数据的本地副本。 另外,CPU可能具有多个缓存级别 。 最接近CPU的级别是最快的,但可以存储的数据量最少。 每个连续层较慢,但可以存储更多数据。 例如,当您购买新计算机时,您可能听说过术语L1,L2和L3缓存。
Caches are a powerful concept that is seemingly everywhere once you start looking for it!
缓存是一个强大的概念,一旦您开始寻找它,它似乎无处不在!
翻译自: https://medium.com/bits-n-pieces/how-does-a-cache-work-878f7ea20fdf
服务器缓存工作原理