> For the complete documentation index, see [llms.txt](https://eastlakeside.gitbook.io/interpy-zh/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://eastlakeside.gitbook.io/interpy-zh/func_caching.md).

# 函数缓存

函数缓存允许我们将一个函数对于给定参数的返回值缓存起来。\
当一个 I/O 密集的函数被频繁使用相同的参数调用的时候，函数缓存可以节约时间。\
在 Python 3.2 版本以前我们只有写一个自定义的实现。在 Python 3.2 以后版本，有个 `lru_cache` 的装饰器，允许我们将一个函数的返回值快速地缓存或取消缓存。

我们来看看，Python 3.2 前后的版本分别如何使用它。
