WEB之缓存(Buffer和Cache)

2021-08-01

Buffer

1. ​(British English) one of two round metal devices on the front or end of a train, or at the end of a railway track, that reduce the shock if the train hits something
​2. (computing) an area in a computer’s memory where data can be stored for a short time

0.缓冲。缓和冲击。

Cache

​1. a hidden store of things such as weapons
2. (computing) a part of a computer’s memory that stores copies of data that is often needed while a program is running. This data can be accessed very quickly.

0.缓存。为了缓和冲击而 临时的 存储。

很多人在读和写上纠结这两个词,但是比如 InnoDB 中就定义了Buffer Pool,Change Buffer。纠结read-cache和write-buffer就跟纠结回字有几种写法一样太。。。没意思。

所以,一个字,缓。

 

对于web应用来说,主要还是为了,减少内存和硬盘间的数据交换,或者降低计算量,把计算结果(为了缓和后面多次请求的冲击而 临时)存起来,在数据未改变时 下次再请求就直接返回上次的结果。

比如,MySQL大量读写时,将部分Page放到Buffer里,减少一遍遍去硬盘获取、修改数据。

 

 

spring boot开发中,可以把需要缓冲的数据放到单例对象里,或者放到redis里,或者如果有大量需要计算的数据可以先计算好放入数据库或者随便哪儿。

重要的是清除缓存机制。

 

# spring boot 自带的缓存机制

@Cacheable
@CacheEvict