การหมดอายุของคีย์และ TTL ใน Redis
เรียนรู้ว่า Redis ช่วยผูกค่าระยะเวลาการใช้งานเข้ากับคีย์เพื่อให้หมดอายุโดยอัตโนมัติได้อย่างไร คำสั่งใดควบคุม TTL และ Redis ตัดสินใจลบคีย์ที่หมดอายุเมื่อใด
การหมดอายุของคีย์และ TTL ใน Redis เป็นบทเรียน Redis Caching & Messaging (Pub/Sub, Streams) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 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.
คำถามที่พบบ่อย
บทเรียน “การหมดอายุของคีย์และ TTL ใน Redis” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การหมดอายุของคีย์และ TTL ใน Redis” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Redis Caching & Messaging (Pub/Sub, Streams) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Redis Caching & Messaging (Pub/Sub, Streams) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การหมดอายุของคีย์และ TTL ใน Redis”
เรียนรู้ว่า Redis ช่วยผูกค่าระยะเวลาการใช้งานเข้ากับคีย์เพื่อให้หมดอายุโดยอัตโนมัติได้อย่างไร คำสั่งใดควบคุม TTL และ Redis ตัดสินใจลบคีย์ที่หมดอายุเมื่อใด คุณปฏิบัติ Redis Caching & Messaging (Pub/Sub, Streams) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Redis Caching & Messaging (Pub/Sub, Streams) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Redis Caching & Messaging (Pub/Sub, Streams) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การหมดอายุของคีย์และ TTL ใน Redis” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Redis Caching & Messaging (Pub/Sub, Streams) นี้ได้ไหม
ได้ บทเรียน Redis Caching & Messaging (Pub/Sub, Streams) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่ Redis
- พื้นฐาน Redis CLI
- โครงสร้างข้อมูลหลักของ Redis
- การหมดอายุของคีย์และ TTL ใน Redis