0Pricing
Redis Caching & Messaging (Pub/Sub, Streams) · 课时

Redis 中的密钥过期与 TTL

学习 Redis 如何为密钥附加生存时间值,使其自动过期,了解控制 TTL 的命令,以及 Redis 如何决定何时删除过期密钥

Redis 中的密钥过期与 TTL 是 CoddyKit 上的免费 Redis Caching & Messaging (Pub/Sub, Streams) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Redis Caching & Messaging (Pub/Sub, Streams) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。

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

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 3600

Set 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 300

Checking 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:42

Millisecond 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:99

Expiring 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 1893456000

Removing 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:42

How 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__:expired

Quick 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.

常见问题解答

「Redis 中的密钥过期与 TTL」课时是免费的吗?

是的 — 「Redis 中的密钥过期与 TTL」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Redis Caching & Messaging (Pub/Sub, Streams) 课程的其余内容,请升级到 CoddyKit PRO。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。

「Redis 中的密钥过期与 TTL」这节课中我会学到什么?

学习 Redis 如何为密钥附加生存时间值,使其自动过期,了解控制 TTL 的命令,以及 Redis 如何决定何时删除过期密钥 你通过在浏览器中直接运行的动手代码来练习 Redis Caching & Messaging (Pub/Sub, Streams),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Redis Caching & Messaging (Pub/Sub, Streams) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Redis Caching & Messaging (Pub/Sub, Streams) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「Redis 中的密钥过期与 TTL」课时需要多长时间?

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

我能在这节 Redis Caching & Messaging (Pub/Sub, Streams) 课中编写并运行代码吗?

能。每节 Redis Caching & Messaging (Pub/Sub, Streams) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Redis 简介
  2. Redis CLI 基础
  3. Redis 核心数据结构
  4. Redis 中的密钥过期与 TTL
← 返回 Redis Caching & Messaging (Pub/Sub, Streams)