Typorithm · easy

SET, GET & TTL

redis-set-getredissetttlexpire
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn SET, GET & TTL

0%

Strings & Keys

SET stores a string value at a key and GET reads it back; the EX option attaches a seconds-based expiry, TTL reports the remaining life, and DEL removes a key entirely.

Redis keys are flat strings and values default to strings, so SET user:1 "Ada" simply stores the text Ada under the key user:1, and GET user:1 reads it straight back.

Adding EX 3600 to a SET gives the key a time-to-live of 3600 seconds; once that window elapses Redis evicts the key automatically. TTL session:42 reports how many seconds remain.

DEL user:1 removes a key and its value on the spot. TTL also doubles as a probe: it returns -1 for a key with no expiry and -2 for a key that no longer exists.

01

SET writes a string value; GET reads it.

02

EX <seconds> on SET sets a time-to-live.

03

TTL reports remaining life; DEL removes a key.

SET user:1 "Ada"
GET user:1
SET session:42 "tok" EX 3600
TTL session:42
DEL user:1

Read the implementation

Connect each part of the code to the concept before you type.

1. Store a value

SET writes a string value to a key, creating or overwriting it.

SET user:1 "Ada"

2. Read a value

GET returns the string value stored at a key, or nil if it does not exist.

GET user:1

3. Set with expiry

The EX option gives the key a time-to-live in seconds — here 3600 (one hour).

SET session:42 "tok" EX 3600

4. Query remaining life

TTL returns the seconds left before a key expires (-1 if it has no expiry, -2 if it is gone).

TTL session:42

5. Remove a key

DEL deletes a key and its value immediately.

DEL user:1

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

SET, GET & TTL: Lesson & Practice · Typorithm | ApplyDjinn