Configure Flask-Caching
Escolha um backend e inicialize o cache.
Configure Flask-Caching é uma aula grátis de Flask Academy no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Flask Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Flask Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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-CachingImport 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 CachePick 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"] = 300Verify 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. 🎉
Perguntas Frequentes
A aula “Configure Flask-Caching” é grátis?
Sim — o texto completo de “Configure Flask-Caching” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Flask Academy, atualize para CoddyKit PRO. O curso de Flask Academy inclui 4 aulas no total.
O que vou aprender em “Configure Flask-Caching”?
Escolha um backend e inicialize o cache. Você pratica Flask Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Flask Academy?
Nenhuma experiência prévia é necessária. Flask Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.
Quanto tempo leva a aula “Configure Flask-Caching”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Flask Academy?
Sim. Cada aula de Flask Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Configure Flask-Caching
- Armazene uma visualização em cache com cached
- Memorize funções dispendiosas
- Invalide e faça entradas do cache expirarem