Working with String Values

1. Setting String Values

CommandOptions
SET key value [EX s|PX ms|EXAT ts|PXAT ms] [NX|XX] [KEEPTTL] [GET] [IDLE s]Universal setter; GET returns old value 7.0+

Example: Atomic SET with TTL and condition

SET session:abc "data" EX 3600 NX
SET counter 0 KEEPTTL
SET key newval GET

2. Getting String Values

CommandDescription
GET keyValue or nil
GETDEL keyGet and delete atomically 6.2+
GETEX key [EX|PX|EXAT|PXAT|PERSIST]Get and update TTL 6.2+

3. Setting Multiple Values

CommandBehavior
MSET k1 v1 k2 v2 ...Atomic multi-set, always overwrites
MSETNX k1 v1 ...Only if NONE of the keys exist
MGET k1 k2 ...Returns array, nil for missing

4. Setting with Expiration

CommandEquivalent
SETEX key seconds valueSET key value EX seconds
PSETEX key ms valueSET key value PX ms

5. Setting if Not Exists

CommandReturns
SETNX key value1 set, 0 exists
SET key value NXPreferred — supports TTL in one shot

6. Getting and Setting

CommandReturns
SET key value GETOld value, or nil if absent 6.2+
GETSET key valueDEPRECATED use SET ... GET

7. Appending to Strings

CommandReturns
APPEND key valueNew string length; creates key if missing

8. Getting String Length

CommandReturns
STRLEN keyByte length; 0 if missing

9. Getting Substring

CommandRange
GETRANGE key start endInclusive; supports negatives (-1 = end)
SETRANGE key offset valueOverwrite at offset; zero-pads if needed

10. Incrementing Numbers

CommandDescription
INCR key+1, returns new value
INCRBY key n+n, n can be negative

Example: Atomic counter

INCR page:views:home
INCRBY user:42:credits 50

11. Decrementing Numbers

CommandDescription
DECR key-1
DECRBY key n-n

12. Incrementing Floats

CommandNotes
INCRBYFLOAT key amountIEEE 754 double; supports negative amounts; precision up to 17 digits
Warning: Float math is not associative — sums of many INCRBYFLOAT calls can drift.