Working with String Values
1. Setting String Values
| Command | Options |
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
| Command | Description |
GET key | Value or nil |
GETDEL key | Get and delete atomically 6.2+ |
GETEX key [EX|PX|EXAT|PXAT|PERSIST] | Get and update TTL 6.2+ |
3. Setting Multiple Values
| Command | Behavior |
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
| Command | Equivalent |
SETEX key seconds value | SET key value EX seconds |
PSETEX key ms value | SET key value PX ms |
5. Setting if Not Exists
| Command | Returns |
SETNX key value | 1 set, 0 exists |
SET key value NX | Preferred — supports TTL in one shot |
6. Getting and Setting
| Command | Returns |
SET key value GET | Old value, or nil if absent 6.2+ |
GETSET key value | DEPRECATED use SET ... GET |
7. Appending to Strings
| Command | Returns |
APPEND key value | New string length; creates key if missing |
8. Getting String Length
| Command | Returns |
STRLEN key | Byte length; 0 if missing |
9. Getting Substring
| Command | Range |
GETRANGE key start end | Inclusive; supports negatives (-1 = end) |
SETRANGE key offset value | Overwrite at offset; zero-pads if needed |
10. Incrementing Numbers
| Command | Description |
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
| Command | Description |
DECR key | -1 |
DECRBY key n | -n |
12. Incrementing Floats
| Command | Notes |
INCRBYFLOAT key amount | IEEE 754 double; supports negative amounts; precision up to 17 digits |
Warning: Float math is not associative — sums of many INCRBYFLOAT calls can drift.