Introduction
Sometimes you want to store data that only lasts for a short time and then disappears automatically. This helps keep your database clean and saves space.
SET key value EX seconds
SET session123 user_data EX 300SET temp_code 123456 EX 60
SET cache_page "<html>...</html>" EX 3600
SET login_token abcdef EX 120
GET login_token
TTL login_tokenEX option do when used with the Redis SET command?SET command stores a key-value pair. The EX option specifies the expiration time in seconds.EX is used, the key will automatically be deleted after the given time passes.session with value abc123 that expires in 120 seconds in Redis?SET key value EX seconds.TTL user:123 immediately after execution?
SET user:123 "John" EX 60 TTL user:123
user:123 is set with a TTL of 60 seconds.TTL command returns the remaining time to live in seconds. Immediately after setting, it should be 60.SET temp_key "data" EX 30 EXPIRE temp_key 60 TTL temp_keyWhat will
TTL temp_key return?temp_key initially has 30 seconds TTL.EXPIRE temp_key 60 resets the TTL to 60 seconds.TTL temp_key returns 60 seconds remaining.SET login_token abc EX 300 sets the token with 300 seconds TTL.SLEEP 180 simulates waiting. Then TTL login_token shows remaining time, confirming token still exists.Pass the test and this pattern moves unknown