Skip to content

Commit 04b8a68

Browse files
committed
[20_hashtable] LRU.
1 parent e093136 commit 04b8a68

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

notes/20_hashtable/readme.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,26 @@
66
* 顺序表:支持随机访问,连续存储。
77

88
散列表 + 链表:结合优点、规避缺点。
9+
10+
## 结合散列表的 LRU 缓存淘汰算法
11+
12+
缓存的操作接口:
13+
14+
* 向缓存添加数据
15+
* 从缓存删除数据
16+
* 在缓存中查找数据
17+
18+
然而——不管是添加还是删除,都涉及到查找数据。因此,单纯的链表效率低下。
19+
20+
魔改一把!
21+
22+
![](https://static001.geekbang.org/resource/image/ea/6e/eaefd5f4028cc7d4cfbb56b24ce8ae6e.jpg)
23+
24+
* `prev``next`:双向链表——LRU 的链表
25+
* `hnext`:单向链表——解决散列冲突的链表
26+
27+
操作:
28+
29+
* 在缓存中查找数据:利用散列表
30+
* 从缓存中删除数据:先利用散列表寻找数据,然后删除——改链表就好了,效率很高
31+
* 向缓存中添加数据:先利用散列表寻找数据,如果找到了,LRU 更新;如果没找到,直接添加在 LRU 链表尾部

0 commit comments

Comments
 (0)