Redis data type hash and usage scenarios
Introduction: Redis data type hash and usage scenarios
Hash is actually the existence of key-value pairs in Java, and redis is no exception. The usage scenarios are:
New storage requirements: group a series of stored data for easy management, typical applications store object information
Required storage structure: one storage space saves multiple key-value pairs of data
Hash type: The bottom layer uses a hash table structure to implement data storage
Hash storage structure optimization
If the number of fields is small, the storage structure is optimized to an array-like structure
If the number of fields is large, the storage structure uses the HashMap structure
Notes on hash type data operations
The value under the hash type can only store strings, other data types are not allowed, and there is no nesting phenomenon. If the data is not obtained,
The corresponding value is (nil)
Each hash can store 2 to the power of 32 - 1 key-value pair
The hash type is very close to the data storage form of the object, and can flexibly add and delete object attributes. But the original purpose of hash design is not to save
It is designed to store a large number of objects, remember not to abuse, let alone use hash as a list of objects
The hgetall operation can obtain all attributes. If there are too many internal fields, the efficiency of traversing the overall data will be very low, and it may become a data access
bottleneck
Basic operations of hash data type
add/modify data
hset key field value
Get the data of the specified key
hget key field
get all data
hgetall key
delete the specified key
hdel key field1 [field2]
Add/modify multiple data, the same operation as string, add an m in front of set after h
hmset key field1 value1 field2 value2 …
Get multiple data
hmget key field1 field2 …
Get the number of fields in a hash table
hlen key
Get whether the specified field exists in the hash table
hexists key field
hash type data extension operation
Get all field names or field values in the hash table
hkeys key
hvals key
Set the numeric data of the specified field to increase the value of the specified range
hincrby key field increment
hincrbyfloat key field increment
Notes on hash type data operations
The value under the hash type can only store strings, other data types are not allowed, and there is no nesting phenomenon. If the data is not obtained,
The corresponding value is (nil)
Each hash can store 2 to the power of 32 - 1 key-value pair
The hash type is very close to the data storage form of the object, and can flexibly add and delete object attributes. But the original purpose of hash design is not to save
It is designed to store a large number of objects, remember not to abuse, let alone use hash as a list of objects
The hgetall operation can obtain all attributes. If there are too many internal fields, the efficiency of traversing the overall data will be very low, and it may become a data access
bottleneck
hash type application scenarios
* With the customer id as the key, each customer creates a hash storage structure to store the corresponding shopping cart information
* Store the item number as a field and the purchase quantity as a value
*Add item: add a new field and value
*Browse: traverse the hash
*Change quantity: auto increment/decrement, set value
*Delete item: delete field
*Clear: delete key
*Only model design in shopping cart is discussed here
*Persistent synchronization between shopping cart and database, relationship between shopping cart and order, shopping cart information storage for users who are not logged in will not be discussed
*Use the merchant id as the key
* Take the id of the product participating in the snap-up as the field
* Take the number of items participating in the snap-up as the corresponding value
*Use the method of devaluation to control the quantity of products when panic buying
*There are practical problems such as oversold in the actual business, which will not be discussed here.
Redis is applied to the data storage design of rush purchases, limited purchases, limited coupons, activation codes, etc.
Business scene
String storage object (json) and hash storage object
Hash is actually the existence of key-value pairs in Java, and redis is no exception. The usage scenarios are:
New storage requirements: group a series of stored data for easy management, typical applications store object information
Required storage structure: one storage space saves multiple key-value pairs of data
Hash type: The bottom layer uses a hash table structure to implement data storage
Hash storage structure optimization
If the number of fields is small, the storage structure is optimized to an array-like structure
If the number of fields is large, the storage structure uses the HashMap structure
Notes on hash type data operations
The value under the hash type can only store strings, other data types are not allowed, and there is no nesting phenomenon. If the data is not obtained,
The corresponding value is (nil)
Each hash can store 2 to the power of 32 - 1 key-value pair
The hash type is very close to the data storage form of the object, and can flexibly add and delete object attributes. But the original purpose of hash design is not to save
It is designed to store a large number of objects, remember not to abuse, let alone use hash as a list of objects
The hgetall operation can obtain all attributes. If there are too many internal fields, the efficiency of traversing the overall data will be very low, and it may become a data access
bottleneck
Basic operations of hash data type
add/modify data
hset key field value
Get the data of the specified key
hget key field
get all data
hgetall key
delete the specified key
hdel key field1 [field2]
Add/modify multiple data, the same operation as string, add an m in front of set after h
hmset key field1 value1 field2 value2 …
Get multiple data
hmget key field1 field2 …
Get the number of fields in a hash table
hlen key
Get whether the specified field exists in the hash table
hexists key field
hash type data extension operation
Get all field names or field values in the hash table
hkeys key
hvals key
Set the numeric data of the specified field to increase the value of the specified range
hincrby key field increment
hincrbyfloat key field increment
Notes on hash type data operations
The value under the hash type can only store strings, other data types are not allowed, and there is no nesting phenomenon. If the data is not obtained,
The corresponding value is (nil)
Each hash can store 2 to the power of 32 - 1 key-value pair
The hash type is very close to the data storage form of the object, and can flexibly add and delete object attributes. But the original purpose of hash design is not to save
It is designed to store a large number of objects, remember not to abuse, let alone use hash as a list of objects
The hgetall operation can obtain all attributes. If there are too many internal fields, the efficiency of traversing the overall data will be very low, and it may become a data access
bottleneck
hash type application scenarios
* With the customer id as the key, each customer creates a hash storage structure to store the corresponding shopping cart information
* Store the item number as a field and the purchase quantity as a value
*Add item: add a new field and value
*Browse: traverse the hash
*Change quantity: auto increment/decrement, set value
*Delete item: delete field
*Clear: delete key
*Only model design in shopping cart is discussed here
*Persistent synchronization between shopping cart and database, relationship between shopping cart and order, shopping cart information storage for users who are not logged in will not be discussed
*Use the merchant id as the key
* Take the id of the product participating in the snap-up as the field
* Take the number of items participating in the snap-up as the corresponding value
*Use the method of devaluation to control the quantity of products when panic buying
*There are practical problems such as oversold in the actual business, which will not be discussed here.
Redis is applied to the data storage design of rush purchases, limited purchases, limited coupons, activation codes, etc.
Business scene
String storage object (json) and hash storage object
Related Articles
-
A detailed explanation of Hadoop core architecture HDFS
Knowledge Base Team
-
What Does IOT Mean
Knowledge Base Team
-
6 Optional Technologies for Data Storage
Knowledge Base Team
-
What Is Blockchain Technology
Knowledge Base Team
Explore More Special Offers
-
Short Message Service(SMS) & Mail Service
50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00