Hash structures in Redis allow storage of key-value pairs where the value itself is a collection of field-value mappings, similar to dictionaries in Python or maps in Java.
Storing Data
Use HSET to insert field-value pairs into a hash:
127.0.0.1:6379> HSET users username john age empty location mars
(integer) 3
Retreiving Individual Values
Retrieve specific fields using HGET:
127.0.0.1:6379> HGET users age
"empty"
127.0.0.1:6379> HGET users username
"john"
127.0.0.1:6379> HGET users location
"mars"
Updating Values
Modify existing fields with HSET:
127.0.0.1:6379> HSET users age adult
(integer) 0
127.0.0.1:6379> HGET users age
"adult"
Removing Specific Fields
Delete individual fields using HDEL:
127.0.0.1:6379> HDEL users age
(integer) 1
127.0.0.1:6379> HGET users username
"john"
127.0.0.1:6379> HGET users age
(nil)
127.0.0.1:6379> HGET users location
"mars"
Deleting Entire Hash
Remove the complete hash structure with DEL:
127.0.0.1:6379> DEL users
(integer) 1
127.0.0.1:6379> HGET users username
(nil)
127.0.0.1:6379> HGET users location
(nil)
Checking Field Existence
Verify if a field exists within a hash using HEXISTS:
127.0.0.1:6379> HSET users username john age empty location mars
(integer) 3
127.0.0.1:6379> HEXISTS users username
(integer) 1
127.0.0.1:6379> HEXISTS users height
(integer) 0
Conditional Field Creation
Create fields only if they don't already exist with HSETNX:
127.0.0.1:6379> HGET users username
"john"
127.0.0.1:6379> HSETNX users username newname
(integer) 0
127.0.0.1:6379> HGET users username
"john"
127.0.0.1:6379> HGET users height
(nil)
127.0.0.1:6379> HSETNX users height 175
(integer) 1
127.0.0.1:6379> HGET users height
"175"
Retrieving Multiple Fields
Fetch several field values simultaneously using HMGET:
127.0.0.1:6379> HSET users username john age empty address venus
(integer) 3
127.0.0.1:6379> HMGET users username age address
1) "john"
2) "empty"
3) "venus"
Retrieving All Field-Value Pairs
Get all fields and their values with HGETALL:
127.0.0.1:6379> HGETALL users
1) "username"
2) "john"
3) "age"
4) "empty"
5) "address"
6) "venus"
Retrieving All Field Names
List all field names in the hash using HKEYS:
127.0.0.1:6379> HKEYS users
1) "username"
2) "age"
3) "address"
Retrieving All Values
Extract all values from the hash with HVALS:
127.0.0.1:6379> HVALS users
1) "john"
2) "empty"
3) "venus"
Numeric Operations
Incrementing Values
Increase numeric field values using HINCRBY:
127.0.0.1:6379> HGET users counter
"1"
127.0.0.1:6379> HINCRBY users counter 1
(integer) 2
127.0.0.1:6379> HINCRBY users counter 1
(integer) 3
127.0.0.1:6379> HINCRBY users counter 1
(integer) 4
Decrementing Values
Decraese numeric field values with HINCRBY:
127.0.0.1:6379> HGET users counter
"4"
127.0.0.1:6379> HINCRBY users counter -1
(integer) 3
127.0.0.1:6379> HINCRBY users counter -1
(integer) 2
127.0.0.1:6379> HINCRBY users counter -1
(integer) 1
Floating Point Operations
Handle decimal increments with HINCRBYFLOAT:
127.0.0.1:6379> HGET users counter
"1"
127.0.0.1:6379> HINCRBYFLOAT users counter 0.7
"1.7"
127.0.0.1:6379> HINCRBYFLOAT users counter 0.7
"2.4"
127.0.0.1:6379> HINCRBYFLOAT users counter 0.7
"3.1"
127.0.0.1:6379> HINCRBYFLOAT users counter 10.9
"13.99999999999999999" # Precision limitations apply
Measuring Hash Size
Determine the number of field-value pairs with HLEN:
127.0.0.1:6379> HLEN users
(integer) 4
# The users hash contains 4 field-value pairs
Measuring Field Value Length
Calculate the length of a specific field's value using HSTRLEN:
127.0.0.1:6379> HGET users username
"john"
127.0.0.1:6379> HSTRLEN users username
(integer) 4
Setting Expiration
Apply time-to-live settings with EXPIRE:
# Set expiration to 10 seconds
127.0.0.1:6379> EXPIRE users 10
(integer) 1
# Check remaining time
127.0.0.1:6379> TTL users
(integer) -2
# After expiration, data is no longer accessible
127.0.0.1:6379> HGETALL users
(empty array)