# 函数缓存

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

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://eastlakeside.gitbook.io/interpy-zh/func_caching.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
