Hashes
Hashes are maps of field-value pairs, ideal for representing objects. Each hash can store over 4 billion field-value pairs.
HSET / HGET / HDEL
Set one or more fields, retrieve a single field, or delete fields from a hash.
HSET key field value [field value ...]
HGET key field
HDEL key field [field ...]> HSET user:1 name "alice" email "alice@example.com" age "30"
(integer) 3
> HGET user:1 name
"alice"
> HDEL user:1 age
(integer) 1HGETALL / HKEYS / HVALS
Retrieve all fields and values, just the field names, or just the values from a hash.
> HGETALL user:1
1) "name"
2) "alice"
3) "email"
4) "alice@example.com"
> HKEYS user:1
1) "name"
2) "email"
> HVALS user:1
1) "alice"
2) "alice@example.com"HINCRBY / HINCRBYFLOAT
Atomically increment a hash field by an integer or float value. Creates the field with the increment as the value if it does not exist.
> HSET product:1 views 0 price 9.99
(integer) 2
> HINCRBY product:1 views 1
(integer) 1
> HINCRBY product:1 views 5
(integer) 6
> HINCRBYFLOAT product:1 price 0.50
"10.49"HMSET / HMGET
Set or get multiple fields in a single call. HMSET is functionally identical to HSET with multiple fields but remains available for compatibility.
> HMSET config:app theme "dark" lang "en" tz "UTC"
OK
> HMGET config:app theme lang
1) "dark"
2) "en"HEXISTS / HLEN / HSETNX
Check if a field exists, count total fields, or set a field only if it does not already exist.
> HEXISTS user:1 name
(integer) 1
> HEXISTS user:1 phone
(integer) 0
> HLEN user:1
(integer) 2
> HSETNX user:1 name "bob"
(integer) 0
> HSETNX user:1 phone "555-1234"
(integer) 1HSCAN
Incrementally iterate over fields and values in a hash. Use MATCH to filter by pattern and COUNT to hint at the number of elements to return per call.
> HSCAN user:1 0 MATCH "e*" COUNT 10
1) "0"
2) 1) "email"
2) "alice@example.com"Command Reference
| Command | Description |
|---|---|
| HSET | Set one or more field-value pairs |
| HGET | Get the value of a field |
| HDEL | Delete one or more fields |
| HGETALL | Get all fields and values |
| HKEYS | Get all field names |
| HVALS | Get all values |
| HLEN | Get the number of fields |
| HEXISTS | Check if a field exists |
| HINCRBY | Increment a field's integer value |
| HINCRBYFLOAT | Increment a field's float value |
| HMSET | Set multiple fields (same as HSET) |
| HMGET | Get values of multiple fields |
| HSETNX | Set a field only if it does not exist |
| HSTRLEN | Get the length of a field's value |
| HRANDFIELD | Return random fields from a hash |
| HSCAN | Incrementally iterate over fields |