0Pricing
Flask Academy · 课时

设置 Flask-Caching

选择后端并初始化缓存

设置 Flask-Caching 是 CoddyKit 上的免费 Flask Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flask Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flask Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Caching Matters

When the same answer gets computed over and over, you waste time. Caching stores a result once and serves it instantly next time. ⚡

What Flask-Caching Adds

Flask-Caching is an extension that bolts a cache layer onto your app, so you can save view output or function results with almost no code.

Install the Extension

You start by installing the package from PyPI. This pulls in Flask-Caching and makes the Cache class available to import.

pip install Flask-Caching

Import the Cache Class

The whole extension centers on one object. You import the Cache class and will create a single instance to manage all cached data.

from flask_caching import Cache

Pick a Cache Backend

A backend decides where cached data lives. SimpleCache keeps it in memory, while Redis or Memcached share it across many processes.

SimpleCache for Local Dev

For learning and tiny apps, SimpleCache is perfect: it stores values in the process memory with zero extra services to run.

config = {"CACHE_TYPE": "SimpleCache"}

Configure with a Dict

You tell Flask-Caching how to behave through config keys like CACHE_TYPE. These can sit in your app config or a plain dict.

app.config["CACHE_TYPE"] = "SimpleCache"

Create the Cache Object

You build one Cache instance for the whole app. Passing the app now binds the cache immediately so it is ready to use.

cache = Cache(app)

The init_app Pattern

With an app factory you defer binding. You create the cache empty, then call init_app later once the app exists.

cache = Cache()
cache.init_app(app)

Set a Default Timeout

The CACHE_DEFAULT_TIMEOUT key sets how many seconds entries live before they expire, so stale data does not linger forever.

app.config["CACHE_DEFAULT_TIMEOUT"] = 300

Verify It Works

A quick smoke test: store a value and read it straight back. If both lines agree, your cache is wired up correctly. ✅

cache.set("k", 42)
print(cache.get("k"))

Quick Check

You want a cache that needs no external server during local development.

Recap: Setup

You installed Flask-Caching, picked a backend, created one Cache object, and set a default timeout. The cache is ready to use. 🎉

常见问题解答

「设置 Flask-Caching」课时是免费的吗?

是的 — 「设置 Flask-Caching」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flask Academy 课程的其余内容,请升级到 CoddyKit PRO。 Flask Academy 课程共包含 4 节课。

「设置 Flask-Caching」这节课中我会学到什么?

选择后端并初始化缓存 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Flask Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Flask Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「设置 Flask-Caching」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Flask Academy 课中编写并运行代码吗?

能。每节 Flask Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 设置 Flask-Caching
  2. 使用 cached 缓存视图
  3. 记忆化开销大的函数
  4. 使缓存条目失效并过期
← 返回 Flask Academy