TTL และการหมดอายุใน Redis
เรียนรู้ว่า Redis จัดการการหมดอายุของคีย์อย่างไร ตั้งแต่การกำหนด TTL คำสั่ง EXPIRE และ SETEX ไปจนถึงการทำงานของการหมดอายุแบบขี้เกียจและแบบทำงานเชิงรุก รวมถึงนโยบายขับข้อมูลออกของ maxmemory ที่ทำงานร่วมกับ TTL
TTL และการหมดอายุใน Redis เป็นบทเรียน Caching Strategies: Redis + CDN + Edge Computing ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Caching Strategies: Redis + CDN + Edge Computing และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Caching Strategies: Redis + CDN + Edge Computing มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Expire Cache Keys?
Cached data goes stale. Time To Live (TTL) lets Redis automatically remove a key after a set duration, so your cache stays reasonably fresh without manual cleanup.
Setting a TTL with EXPIRE
The EXPIRE command sets a timeout in seconds on an existing key. After that time, Redis deletes the key automatically.
SET user:1 "Alice"
EXPIRE user:1 60
TTL user:1SET with EX in One Step
You can set a value and its TTL together. SET key value EX seconds (or the older SETEX) avoids a race between writing and expiring.
SET session:abc "data" EX 300Checking Remaining TTL
The TTL command returns seconds left. PTTL returns milliseconds. Special return values matter:
-1: key exists but has no expiry-2: key does not exist
def interpret_ttl(value):
if value == -1:
return 'no expiry set'
if value == -2:
return 'key does not exist'
return str(value) + 's remaining'
print(interpret_ttl(-2))
print(interpret_ttl(45))Removing an Expiry
The PERSIST command removes the TTL from a key, making it permanent again. Useful when a value should stop expiring after some condition is met.
PERSIST user:1Lazy Expiration
Redis does not check every key constantly. With lazy expiry, an expired key is only removed when someone tries to access it. Until then it lingers in memory.
Active Expiration
To reclaim memory from keys nobody touches, Redis also runs a background sampler that periodically removes a batch of expired keys. This is active expiry. Together with lazy expiry it keeps memory in check.
TTL vs Eviction
TTL is voluntary removal at a set time. Eviction happens when Redis hits its maxmemory limit and must drop keys to make room. They are different mechanisms that often work together.
volatile vs allkeys Policies
Eviction policies decide what to drop under memory pressure:
volatile-lru: evict least-recently-used keys that have a TTLallkeys-lru: evict LRU among all keys
For a pure cache, allkeys policies are common.
Choosing TTL Values
Pick TTLs from the data's tolerance for staleness: seconds for fast-changing prices, hours for catalogs, days for rarely-changing config. Add jitter to avoid synchronized mass expiry.
import random
base = 3600
print('TTL:', base + random.randint(0, 300), 'seconds')Common Pitfall: No TTL
Forgetting to set a TTL turns a cache into an ever-growing store that eventually exhausts memory. For cache use cases, almost every key should have an expiry or rely on an allkeys eviction policy.
Quick Check
What does the Redis command TTL mykey return if the key exists but has no expiration set?
Recap
You learned TTL and expiration in Redis:
- EXPIRE / SET ... EX set timeouts; TTL/PTTL inspect them; PERSIST removes them.
- Redis uses lazy plus active expiration.
- Eviction policies (volatile vs allkeys) handle memory pressure.
- Always set TTLs (or an allkeys policy) for cache keys.
Good expiration keeps a cache both fresh and bounded.
เรียนรู้ Caching Strategies: Redis + CDN + Edge Computing ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “TTL และการหมดอายุใน Redis” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “TTL และการหมดอายุใน Redis” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Caching Strategies: Redis + CDN + Edge Computing ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Caching Strategies: Redis + CDN + Edge Computing มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “TTL และการหมดอายุใน Redis”
เรียนรู้ว่า Redis จัดการการหมดอายุของคีย์อย่างไร ตั้งแต่การกำหนด TTL คำสั่ง EXPIRE และ SETEX ไปจนถึงการทำงานของการหมดอายุแบบขี้เกียจและแบบทำงานเชิงรุก รวมถึงนโยบายขับข้อมูลออกของ maxmemory ที่ทำงานร่… คุณปฏิบัติ Caching Strategies: Redis + CDN + Edge Computing ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Caching Strategies: Redis + CDN + Edge Computing หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Caching Strategies: Redis + CDN + Edge Computing บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “TTL และการหมดอายุใน Redis” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Caching Strategies: Redis + CDN + Edge Computing นี้ได้ไหม
ได้ บทเรียน Caching Strategies: Redis + CDN + Edge Computing ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่การแคชด้วยรีดิส
- โครงสร้างข้อมูลของรีดิสสำหรับแคช
- การดำเนินการแคชพื้นฐานในรีดิส
- TTL และการหมดอายุใน Redis