3.2 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	🔑 Redis HSET and Related Hash Commands
1. HSET
- 
Purpose: Set the value of one or more fields in a hash. 
- 
Syntax: 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: HSETNX key field value
- 
Return: - 1if field was set.
- 0if 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: HGET key field
- 
Return: - Bulk string (value) or nilif field does not exist.
 
- Bulk string (value) or 
- 
RESP Protocol: *3 $4 HGET $3 key $5 field
4. HGETALL
- 
Purpose: Get all fields and values in a hash. 
- 
Syntax: HGETALL key
- 
Return: - Array of [field1, value1, field2, value2, ...].
 
- Array of 
- 
RESP Protocol: *2 $7 HGETALL $3 key
5. HMSET (⚠️ Deprecated, use HSET)
- 
Purpose: Set multiple field-value pairs. 
- 
Syntax: HMSET key field value [field value ...]
- 
Return: - Always OK.
 
- Always 
- 
RESP Protocol: *6 $5 HMSET $3 key $5 field $5 value $5 field2 $5 value2
6. HMGET
- 
Purpose: Get values of multiple fields. 
- 
Syntax: 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: 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: HEXISTS key field
- 
Return: - 1if exists,- 0if not.
 
- 
RESP Protocol: *3 $7 HEXISTS $3 key $5 field
9. HKEYS
- 
Purpose: Get all field names in a hash. 
- 
Syntax: HKEYS key
- 
Return: - Array of field names.
 
- 
RESP Protocol: *2 $5 HKEYS $3 key
10. HVALS
- 
Purpose: Get all values in a hash. 
- 
Syntax: HVALS key
- 
Return: - Array of values.
 
- 
RESP Protocol: *2 $5 HVALS $3 key
11. HLEN
- 
Purpose: Get number of fields in a hash. 
- 
Syntax: 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: HSCAN key cursor [MATCH pattern] [COUNT count]
- 
Return: - Array: [new-cursor, [field1, value1, ...]]
 
- Array: 
- 
RESP Protocol: *3 $5 HSCAN $3 key $1 0