Skip to content

Commit 635c2a5

Browse files
committed
lru comment
1 parent caf4cde commit 635c2a5

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

docs/03_链表/lru_cache.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def cache(func):
1717
store = {}
1818

1919
@wraps(func)
20-
def _(n):
20+
def _(n): # 这里函数没啥意义就随便用下划线命名了
2121
if n in store:
2222
return store[n]
2323
else:
@@ -84,6 +84,13 @@ def add_or_update(self, key, value):
8484
self.od.popitem(last=False)
8585

8686
def __call__(self, func):
87+
"""
88+
一个简单的 LRU 实现。有一些问题需要思考下:
89+
90+
- 这里为了简化默认参数只有一个数字 n,假如可以传入多个参数,如何确定缓存的key 呢?
91+
- 这里实现没有考虑线程安全的问题,要如何才能实现线程安全的 LRU 呢?当然如果不是多线程环境下使用是不需要考虑的
92+
- 假如这里没有用内置的 dict,你能使用 redis 来实现这个 LRU 吗,如果使用了 redis,我们可以存储更多数据到服务器。而使用字典实际上是换到到了Python进程里(localCache)。
93+
"""
8794
def _(n):
8895
if n in self.od:
8996
return self.get(n)

0 commit comments

Comments
 (0)