0Pricing
Django Academy · Lezione

Backend di cache e Redis

Configurare un backend di cache per la produzione

Backend di cache e Redis è una lezione Django Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Django Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Django Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

What a Cache Saves You

A cache stores expensive results so the next request can skip the slow work and reply almost instantly. ⚡

Where Caches Live

Django talks to a cache through a backend, a pluggable adapter that decides where your cached data is physically stored.

The CACHES Setting

You configure caching in the CACHES dict in settings.py, always with a key named default that Django reads first.

CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
    }
}

Local Memory for Dev

The default LocMemCache keeps data in your process memory. It is fine for development but is not shared across servers.

Why Redis in Production

For real apps, point the backend at Redis, a fast in-memory store that many web servers can share at once.

Configuring the Redis Backend

Set BACKEND to RedisCache and give it a LOCATION pointing at your Redis server URL.

CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.redis.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379",
    }
}

Talking to the Cache

Import the shared cache object from django.core.cache to read and write values from anywhere in your code.

from django.core.cache import cache

set and get

Use set to store a value under a key, then get to read it back later in another request.

cache.set("answer", 42)
value = cache.get("answer")

Timeouts Keep Data Fresh

The third argument to set is a timeout in seconds. After it passes, the key expires and get returns None.

cache.set("answer", 42, 300)

Handling Cache Misses

A missing key is a cache miss. Pass a default to get so your code has a safe value to fall back on.

value = cache.get("answer", "none yet")

Other Backends Exist

Beyond Redis, Django ships database and file-based backends, handy when you cannot run a separate memory server.

Quick Check

Time to confirm where production caches usually live.

Recap

You met the cache backend: configure CACHES, use LocMem in dev and Redis in production, then set and get values with a timeout. 🎉

Domande Frequenti

La lezione «Backend di cache e Redis» è gratuita?

Sì — il testo completo di «Backend di cache e Redis» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Django Academy, passa a CoddyKit PRO. Il corso Django Academy include 4 lezioni in totale.

Cosa imparerò in «Backend di cache e Redis»?

Configurare un backend di cache per la produzione Eserciti Django Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Django Academy?

Non è richiesta alcuna esperienza precedente. Django Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.

Quanto tempo richiede la lezione «Backend di cache e Redis»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Django Academy?

Sì. Ogni lezione Django Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Backend di cache e Redis
  2. Caching per vista e per sito
  3. Caching dei frammenti di template
  4. API cache di basso livello e invalidazione
← Torna a Django Academy