Database / REDIS
What is the purpose of the EXPIRE command?
EXPIRE attaches a time-to-live to an existing key, after which Redis automatically removes it — useful for data that should only be valid temporarily, like a session token, a rate-limit counter, or a cached query result that shouldn't be served stale forever.
SET session:abc123 "user-data" EXPIRE session:abc123 3600 # expires in 3600 seconds (1 hour) TTL session:abc123 # check remaining time-to-live
Related commands round out TTL management: PEXPIRE sets the TTL in milliseconds for finer granularity, EXPIREAT/PEXPIREAT set an absolute expiration timestamp instead of a relative duration, and PERSIST removes a key's TTL entirely, making it permanent again. A key's TTL is also cleared if the key is overwritten with a plain SET (unless the KEEPTTL option is used), which is a common source of confusion when a value update unexpectedly makes a previously-expiring key permanent.
More Related questions...
