308 lines
3.2 KiB
Markdown
308 lines
3.2 KiB
Markdown
|
|
# 🔑 Redis `HSET` and Related Hash Commands
|
|
|
|
## 1. `HSET`
|
|
|
|
* **Purpose**: Set the value of one or more fields in a hash.
|
|
* **Syntax**:
|
|
|
|
```bash
|
|
HSET key field value [field value ...]
|
|
```
|
|
* **Return**:
|
|
|
|
* Integer: number of fields that were newly added.
|
|
* **RESP Protocol**:
|
|
|
|
```
|
|
*4
|
|
$4
|
|
HSET
|
|
$3
|
|
key
|
|
$5
|
|
field
|
|
$5
|
|
value
|
|
```
|
|
|
|
(If multiple field-value pairs: `*6`, `*8`, etc.)
|
|
|
|
---
|
|
|
|
## 2. `HSETNX`
|
|
|
|
* **Purpose**: Set the value of a hash field only if it does **not** exist.
|
|
* **Syntax**:
|
|
|
|
```bash
|
|
HSETNX key field value
|
|
```
|
|
* **Return**:
|
|
|
|
* `1` if field was set.
|
|
* `0` if field already exists.
|
|
* **RESP Protocol**:
|
|
|
|
```
|
|
*4
|
|
$6
|
|
HSETNX
|
|
$3
|
|
key
|
|
$5
|
|
field
|
|
$5
|
|
value
|
|
```
|
|
|
|
---
|
|
|
|
## 3. `HGET`
|
|
|
|
* **Purpose**: Get the value of a hash field.
|
|
* **Syntax**:
|
|
|
|
```bash
|
|
HGET key field
|
|
```
|
|
* **Return**:
|
|
|
|
* Bulk string (value) or `nil` if field does not exist.
|
|
* **RESP Protocol**:
|
|
|
|
```
|
|
*3
|
|
$4
|
|
HGET
|
|
$3
|
|
key
|
|
$5
|
|
field
|
|
```
|
|
|
|
---
|
|
|
|
## 4. `HGETALL`
|
|
|
|
* **Purpose**: Get all fields and values in a hash.
|
|
* **Syntax**:
|
|
|
|
```bash
|
|
HGETALL key
|
|
```
|
|
* **Return**:
|
|
|
|
* Array of `[field1, value1, field2, value2, ...]`.
|
|
* **RESP Protocol**:
|
|
|
|
```
|
|
*2
|
|
$7
|
|
HGETALL
|
|
$3
|
|
key
|
|
```
|
|
|
|
---
|
|
|
|
## 5. `HMSET` (⚠️ Deprecated, use `HSET`)
|
|
|
|
* **Purpose**: Set multiple field-value pairs.
|
|
* **Syntax**:
|
|
|
|
```bash
|
|
HMSET key field value [field value ...]
|
|
```
|
|
* **Return**:
|
|
|
|
* Always `OK`.
|
|
* **RESP Protocol**:
|
|
|
|
```
|
|
*6
|
|
$5
|
|
HMSET
|
|
$3
|
|
key
|
|
$5
|
|
field
|
|
$5
|
|
value
|
|
$5
|
|
field2
|
|
$5
|
|
value2
|
|
```
|
|
|
|
---
|
|
|
|
## 6. `HMGET`
|
|
|
|
* **Purpose**: Get values of multiple fields.
|
|
* **Syntax**:
|
|
|
|
```bash
|
|
HMGET key field [field ...]
|
|
```
|
|
* **Return**:
|
|
|
|
* Array of values (bulk strings or nils).
|
|
* **RESP Protocol**:
|
|
|
|
```
|
|
*4
|
|
$5
|
|
HMGET
|
|
$3
|
|
key
|
|
$5
|
|
field1
|
|
$5
|
|
field2
|
|
```
|
|
|
|
---
|
|
|
|
## 7. `HDEL`
|
|
|
|
* **Purpose**: Delete one or more fields from a hash.
|
|
* **Syntax**:
|
|
|
|
```bash
|
|
HDEL key field [field ...]
|
|
```
|
|
* **Return**:
|
|
|
|
* Integer: number of fields removed.
|
|
* **RESP Protocol**:
|
|
|
|
```
|
|
*3
|
|
$4
|
|
HDEL
|
|
$3
|
|
key
|
|
$5
|
|
field
|
|
```
|
|
|
|
---
|
|
|
|
## 8. `HEXISTS`
|
|
|
|
* **Purpose**: Check if a field exists.
|
|
* **Syntax**:
|
|
|
|
```bash
|
|
HEXISTS key field
|
|
```
|
|
* **Return**:
|
|
|
|
* `1` if exists, `0` if not.
|
|
* **RESP Protocol**:
|
|
|
|
```
|
|
*3
|
|
$7
|
|
HEXISTS
|
|
$3
|
|
key
|
|
$5
|
|
field
|
|
```
|
|
|
|
---
|
|
|
|
## 9. `HKEYS`
|
|
|
|
* **Purpose**: Get all field names in a hash.
|
|
* **Syntax**:
|
|
|
|
```bash
|
|
HKEYS key
|
|
```
|
|
* **Return**:
|
|
|
|
* Array of field names.
|
|
* **RESP Protocol**:
|
|
|
|
```
|
|
*2
|
|
$5
|
|
HKEYS
|
|
$3
|
|
key
|
|
```
|
|
|
|
---
|
|
|
|
## 10. `HVALS`
|
|
|
|
* **Purpose**: Get all values in a hash.
|
|
* **Syntax**:
|
|
|
|
```bash
|
|
HVALS key
|
|
```
|
|
* **Return**:
|
|
|
|
* Array of values.
|
|
* **RESP Protocol**:
|
|
|
|
```
|
|
*2
|
|
$5
|
|
HVALS
|
|
$3
|
|
key
|
|
```
|
|
|
|
---
|
|
|
|
## 11. `HLEN`
|
|
|
|
* **Purpose**: Get number of fields in a hash.
|
|
* **Syntax**:
|
|
|
|
```bash
|
|
HLEN key
|
|
```
|
|
* **Return**:
|
|
|
|
* Integer: number of fields.
|
|
* **RESP Protocol**:
|
|
|
|
```
|
|
*2
|
|
$4
|
|
HLEN
|
|
$3
|
|
key
|
|
```
|
|
|
|
|
|
|
|
## 12. `HSCAN`
|
|
|
|
* **Purpose**: Iterate fields/values of a hash (cursor-based scan).
|
|
* **Syntax**:
|
|
|
|
```bash
|
|
HSCAN key cursor [MATCH pattern] [COUNT count]
|
|
```
|
|
* **Return**:
|
|
|
|
* Array: `[new-cursor, [field1, value1, ...]]`
|
|
* **RESP Protocol**:
|
|
|
|
```
|
|
*3
|
|
$5
|
|
HSCAN
|
|
$3
|
|
key
|
|
$1
|
|
0
|
|
```
|