Scadenza delle chiavi e TTL in Redis
Impari come Redis permette di associare valori time-to-live alle chiavi affinché scadano automaticamente, quali comandi controllano il TTL e come Redis decide quando rimuovere le chiavi scadute.
Scadenza delle chiavi e TTL in Redis è una lezione Redis Caching & Messaging (Pub/Sub, Streams) gratuita su CoddyKit. Questa è la lezione 4 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 Redis Caching & Messaging (Pub/Sub, Streams), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Redis Caching & Messaging (Pub/Sub, Streams) include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Why Keys Need to Expire
Redis often holds temporary data — sessions, one-time codes, cached responses. Expiration lets a key delete itself after a set time so memory stays lean.
Setting a TTL with EXPIRE
EXPIRE attaches a time-to-live in seconds to an existing key. Once the clock runs out, the key vanishes. See the example below.
SET session:42 "active"
EXPIRE session:42 3600Set and Expire in One Step
Set value and TTL together with SET ... EX — safer than two separate commands, since there's no window where the key has no expiry.
SET otp:99 "123456" EX 300Checking Remaining Time
TTL returns the seconds left on a key. It returns -1 if the key has no expiry, and -2 if the key doesn't exist at all.
TTL session:42Millisecond Precision
For finer control, PEXPIRE and PTTL work in milliseconds instead of seconds. The code below sets and reads a millisecond TTL.
PEXPIRE otp:99 1500
PTTL otp:99Expiring at a Specific Time
EXPIREAT takes a Unix timestamp, so a key can die at an exact wall-clock moment — like precisely at midnight.
EXPIREAT daily:report 1893456000Removing an Expiry
PERSIST strips a key's TTL, making it permanent again — handy when temporary data gets promoted to durable.
PERSIST session:42
TTL session:42How Redis Deletes Expired Keys
Redis removes expired keys two ways: lazy deletion checks on access, and active deletion samples keys in a background task and purges expired ones.
Writes Can Clear TTL
Watch out: a plain SET (no EX) overwrites a value and clears its existing TTL. Modifying commands like APPEND or INCR keep the TTL intact.
Common Use Cases
TTL powers real patterns: auto-expiring sessions, rate-limit counters, one-time passwords and verification tokens, and caches that refresh after N seconds.
Keyspace Notifications on Expiry
Redis can publish an event when a key expires via keyspace notifications, so your app can react. Enable expired-key events, then subscribe. See below.
CONFIG SET notify-keyspace-events Ex
SUBSCRIBE __keyevent@0__:expiredQuick Check
Test your expiration knowledge.
Recap
Recap: EXPIRE and SET ... EX set a TTL, TTL reads it (-1 none, -2 missing), PEXPIRE/EXPIREAT give finer control, PERSIST removes it, plain overwrites clear it.
Domande Frequenti
La lezione «Scadenza delle chiavi e TTL in Redis» è gratuita?
Sì — il testo completo di «Scadenza delle chiavi e TTL in 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 Redis Caching & Messaging (Pub/Sub, Streams), passa a CoddyKit PRO. Il corso Redis Caching & Messaging (Pub/Sub, Streams) include 4 lezioni in totale.
Cosa imparerò in «Scadenza delle chiavi e TTL in Redis»?
Impari come Redis permette di associare valori time-to-live alle chiavi affinché scadano automaticamente, quali comandi controllano il TTL e come Redis decide quando rimuovere le chiavi scadute. Eserciti Redis Caching & Messaging (Pub/Sub, Streams) 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 Redis Caching & Messaging (Pub/Sub, Streams)?
Non è richiesta alcuna esperienza precedente. Redis Caching & Messaging (Pub/Sub, Streams) su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Scadenza delle chiavi e TTL in 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 Redis Caching & Messaging (Pub/Sub, Streams)?
Sì. Ogni lezione Redis Caching & Messaging (Pub/Sub, Streams) 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
- Introduzione a Redis
- Nozioni di base sulla Redis CLI
- Strutture dati fondamentali di Redis
- Scadenza delle chiavi e TTL in Redis