根据文档,另一个方面是:
如果 maxsize 设置为 None,则禁用 LRU 功能,并且缓存可以无限制地增长。当 maxsize 是 2 的幂时,LRU 功能表现最佳。
文档示例
import functools
import urllib
import requests
@functools.lru_cache(maxsize=32)
def get_pep(num):
'Retrieve text of a Python Enhancement Proposal'
resource = 'http://www.python.org/dev/peps/pep-%04d/' % num
try:
with urllib.request.urlopen(resource) as s:
return s.read()
except urllib.error.HTTPError:
return 'Not Found'
for n in 8, 290, 308, 320, 8, 218, 320, 279, 289, 320, 9991:
pep = get_pep(n)
print(n, len(pep))
print(get_pep.cache_info())
输出
8 106439
290 59766
308 56972
320 49551
8 106439
218 46795
320 49551
279 48553
289 50882
320 49551
9991 9
CacheInfo(hits=3, misses=8, maxsize=32, currsize=8)