mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-27 05:23:46 +00:00
Update 缓存.md
补充布隆过滤器的原理说明和示意图,以及 LRU 中 HashMap 与双向链表的结构图。
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 105 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 81 KiB |
@@ -97,6 +97,10 @@ CDN 主要有以下优点:
|
||||
- 对这些不存在的数据缓存一个空数据,并设置较短的过期时间;
|
||||
- 使用布隆过滤器保存所有存在的 Key,对不存在的 Key 的请求直接拒绝。
|
||||
|
||||
布隆过滤器用多个哈希函数把 Key 映射到位数组的多个位置并置为 1。查询时只要有一个位置为 0,Key 就一定不存在;全部为 1 时 Key 可能存在,存在一定的误判率。
|
||||
|
||||
<div align="center"> <img src="pics/bloom-filter.png" width="560px"/> </div><br>
|
||||
|
||||
### 缓存击穿
|
||||
|
||||
指的是某个热点数据的缓存过期时,大量对它的请求同时到达数据库。
|
||||
@@ -200,6 +204,8 @@ Redis Cluster 使用的不是一致性哈希,而是把 Key 映射到固定的
|
||||
- 访问某个节点时,将其从原来的位置删除,并重新插入到链表头部。这样就能保证链表尾部存储的就是最近最久未使用的节点,当节点数量大于缓存最大空间时就淘汰链表尾部的节点。
|
||||
- 为了使删除操作时间复杂度为 O(1),就不能采用遍历的方式找到某个节点。HashMap 存储着 Key 到节点的映射,通过 Key 就能以 O(1) 的时间得到节点,然后再以 O(1) 的时间将其从双向队列中删除。
|
||||
|
||||
<div align="center"> <img src="pics/lru.png" width="560px"/> </div><br>
|
||||
|
||||
Java 的 LinkedHashMap 内部就是这种结构,把 accessOrder 设为 true 并重写 removeEldestEntry 方法,就可以直接得到一个 LRU 缓存。
|
||||
|
||||
```java
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="620" height="300" viewBox="0 0 620 300">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #666; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #777; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.sub { font-size: 11px; fill: #888; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #666; stroke-width: 1.3; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #666; stroke-width: 1.3; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #C0C0C0; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1 L9,5 L1,9" fill="none" stroke="#666" stroke-width="1.6"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1 L9,5 L1,9" fill="none" stroke="#C0605A" stroke-width="1.6"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1 L9,5 L1,9" fill="none" stroke="#6A9F68" stroke-width="1.6"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="310" y="28" class="title">布隆过滤器</text>
|
||||
<rect x="54" y="135" width="32" height="30" fill="#FFFFFF" stroke="#999"/>
|
||||
<text x="70" y="155" class="c b">0</text>
|
||||
<text x="70" y="181" class="c sub">0</text>
|
||||
<rect x="86" y="135" width="32" height="30" fill="#B1DDF0" stroke="#999"/>
|
||||
<text x="102" y="155" class="c b">1</text>
|
||||
<text x="102" y="181" class="c sub">1</text>
|
||||
<rect x="118" y="135" width="32" height="30" fill="#FFFFFF" stroke="#999"/>
|
||||
<text x="134" y="155" class="c b">0</text>
|
||||
<text x="134" y="181" class="c sub">2</text>
|
||||
<rect x="150" y="135" width="32" height="30" fill="#FFFFFF" stroke="#999"/>
|
||||
<text x="166" y="155" class="c b">0</text>
|
||||
<text x="166" y="181" class="c sub">3</text>
|
||||
<rect x="182" y="135" width="32" height="30" fill="#B1DDF0" stroke="#999"/>
|
||||
<text x="198" y="155" class="c b">1</text>
|
||||
<text x="198" y="181" class="c sub">4</text>
|
||||
<rect x="214" y="135" width="32" height="30" fill="#FFFFFF" stroke="#999"/>
|
||||
<text x="230" y="155" class="c b">0</text>
|
||||
<text x="230" y="181" class="c sub">5</text>
|
||||
<rect x="246" y="135" width="32" height="30" fill="#B1DDF0" stroke="#999"/>
|
||||
<text x="262" y="155" class="c b">1</text>
|
||||
<text x="262" y="181" class="c sub">6</text>
|
||||
<rect x="278" y="135" width="32" height="30" fill="#FFFFFF" stroke="#999"/>
|
||||
<text x="294" y="155" class="c b">0</text>
|
||||
<text x="294" y="181" class="c sub">7</text>
|
||||
<rect x="310" y="135" width="32" height="30" fill="#FFFFFF" stroke="#999"/>
|
||||
<text x="326" y="155" class="c b">0</text>
|
||||
<text x="326" y="181" class="c sub">8</text>
|
||||
<rect x="342" y="135" width="32" height="30" fill="#FFFFFF" stroke="#999"/>
|
||||
<text x="358" y="155" class="c b">0</text>
|
||||
<text x="358" y="181" class="c sub">9</text>
|
||||
<rect x="374" y="135" width="32" height="30" fill="#B1DDF0" stroke="#999"/>
|
||||
<text x="390" y="155" class="c b">1</text>
|
||||
<text x="390" y="181" class="c sub">10</text>
|
||||
<rect x="406" y="135" width="32" height="30" fill="#FFFFFF" stroke="#999"/>
|
||||
<text x="422" y="155" class="c b">0</text>
|
||||
<text x="422" y="181" class="c sub">11</text>
|
||||
<rect x="438" y="135" width="32" height="30" fill="#FFFFFF" stroke="#999"/>
|
||||
<text x="454" y="155" class="c b">0</text>
|
||||
<text x="454" y="181" class="c sub">12</text>
|
||||
<rect x="470" y="135" width="32" height="30" fill="#B1DDF0" stroke="#999"/>
|
||||
<text x="486" y="155" class="c b">1</text>
|
||||
<text x="486" y="181" class="c sub">13</text>
|
||||
<rect x="502" y="135" width="32" height="30" fill="#FFFFFF" stroke="#999"/>
|
||||
<text x="518" y="155" class="c b">0</text>
|
||||
<text x="518" y="181" class="c sub">14</text>
|
||||
<rect x="534" y="135" width="32" height="30" fill="#FFFFFF" stroke="#999"/>
|
||||
<text x="550" y="155" class="c b">0</text>
|
||||
<text x="550" y="181" class="c sub">15</text>
|
||||
<rect x="123" y="53" width="80" height="30" rx="6" class="shadow"/>
|
||||
<rect x="120" y="50" width="80" height="30" rx="6" fill="#B0E3E6"/>
|
||||
<text x="160" y="69.5" class="b c">key1</text>
|
||||
<rect x="423" y="53" width="80" height="30" rx="6" class="shadow"/>
|
||||
<rect x="420" y="50" width="80" height="30" rx="6" fill="#B0E3E6"/>
|
||||
<text x="460" y="69.5" class="b c">key2</text>
|
||||
<line x1="160" y1="83" x2="102" y2="132" class="arrow "/>
|
||||
<line x1="160" y1="83" x2="198" y2="132" class="arrow "/>
|
||||
<line x1="160" y1="83" x2="262" y2="132" class="arrow "/>
|
||||
<line x1="460" y1="83" x2="262" y2="132" class="arrow "/>
|
||||
<line x1="460" y1="83" x2="390" y2="132" class="arrow "/>
|
||||
<line x1="460" y1="83" x2="486" y2="132" class="arrow "/>
|
||||
<text x="30" y="70" class="b">插入</text>
|
||||
<rect x="93" y="243" width="80" height="30" rx="6" class="shadow"/>
|
||||
<rect x="90" y="240" width="80" height="30" rx="6" fill="#E6E6E6"/>
|
||||
<text x="130" y="259.5" class="b c">key3</text>
|
||||
<rect x="383" y="243" width="80" height="30" rx="6" class="shadow"/>
|
||||
<rect x="380" y="240" width="80" height="30" rx="6" fill="#E6E6E6"/>
|
||||
<text x="420" y="259.5" class="b c">key4</text>
|
||||
<line x1="130" y1="237" x2="102" y2="168" class="arrow "/>
|
||||
<line x1="130" y1="237" x2="166" y2="168" class="arrow red"/>
|
||||
<line x1="130" y1="237" x2="198" y2="168" class="arrow "/>
|
||||
<line x1="420" y1="237" x2="198" y2="168" class="arrow "/>
|
||||
<line x1="420" y1="237" x2="262" y2="168" class="arrow "/>
|
||||
<line x1="420" y1="237" x2="486" y2="168" class="arrow "/>
|
||||
<text x="30" y="260" class="b">查询</text>
|
||||
<text x="180" y="252" class="note">位 3 为 0</text>
|
||||
<text x="180" y="270" class="note">一定不存在</text>
|
||||
<text x="470" y="252">全为 1:可能存在</text>
|
||||
<text x="470" y="270" class="sub">实际未插入过,属于误判</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 6.1 KiB |
@@ -0,0 +1,73 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="620" height="270" viewBox="0 0 620 270">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #666; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #777; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.sub { font-size: 11px; fill: #888; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #666; stroke-width: 1.3; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #666; stroke-width: 1.3; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #C0C0C0; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1 L9,5 L1,9" fill="none" stroke="#666" stroke-width="1.6"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1 L9,5 L1,9" fill="none" stroke="#C0605A" stroke-width="1.6"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1 L9,5 L1,9" fill="none" stroke="#6A9F68" stroke-width="1.6"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="310" y="28" class="title">LRU:HashMap + 双向链表</text>
|
||||
<rect x="33" y="160" width="80" height="36" rx="6" class="shadow"/>
|
||||
<rect x="30" y="157" width="80" height="36" rx="6" fill="#E6E6E6"/>
|
||||
<text x="70" y="179.5" class="c b mono">head</text>
|
||||
<rect x="153" y="160" width="80" height="36" rx="6" class="shadow"/>
|
||||
<rect x="150" y="157" width="80" height="36" rx="6" fill="#B1DDF0"/>
|
||||
<text x="190" y="179.5" class="c b mono">k2</text>
|
||||
<rect x="273" y="160" width="80" height="36" rx="6" class="shadow"/>
|
||||
<rect x="270" y="157" width="80" height="36" rx="6" fill="#B1DDF0"/>
|
||||
<text x="310" y="179.5" class="c b mono">k3</text>
|
||||
<rect x="393" y="160" width="80" height="36" rx="6" class="shadow"/>
|
||||
<rect x="390" y="157" width="80" height="36" rx="6" fill="#B1DDF0"/>
|
||||
<text x="430" y="179.5" class="c b mono">k1</text>
|
||||
<rect x="513" y="160" width="80" height="36" rx="6" class="shadow"/>
|
||||
<rect x="510" y="157" width="80" height="36" rx="6" fill="#E6E6E6"/>
|
||||
<text x="550" y="179.5" class="c b mono">tail</text>
|
||||
<line x1="112" y1="169" x2="148" y2="169" class="arrow "/>
|
||||
<line x1="148" y1="181" x2="112" y2="181" class="arrow "/>
|
||||
<line x1="232" y1="169" x2="268" y2="169" class="arrow "/>
|
||||
<line x1="268" y1="181" x2="232" y2="181" class="arrow "/>
|
||||
<line x1="352" y1="169" x2="388" y2="169" class="arrow "/>
|
||||
<line x1="388" y1="181" x2="352" y2="181" class="arrow "/>
|
||||
<line x1="472" y1="169" x2="508" y2="169" class="arrow "/>
|
||||
<line x1="508" y1="181" x2="472" y2="181" class="arrow "/>
|
||||
<rect x="132" y="52" width="356" height="66" rx="8" class="frame"/>
|
||||
<text x="142" y="70" class="b">HashMap</text>
|
||||
<rect x="160" y="72" width="60" height="30" rx="6" fill="#B0E3E6"/>
|
||||
<text x="190" y="91.5" class="c mono">k2</text>
|
||||
<line x1="190" y1="104" x2="190" y2="154" class="arrow dash"/>
|
||||
<rect x="280" y="72" width="60" height="30" rx="6" fill="#B0E3E6"/>
|
||||
<text x="310" y="91.5" class="c mono">k3</text>
|
||||
<line x1="310" y1="104" x2="310" y2="154" class="arrow dash"/>
|
||||
<rect x="400" y="72" width="60" height="30" rx="6" fill="#B0E3E6"/>
|
||||
<text x="430" y="91.5" class="c mono">k1</text>
|
||||
<line x1="430" y1="104" x2="430" y2="154" class="arrow dash"/>
|
||||
<text x="70" y="217" class="c sub">哨兵</text>
|
||||
<text x="550" y="217" class="c sub">哨兵</text>
|
||||
<text x="190" y="217" class="c ok">最近访问</text>
|
||||
<text x="430" y="217" class="c note">最久未访问,最先淘汰</text>
|
||||
<text x="310" y="255" class="c">访问或插入时把节点移到 head 之后,超出容量时删除 tail 之前的节点</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 4.1 KiB |
Reference in New Issue
Block a user