Python 中 lru_cache 的使用和实现

在计算机软件领域,缓存(Cache)指的是将部分数据存储在内存中,以便下次能够更快地访问这些数据,这也是一个典型的用空间换时间的例子。一般用于缓存的内存空间是固定的,当有更多的数据需要缓存的时候,需要将已缓存的部分数据清除后再将新的缓存数据放进去。需要清除哪些数据,就涉及到了缓存置换的策略,LRU(Least Recently Used,最近最少使用)是很常见的一个,也是 Python 中提供的缓存置换策略。

下面我们通过一个简单的示例来看 Python 中的 lru_cache 是如何使用的。

def factorial(n): print(f"计算 {n} 的阶乘") return 1 if n <= 1 else n * factorial(n - 1) a = factorial(5) print(f'5! = {a}') b = factorial(3) print(f'3! = {b}')

上面的代码中定义了函数 factorial,通过递归的方式计算 n 的阶乘,并且在函数调用的时候打印出 n 的值。然后分别计算 5 和 3 的阶乘,并打印结果。运行上面的代码,输出如下

计算 5 的阶乘 计算 4 的阶乘 计算 3 的阶乘 计算 2 的阶乘 计算 1 的阶乘 5! = 120 计算 3 的阶乘 计算 2 的阶乘 计算 1 的阶乘 3! = 6

可以看到,factorial(3) 的结果在计算 factorial(5) 的时候已经被计算过了,但是后面又被重复计算了。为了避免这种重复计算,我们可以在定义函数 factorial 的时候加上 lru_cache 装饰器,如下所示

import functools # 注意 lru_cache 后的一对括号,证明这是带参数的装饰器 @functools.lru_cache() def factorial(n): print(f"计算 {n} 的阶乘") return 1 if n <= 1 else n * factorial(n - 1)

重新运行代码,输入如下

计算 5 的阶乘 计算 4 的阶乘 计算 3 的阶乘 计算 2 的阶乘 计算 1 的阶乘 5! = 120 3! = 6

可以看到,这次在调用 factorial(3) 的时候没有打印相应的输出,也就是说 factorial(3) 是直接从缓存读取的结果,证明缓存生效了。

被 lru_cache 修饰的函数在被相同参数调用的时候,后续的调用都是直接从缓存读结果,而不用真正执行函数。下面我们深入源码,看看 Python 内部是怎么实现 lru_cache 的。写作时 Python 最新发行版是 3.9,所以这里使用的是 ,并且保留了源码中的注释。

def lru_cache(maxsize=128, typed=False): """Least-recently-used cache decorator. If *maxsize* is set to None, the LRU features are disabled and the cache can grow without bound. If *typed* is True, arguments of different types will be cached separately. For example, f(3.0) and f(3) will be treated as distinct calls with distinct results. Arguments to the cached function must be hashable. View the cache statistics named tuple (hits, misses, maxsize, currsize) with f.cache_info(). Clear the cache and statistics with f.cache_clear(). Access the underlying function with f.__wrapped__. See: #Least_recently_used_(LRU) """ # Users should only access the lru_cache through its public API: # cache_info, cache_clear, and f.__wrapped__ # The internals of the lru_cache are encapsulated for thread safety and # to allow the implementation to change (including a possible C version). if isinstance(maxsize, int): # Negative maxsize is treated as 0 if maxsize < 0: maxsize = 0 elif callable(maxsize) and isinstance(typed, bool): # The user_function was passed in directly via the maxsize argument user_function, maxsize = maxsize, 128 wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo) wrapper.cache_parameters = lambda : {'maxsize': maxsize, 'typed': typed} return update_wrapper(wrapper, user_function) elif maxsize is not None: raise TypeError( 'Expected first argument to be an integer, a callable, or None') def decorating_function(user_function): wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo) wrapper.cache_parameters = lambda : {'maxsize': maxsize, 'typed': typed} return update_wrapper(wrapper, user_function) return decorating_function

这段代码中有如下几个关键点

关键字参数

maxsize 表示缓存容量,如果为 None 表示容量不设限, typed 表示是否区分参数类型,注释中也给出了解释,如果 typed == True,那么 f(3) 和 f(3.0) 会被认为是不同的函数调用。

第 24 行的条件分支

如果 lru_cache 的第一个参数是可调用的,直接返回 wrapper,也就是把 lru_cache 当做不带参数的装饰器,这是 Python 3.8 才有的特性,也就是说在 Python 3.8 及之后的版本中我们可以用下面的方式使用 lru_cache,可能是为了防止程序员在使用 lru_cache 的时候忘记加括号。

import functools # 注意 lru_cache 后面没有括号, # 证明这是将其当做不带参数的装饰器 @functools.lru_cache def factorial(n): print(f"计算 {n} 的阶乘") return 1 if n <= 1 else n * factorial(n - 1)

注意,Python 3.8 之前的版本运行上面代码会报错:TypeError: Expected maxsize to be an integer or None。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpzzfw.html