Difference between Cache and Buffer

Key difference: A cache transparently stores data so that future requests for that data can be served faster. A buffer, on the other hand, temporarily stores data while the data is the process of moving from one place to another.

 

Both cache and buffer are types of temporary storage that are utilized in computer science. However, they differ in the methods and the capabilities in which they are used. A cache transparently stores data so that future requests for that data can be served faster. A buffer, on the other hand, temporarily stores data while the data is the process of moving from one place to another, i.e. the input device to the output device.

 

There are two main types of caches, memory caching and disk caching. Memory caching is when the cache is part of the main memory, whereas disk caching is when the cache is part of some other separate storage area, such as a hard disk. Caching is the process of storing data in a cache so that the data can be accessed faster in the future. The data that is stored within a cache might be values that have been computed earlier or duplicates of original values that are stored elsewhere. When some data is requested, the cache is first checked to see whether it contains that data. The data can be retrieved more quickly from the cache than from its source origin.

 

An easy example to understand caching is to look at web caching. A web cache is a mechanism for the temporary storage (caching) of web documents, such as HTML pages and images. This is mainly done to reduce bandwidth usage, server load, and perceived lag. When a web page is loaded, the data on the pages is cached; hence the next time the page is loaded it is quicker, as data is already present, and only the changes made to the page need to be loaded, which are in turn cached for next time. Google's cache link in its search results provides a way of retrieving information from websites that have recently gone down and a way of retrieving data more quickly than by clicking the direct link.

 

The buffer, on the other hand, is found mainly in the RAM and acts as an area where the CPU can store data temporarily. This area is used mainly when the computer and the other devices have different processing speeds. Typically, the data is stored in a buffer as it is retrieved from an input device (such as a mouse) or just before it is sent to an output device (such as speakers). However, the buffer may also be used when moving data between processes within a computer.

 

So, the computer writes the data up into a buffer, from where the device can access the data, as its own speed. This allows the computer to be able to focus on other matters after it writes up the data in the buffer; as oppose to constantly focus on the data, until the device is done.

 

Buffers can be implemented in a fixed memory location in hardware or by using a virtual data buffer in software, which points to a data buffer are stored on a physical storage medium. Majority of the buffers are utilized in the software. These buffers typically use the faster RAM to store temporary data, as RAM has a much faster access time than hard disk drives. A buffer often adjusts timing by implementing a queue or FIFO algorithm in memory. Hence, it is often writing data into the queue at one rate and reading it at another rate.

 

A common example of this is streaming videos online, such as YouTube. While, watching a video on YouTube, one may notice that a gray bar tends to load before the red bar of the video stream can play. The gray bar is the buffer. It downloads the data of the video and saves it so that the video may play at an uninterrupted rate. As you might have noticed that when the red bar catches up to the gray bar, the video stops, in order to load the rest of the video.

 

Buffers are also often used with I/O to hardware, such as disk drives, sending or receiving data to or from a network, or playing sound on a speaker. Buffers are used for many purposes, such as interconnecting two digital circuits operating at different rates, holding data for use at a later time, allowing timing corrections to be made on a data stream, collecting binary data bits into groups that can then be operated on as a unit, and delaying the transit time of a signal in order to allow other operations to occur.

 

However, a buffer cannot be used to instantaneously move your location in the data stream, unless the new part has already been moved to the buffer. Similar to the YouTube video, which cannot be forwarded to a part that is not covered by the gray bar.  If you do, the buffer will relocate and restart from the new location.

 

Still, the functions of a cache and buffer are not mutually exclusive and are often combined for an ideal performance. 



再次比较page cache和buffer cache区别


磁盘的操作有逻辑级(文件系统)和物理级(磁盘块),这两种Cache就是分别缓存逻辑和物理级数据的。
假设我们通过文件系统操作文件,那么文件将被缓存到Page Cache,如果需要刷新文件的时候,Page Cache将交给Buffer Cache去完成,因为Buffer Cache就是缓存磁盘块的。
也就是说,直接去操作文件,那就是Page Cache区缓存,用dd等命令直接操作磁盘块,就是Buffer Cache缓存的东西。


Page cache实际上是针对文件系统的,是文件的缓存,在文件层面上的数据会缓存到page cache。文件的逻辑层需要映射到实际的物理磁盘,这种映射关系由文件系统来完成。当page cache的数据需要刷新时,page cache中的数据交给buffer cache,但是这种处理在2.6版本的内核之后就变的很简单了,没有真正意义上的cache操作。
 
Buffer cache是针对磁盘块的缓存,也就是在没有文件系统的情况下,直接对磁盘进行操作的数据会缓存到buffer cache中,例如,文件系统的元数据都会缓存到buffer cache中。
 
简单说来,page cache用来缓存文件数据,buffer cache用来缓存磁盘数据。在有文件系统的情况下,对文件操作,那么数据会缓存到page cache,如果直接采用dd等工具对磁盘进行读写,那么数据会缓存到buffer cache。


Buffer(Buffer Cache)以块形式缓冲了块设备的操作,定时或手动的同步到硬盘,它是为了缓冲写操作然后一次性将很多改动写入硬盘,避免频繁写硬盘,提高写入效率。
Cache(Page Cache)以页面形式缓存了文件系统的文件,给需要使用的程序读取,它是为了给读操作提供缓冲,避免频繁读硬盘,提高读取效率。


发布了53 篇原创文章 · 获赞 12 · 访问量 33万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章