Database / ValKey Interview questions
How do you apply a TTL to a key in Valkey?
A time-to-live can be attached when a key is created or added afterward, and Valkey will automatically remove the key once it expires.
SET session:1001 "user-data" EXPIRE session:1001 3600 # expire in 3600 seconds SET session:1002 "user-data" EX 3600 # set + TTL in one command TTL session:1001 # check remaining seconds PERSIST session:1001 # remove the expiration
PEXPIRE/PTTL offer millisecond precision, and EXPIRE supports NX/XX/GT/LT flags to conditionally set a TTL relative to any existing one.
More Related questions...