0Pricing
Redis Caching & Messaging (Pub/Sub, Streams) · درس

انتهاء صلاحية المفاتيح وTTL في Redis

تعلّم كيف يتيح Redis إرفاق قيم مدة البقاء بالمفاتيح لتنتهي صلاحيتها تلقائيًا، وتعرّف على الأوامر التي تتحكم في TTL وكيف يقرر Redis إزالة المفاتيح المنتهية

انتهاء صلاحية المفاتيح وTTL في Redis درس مجاني في Redis Caching & Messaging (Pub/Sub, Streams) على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 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.

الأسئلة الشائعة

هل درس «انتهاء صلاحية المفاتيح وTTL في Redis» مجاني؟

نعم — نص درس «انتهاء صلاحية المفاتيح وTTL في Redis» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. مقدمة إلى Redis
  2. أساسيات Redis CLI
  3. هياكل البيانات الأساسية في Redis
  4. انتهاء صلاحية المفاتيح وTTL في Redis
← العودة إلى Redis Caching & Messaging (Pub/Sub, Streams)